概述
我正在尝试理解神经网络的机制。我还没有完全掌握其背后的数学原理,但我认为我已经了解了如何实现它。我目前有一个神经网络,它可以学习AND、OR和NOR训练模式。然而,我似乎无法让它实现XOR模式。我的前馈神经网络由2个输入、3个隐藏层和1个输出组成。权重和偏置在-0.5到0.5之间随机设置,输出是通过 sigmoid激活函数生成的
算法
到目前为止,我猜测我在训练算法中犯了错误,下面是我的算法描述:
- 对于输出层的每个神经元,提供一个
error
值,该值为desiredOutput - actualOutput
—转到步骤3 - 对于隐藏层或输入层(从后往前)的每个神经元,提供一个
error
值,该值为所有forward connection weights * the errorGradient of the neuron at the other end of the connection
的总和 —转到步骤3 - 对于每个神经元,使用提供的
error
值,生成一个error gradient
,其等于output * (1-output) * error
。 —转到步骤4 - 对于每个神经元,调整偏置为
current bias + LEARNING_RATE * errorGradient
。然后调整每个后向连接的权重为current weight + LEARNING_RATE * output of neuron at other end of connection * this neuron's errorGradient
我在线训练我的神经网络,因此每次训练样本后都会运行这个过程。
代码
这是运行神经网络的主要代码:
private void simulate(double maximumError) { int errorRepeatCount = 0; double prevError = 0; double error; // summed squares of errors int trialCount = 0; do { error = 0; // loop through each training set for(int index = 0; index < Parameters.INPUT_TRAINING_SET.length; index++) { double[] currentInput = Parameters.INPUT_TRAINING_SET[index]; double[] expectedOutput = Parameters.OUTPUT_TRAINING_SET[index]; double[] output = getOutput(currentInput); train(expectedOutput); // Subtracts the expected and actual outputs, gets the average of those outputs, and then squares it. error += Math.pow(getAverage(subtractArray(output, expectedOutput)), 2); } } while(error > maximumError);
现在是train()
函数:
public void train(double[] expected) { layers.outputLayer().calculateErrors(expected); for(int i = Parameters.NUM_HIDDEN_LAYERS; i >= 0; i--) { layers.allLayers[i].calculateErrors(); }}
输出层的calculateErrors()
函数:
public void calculateErrors(double[] expectedOutput) { for(int i = 0; i < numNeurons; i++) { Neuron neuron = neurons[i]; double error = expectedOutput[i] - neuron.getOutput(); neuron.train(error); }}
普通(隐藏和输入)层的calculateErrors()
函数:
public void calculateErrors() { for(int i = 0; i < neurons.length; i++) { Neuron neuron = neurons[i]; double error = 0; for(Connection connection : neuron.forwardConnections) { error += connection.output.errorGradient * connection.weight; } neuron.train(error); }}
完整的Neuron类:
package neuralNet.layers.neurons;import java.util.ArrayList;import java.util.List;import java.util.Random;import neuralNet.Parameters;import neuralNet.layers.NeuronLayer;public class Neuron {private double output, bias;public List<Connection> forwardConnections = new ArrayList<Connection>(); // Forward = layer closer to input -> layer closer to outputpublic List<Connection> backwardConnections = new ArrayList<Connection>(); // Backward = layer closer to output -> layer closer to inputpublic double errorGradient;public Neuron() { Random random = new Random(); bias = random.nextDouble() - 0.5;}public void addConnections(NeuronLayer prevLayer) { // This is true for input layers. They create their connections differently. (See InputLayer class) if(prevLayer == null) return; for(Neuron neuron : prevLayer.neurons) { Connection.createConnection(neuron, this); }}public void calcOutput() { output = bias; for(Connection connection : backwardConnections) { connection.input.calcOutput(); output += connection.input.getOutput() * connection.weight; } output = sigmoid(output);}private double sigmoid(double output) { return 1 / (1 + Math.exp(-1*output));}public double getOutput() { return output;}public void train(double error) { this.errorGradient = output * (1-output) * error; bias += Parameters.LEARNING_RATE * errorGradient; for(Connection connection : backwardConnections) { // for clarification: connection.input refers to a neuron that outputs to this neuron connection.weight += Parameters.LEARNING_RATE * connection.input.getOutput() * errorGradient; }}}
结果
当我训练AND、OR或NOR时,网络通常可以在大约1000个周期内收敛,但是当我用XOR训练时,输出变得固定并且永远不会收敛。那么,我做错了什么?有什么想法吗?
编辑
根据其他人的建议,我重新开始并实现了没有类的神经网络…它工作了。我仍然不确定上述代码中的问题出在哪里,但问题就在那里某处。
回答:
LiKao的评论建议简化我的实现并去掉面向对象的方面,解决了我的问题。上述算法中的缺陷尚不明确,但我现在有一个工作的神经网络,它的规模小得多。
欢迎继续提供关于我之前实现的问题的见解,因为将来其他人可能会遇到相同的问题。