根据这个 python 示例,我创建了一个简单的 3 层神经网络:链接(PS:您必须向下滚动,直到到达第 2 部分)
这是我对代码的 Java 实现:
private void trainNet()
{
// INPUT is a 4*3 matrix
// SYNAPSES is a 3*4 matrix
// SYNAPSES2 is a 4*1 matrix
// 4*3 matrix DOT 3*4 matrix => 4*4 matrix: unrefined test results
double[][] layer1 = sigmoid(dot(inputs, synapses), false);
// 4*4 matrix DOT 4*1 matrix => 4*1 matrix: 4 final test results
double[][] layer2 = sigmoid(dot(layer1, synapses2), false);
// 4*1 matrix - 4*1 matrix => 4*1 matrix: error of 4 test results
double[][] layer2Error = subtract(outputs, layer2);
// 4*1 matrix DOT 4*1 matrix => 4*1 matrix: percentage of change of 4 test results
double[][] layer2Delta = dot(layer2Error, sigmoid(layer2, true));
// 4*1 matrix DOT 3*1 matrix => 4*1 matrix
double[][] layer1Error = dot(layer2Delta, synapses2);
// 4*1 matrix DOT 4*4 matrix => 4*4 matrix: percentage of change of 4 test results
double[][] layer1Delta = dot(layer1Error, sigmoid(layer1, true));
double[][] transposedInputs = transpose(inputs);
double[][] transposedLayer1 = transpose(layer1);
// 4*4 matrix DOT 4*1 matrix => 4*1 matrix: the updated weights
// Update the weights
synapses2 = sum(synapses2, dot(transposedLayer1, layer2Delta));
// 3*4 matrix DOT 4*4 matrix => 3*4 matrix: the updated weights
// Update the weights
synapses = sum(synapses, dot(transposedInputs, layer1Delta));
// Test each value of two 4*1 matrices with each other
testValue(layer2, outputs);
}
我自己创建的点、求和、减法和转置函数,我很确定它们能完美地完成工作。
第一批输入给我一个大约 0.4 的误差,这没关系,因为权重是随机值。在第二次运行时,误差范围更小,但只有很小的量(0.001)
在 500,000 个批次(总共 2,000,000 次测试)之后,网络仍然没有给出任何正确的值!所以我尝试使用更多的批次。使用 1,000,000 个批次(总共 4,000,000 次测试),网络生成了高达 16,900 个正确结果。
谁能告诉我发生了什么?
这些是使用的重量:
第一层:
- 2.038829298171684 2.816232761170282 1.6740269469812146 1.634422766238497
- 1.5890997594993828 1.7909325329112222 2.101840236824494 1.063579126586681
- 3.761238407071311 3.757148454039234 3.7557450538398176 3.6715972104291605
第二层:
- -0.019603811941904248
- 218.38253323323553
- 53.70133275445734
-272.83589796861514
编辑:感谢 lsnare 向我指出使用库会更容易!
对于那些感兴趣的是使用 math.nist.gov/javanumerics 库的工作代码:
private void trainNet()
{
// INPUT is a 4*3 matrix
// SYNAPSES is a 3*4 matrix
// SYNAPSES2 is a 4*1 matrix
// 4*3 matrix DOT 3*4 matrix => 4*4 matrix: unrefined test results
Matrix hiddenLayer = sigmoid(inputs.times(synapses), false);
// 4*4 matrix DOT 4*1 matrix => 4*1 matrix: 4 final test results
Matrix outputLayer = sigmoid(hiddenLayer.times(synapses2), false);
// 4*1 matrix - 4*1 matrix => 4*1 matrix: error of 4 test results
Matrix outputLayerError = outputs.minus(outputLayer);
// 4*1 matrix DOT 4*1 matrix => 4*1 matrix: percentage of change of 4 test results
Matrix outputLayerDelta = outputLayerError.arrayTimes(sigmoid(outputLayer, true));
// 4*1 matrix DOT 1*4 matrix => 4*4 matrix
Matrix hiddenLayerError = outputLayerDelta.times(synapses2.transpose());
// 4*4 matrix DOT 4*4 matrix => 4*4 matrix: percentage of change of 4 test results
Matrix hiddenLayerDelta = hiddenLayerError.arrayTimes(sigmoid(hiddenLayer, true));
// 4*4 matrix DOT 4*1 matrix => 4*1 matrix: the updated weights
// Update the weights
synapses2 = synapses2.plus(hiddenLayer.transpose().times(outputLayerDelta));
// 3*4 matrix DOT 4*4 matrix => 3*4 matrix: the updated weights
// Update the weights
synapses = synapses.plus(inputs.transpose().times(hiddenLayerDelta));
// Test each value of two 4*1 matrices with each other
testValue(outputLayer.getArrayCopy(), outputs.getArrayCopy());
}