使用Neuroph尝试创建一个人工神经网络,结束本教程(https://www.baeldung.com/neuroph)
这是我的代码:
(VOCAB_SIZE, 4 * LATENT_SIZE) = (100, 80)
这是程序的输出:
public class Test {
private static NeuralNetwork ann = null;
public static void main(String args[]) {
ann = assembleNeuralNetwork();
trainNeuralNetwork(ann);
ann.setInput(0, 1);
ann.calculate();
print("0, 1", ann.getOutput()[0], 1.0);
ann.setInput(1, 0);
ann.calculate();
print("1, 0", ann.getOutput()[0], 1.0);
ann.setInput(0, 0);
ann.calculate();
print("0, 0", ann.getOutput()[0], 0.0);
ann.setInput(1, 1);
ann.calculate();
print("1, 1", ann.getOutput()[0], 0.0);
}
private static void print(String input, double output, double actual) {
System.out.println("Testing: " + input + " Expected: " + actual + " Result: " + output);
}
public static NeuralNetwork assembleNeuralNetwork() {
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 outputLayer = new Layer();
outputLayer.addNeuron(new Neuron());
NeuralNetwork ann = new NeuralNetwork();
ann.addLayer(0, inputLayer);
ann.addLayer(1, hiddenLayerOne);
ann.addLayer(2, outputLayer);
ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(1));
ConnectionFactory.fullConnect(ann.getLayerAt(1), ann.getLayerAt(2));
ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(ann.getLayersCount() - 1), false);
ann.setInputNeurons(inputLayer.getNeurons());
ann.setOutputNeurons(outputLayer.getNeurons());
ann.setNetworkType(NeuralNetworkType.NEURO_FUZZY_REASONER);
return ann;
}
public static void trainNeuralNetwork(NeuralNetwork ann) {
int inputSize = 2;
int outputSize = 1;
DataSet ds = new DataSet(inputSize, outputSize);
ds.add(new double[] { 0, 1 }, new double[] { 1 });
ds.add(new double[] { 1, 1 }, new double[] { 0 });
ds.add(new double[] { 0, 0 }, new double[] { 0 });
ds.add(new double[] { 1, 0 }, new double[] { 1 });
BackPropagation bp = new BackPropagation();
bp.setMaxIterations(100000);
ann.learn(ds, bp);
}
}
如果要测试的两个数字相同,则结果应为0,但是如果两个数字不同,则结果应为1。
我不明白我要去哪里错了。