我正在尝试将程序从Matlab移植到Java。使用Matlab是因为它具有非常全面的神经网络实现。我现在想将项目移至Java。我正在寻找Java中的综合库,并且遇到过Neuroph。因此,首先,我需要运行一个非常简单的示例,以确保在尝试移植所有内容之前一切都正常。我碰到了本教程。 https://www.baeldung.com/neuroph。我试图在Eclipse中实现它。实现没有错误,因为非常基本的NN的结果是错误的。我希望此示例为1,而我总是为零。
测试:1、0预期:1.0结果:0.0 测试:0,1预期:1.0结果:0.0 测试:1、1预期:0.0结果:0.0 测试:0,0预期:0.0结果:0.0
谁能建议为什么NN设置不正确?非常感谢
import org.neuroph.core.*;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.learning.BackPropagation;
import org.neuroph.util.*;
public class NeuralNetworkExample {
public static void main(String[] args) {
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<BackPropagation> ann = new NeuralNetwork<BackPropagation>();
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.setInputNeurons(inputLayer.getNeurons());
ann.setOutputNeurons(outputLayer.getNeurons());
int input=2;
int output=1;
DataSet ds = new DataSet(input,output);
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);
BackPropagation backPropagation = new BackPropagation();
backPropagation.setMaxIterations(1000);
ann.learn(ds,backPropagation);
ann.setInput(1,0);
ann.calculate();
double[] out = ann.getOutput();
System.out.println(out[0]);
}
}