gpt4 book ai didi

java - 代码使用 Neuroph 给出了 VectorSizeMismatchException

转载 作者:行者123 更新时间:2023-12-02 04:49:24 28 4
gpt4 key购买 nike

我知道如何使用 java 进行编码,但我是 Neuroph 的新手,我在这些代码中遇到了 VectorSizeMismatchException

主要:https://pastebin.com/dntWRMZN

public static void main(String[] args) {
AiManager.trainNeuralNetwork(AiManager.initilizeNetwork());
}

经理: https://pastebin.com/csWsiVvt

import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.Neuron;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.Perceptron;
import org.neuroph.util.ConnectionFactory;
import org.neuroph.util.NeuralNetworkType;

public class AiManager {

public static NeuralNetwork<?> initilizeNetwork() {
Layer inputLayer = new Layer();
inputLayer.addNeuron(new Neuron());
inputLayer.addNeuron(new Neuron());

Layer hiddenLayerOne = new Layer();
hiddenLayerOne.addNeuron(new Neuron());
hiddenLayerOne.addNeuron(new Neuron());
hiddenLayerOne.addNeuron(new Neuron());
hiddenLayerOne.addNeuron(new Neuron());

Layer hiddenLayerTwo = new Layer();
hiddenLayerTwo.addNeuron(new Neuron());
hiddenLayerTwo.addNeuron(new Neuron());
hiddenLayerTwo.addNeuron(new Neuron());
hiddenLayerTwo.addNeuron(new Neuron());

Layer outputLayer = new Layer();
outputLayer.addNeuron(new Neuron());

NeuralNetwork<?> ann = new Perceptron(2, 1);

ann.setInputNeurons(inputLayer.getNeurons());
ann.setOutputNeurons(outputLayer.getNeurons());

ann.addLayer(0, inputLayer);
ann.addLayer(1, hiddenLayerOne);

ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(1));

ann.addLayer(2, hiddenLayerTwo);

ConnectionFactory.fullConnect(ann.getLayerAt(1), ann.getLayerAt(2));

ann.addLayer(3, outputLayer);

ConnectionFactory.fullConnect(ann.getLayerAt(2), ann.getLayerAt(3));

ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(ann.getLayersCount()-1), false);

ann.setNetworkType(NeuralNetworkType.MULTI_LAYER_PERCEPTRON);

ann.setInputNeurons(inputLayer.getNeurons());
ann.setOutputNeurons(outputLayer.getNeurons());

return ann;
}

public static NeuralNetwork<?> trainNeuralNetwork(NeuralNetwork<?> ann) {
int inputSize = 2;
int outputSize = 1;
DataSet ds = new DataSet(inputSize, outputSize);

DataSetRow rOne = new DataSetRow(new double[] { 0, 1 }, new double[] { 1 });

ds.addRow(rOne);

DataSetRow rTwo = new DataSetRow(new double[] { 1, 1 }, new double[] { 0 });

ds.addRow(rTwo);

DataSetRow rThree = new DataSetRow(new double[] { 0, 0 }, new double[] { 0 });

ds.addRow(rThree);

DataSetRow rFour = new DataSetRow(new double[] { 1, 0 }, new double[] { 1 });

ds.addRow(rFour);

ann.learn(ds);

return ann;
}
}

最佳答案

此错误是由于 Neuroph API 的编码方式造成的,您可以看到您添加了两次表达式 ann.setInputNeurons(inputLayer.getNeurons());ann.setOutputNeurons( outputLayer.getNeurons());,但是如果在调用这些表达式之前和之后打印表达式 ann.getInputNeurons().size(),您将看到每次“设置”输入神经元,您添加新神经元。

查看NeuralNetwork类的源码:

/**
* Sets input neurons
*
* @param inputNeurons array of input neurons
*/
public void setInputNeurons(List<Neuron> inputNeurons) {
for (Neuron neuron : inputNeurons) {
this.inputNeurons.add(neuron);
}
}

理论上删除这 4 行应该可以消除错误。

关于java - 代码使用 Neuroph 给出了 VectorSizeMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456447/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com