Java XOR神经网络未正确训练
我有一个带有2个输入、2个隐藏神经元和1个输出神经元的神经网络来解决异或问题。我随机初始化0到1之间的权重,我使用Java XOR神经网络未正确训练,java,neural-network,xor,Java,Neural Network,Xor,我有一个带有2个输入、2个隐藏神经元和1个输出神经元的神经网络来解决异或问题。我随机初始化0到1之间的权重,我使用0.1的学习率和sigmoid激活功能 当我只训练一个选项时,例如目标为1的1和0,它可以很好地工作并给出适当的猜测。但是,当我尝试将所有可能的输入一起训练时,输出收敛于0.5-0.6 我尝试过改变学习率、随机初始化权重的范围和网络训练的次数,但这对最终输出没有影响 这是一个链接到我的代码 关于如何解决这个问题有什么想法吗 我怀疑反向传播没有正确实现。例如,第17至20页中给出了概述
0.1
的学习率和sigmoid
激活功能
当我只训练一个选项时,例如目标为1的1和0,它可以很好地工作并给出适当的猜测。但是,当我尝试将所有可能的输入一起训练时,输出收敛于0.5-0.6
我尝试过改变学习率、随机初始化权重的范围和网络训练的次数,但这对最终输出没有影响
这是一个链接到我的代码
关于如何解决这个问题有什么想法吗 我怀疑反向传播没有正确实现。例如,第17至20页中给出了概述
Output\u Neuron
-类的tuneweights
-和delta\u weights
-方法已正确实现。然而,在这一步中,必须确定数组weightDeltaHidden
(参见代码中的注释),这将在以后调整隐藏神经元的权重时需要
隐藏神经元
-类的tuneweights
-和delta_权重
-方法似乎没有正确实现。这里,除其他外,必须使用先前确定的数组weightDeltaHidden
在下面的代码中,我进行了必要的更改,但没有实质性地更改代码的设计。但也许重构是有意义的
输出中的变化\u神经元
-类别:
...
private double[] weightedDeltaHidden;
...
Output_Neuron(int hiddenNeurons) {
...
this.weightedDeltaHidden = new double[hiddenNeurons];
}
...
void tuneWeights(double LR, double[] hidden_output, int target) {
double delta = (target - output) * f.dSigmoid(output);
for (int i = 0; i < weights.length; i++) {
weights[i] += delta_weights(i, LR, delta, hidden_output);
}
}
double delta_weights(int i, double LR, double delta, double[] hidden_output) {
weightedDeltaHidden[i] = delta * weights[i]; // weightedDeltaHidden is the product of delta of this output neuron and the weight of the i-th hidden neuron.
// That value is needed when the weights of the hidden neurons are tuned...
return LR * delta * hidden_output[i];
}
...
double[] getWeightedDeltaHidden() {
return weightedDeltaHidden;
}
...
void tuneWeights(double LR, int[] inputs, double weightedDeltaHiddenTotal) {
for (int i = 0; i < weights.length; i++) {
weights[i] += delta_weights(LR, inputs[i], weightedDeltaHiddenTotal);
}
}
private double delta_weights(double LR, double input, double weightedDeltaHiddenTotal) {
double deltaOutput = f.dSigmoid(output) * weightedDeltaHiddenTotal;
return LR * deltaOutput * input;
}
...
调整隐藏权重的列车方法中网络
-类的变化:
void train(int[] inputs, int target) {
...
//tune Hidden weights
for (int i = 0; i < numOfHiddenNeurons; i++) {
double weightedDeltaHiddenTotal = 0;
for (int j = 0; j < numOfOutputNeurons; j++) {
weightedDeltaHiddenTotal += output_neurons[j].getWeightedDeltaHidden()[i]; // weightedDeltaHiddenTotal is the sum of the weightedDeltaHidden over all output neurons. Each weightedDeltaHidden
} // is the product of delta of the j-th output neuron and the weight of the i-th hidden neuron.
hidden_neurons[i].tuneWeights(LR, inputs, weightedDeltaHiddenTotal);
}
}
对于100_000_000列车
呼叫(2个隐藏神经元)
隐藏神经元的增加会提高性能。下面显示了1_000_000列车
-呼叫(4个隐藏神经元)的典型输出:
Error: 1.2617e-02 in cycle 0
Error: 7.9950e-04 in cycle 100000
Error: 4.2567e-04 in cycle 200000
Error: 1.7279e-04 in cycle 300000
Error: 1.2246e-04 in cycle 400000
Error: 1.0456e-04 in cycle 500000
Error: 6.9140e-05 in cycle 600000
Error: 6.8698e-05 in cycle 700000
Error: 5.1640e-05 in cycle 800000
Error: 4.4534e-05 in cycle 900000
0 0: 0.0092
1 0: 0.9905
0 1: 0.9912
1 1: 0.0089
也许你可以看看下面的内容来获得线索:。只是一个提示。就像在本文档中一样,总的来说,将尝试不同方法的结果形象化是一个好主意。我很难理解的是,当我只训练一个选项时,它工作得非常好,但当我集体训练它们时,它却不起作用。
Error: 2.4755e-01 in cycle 0
Error: 2.7771e-04 in cycle 5000000
Error: 6.8378e-06 in cycle 10000000
Error: 5.4317e-05 in cycle 15000000
Error: 6.8956e-05 in cycle 20000000
Error: 2.1072e-06 in cycle 25000000
Error: 2.6281e-05 in cycle 30000000
Error: 2.1630e-05 in cycle 35000000
Error: 1.1546e-06 in cycle 40000000
Error: 1.7690e-05 in cycle 45000000
Error: 8.6837e-07 in cycle 50000000
Error: 1.3603e-05 in cycle 55000000
Error: 1.2905e-05 in cycle 60000000
Error: 2.1657e-05 in cycle 65000000
Error: 1.1594e-05 in cycle 70000000
Error: 1.9191e-05 in cycle 75000000
Error: 1.7273e-05 in cycle 80000000
Error: 9.1364e-06 in cycle 85000000
Error: 1.5221e-05 in cycle 90000000
Error: 1.4501e-05 in cycle 95000000
0 0: 0.0008
1 0: 0.9961
0 1: 0.9961
1 1: 0.0053
Error: 1.2617e-02 in cycle 0
Error: 7.9950e-04 in cycle 100000
Error: 4.2567e-04 in cycle 200000
Error: 1.7279e-04 in cycle 300000
Error: 1.2246e-04 in cycle 400000
Error: 1.0456e-04 in cycle 500000
Error: 6.9140e-05 in cycle 600000
Error: 6.8698e-05 in cycle 700000
Error: 5.1640e-05 in cycle 800000
Error: 4.4534e-05 in cycle 900000
0 0: 0.0092
1 0: 0.9905
0 1: 0.9912
1 1: 0.0089