(Java)隐藏层反向传播的部分导数

时间:2015-06-02 15:26:05

标签: java machine-learning artificial-intelligence neural-network

昨天我发布了一个关于first piece of the Back propagation aglorithm的问题。

今天我正在努力了解隐藏层。

很抱歉很多问题,我已经阅读了几个关于这个问题的网站和论文,但不管我读了多少,我仍然很难将它应用到实际的代码中。

这是我正在分析的代码(我在Java中工作,所以很高兴看一下Java示例)

// update weights for the hidden layer
    for (Neuron n : hiddenLayer) {
        ArrayList<Connection> connections = n.getAllInConnections();
        for (Connection con : connections) {
            double output = n.getOutput();
            double ai = con.leftNeuron.getOutput();
            double sumKoutputs = 0;
            int j = 0;
            for (Neuron out_neu : outputLayer) {
                double wjk = out_neu.getConnection(n.id).getWeight();
                double desiredOutput = (double) expectedOutput[j];
                double ak = out_neu.getOutput();
                j++;
                sumKoutputs = sumKoutputs
                        + (-(desiredOutput - ak) * ak * (1 - ak) * wjk);
            }

            double partialDerivative = output * (1 - output) * ai * sumKoutputs;
            double deltaWeight = -learningRate * partialDerivative;
            double newWeight = con.getWeight() + deltaWeight;
            con.setDeltaWeight(deltaWeight);
            con.setWeight(newWeight + momentum * con.getPrevDeltaWeight());
        }
    }

这里的一个真正问题是我不知道所有方法是如何工作的。

此代码遍历隐藏层中的所有神经元,并逐个通过隐藏层中每个神经元的每个连接。它抓住每个连接的输出?那么,这是传入连接的总和(可能通过Sig函数运行)然后*通过连接权重?然后“double ai”获取此特定节点的输入连接值?它只得到一个或神经元输入的总和吗?

然后第三个for循环几乎总结了一个“out_neu.getConnection(n.id).getWeight()”,我不太明白。那么,所需的输出是最终层节点的desiredOutput?那么ak是每个节点的实际输出(求和和激活函数)还是总和+激活*权重?

修改

我开始研究自己的代码,有人可以看一下吗?

公共类BackProp {

public  int layers = 3; 
public int hiddenNeuronsNum = 5;
public int outputNeuronsNum = 1;
public static final double eta = .1;
public double[][][] weights; //holds the network -- weights[layer][neuron][forwardConnetion]


  public void Back(){
for(int neuron = 0; neuron < outputNeuronsNum; neuron++){
    for(int connection = 0; connection < hiddenNeuronsNum; connection++){

        double expOutput = expectedOutput[neuron]; //the expected output from the neuron we're on
        double actOutput = actualOutput[neuron];
        double previousLayerOutput = holdNeuronValues[layers-1][neuron];

        double delta = eta *(actOutput * (1-actOutput) *(expOutput - actOutput)* previousLayerOutput);
        weights[layers-1][neuron][connection] += delta; //OKAY M&M said YOU HAD THIS MESSED UP, 3rd index means end neuron, 2nd means start.. moving from left to right
    }
}


 //Hidden Layer..   

for(int neuron = 0; neuron < outputNeuronsNum; neuron++){
    for(int connection = 0; connection < hiddenNeuronsNum; connection++){

        double input = holdNeuronValues[layers-3][connection]; //what this neuron sends on, -2 for the next layer
        double output = holdNeuronValues[layers-2][connection];
        double sumKoutputs = 0;

        //for the output layer
        for (int outputNeurons = 0; outputNeurons < weights[layers].length; outputNeurons++) {      
            double wjk = weights[layers-2][neuron][outputNeurons]; //get the weight
            double expOutput = expectedOutput[outputNeurons];
            double out = actualOutput[outputNeurons];
            sumKoutputs += (-(expOutput - out) * wjk);
        }
        double partialDerivative = -eta * output * (1 - output) * input * sumKoutputs;

    }
}

} }

0 个答案:

没有答案