Search code examples
pythonneural-networkdeep-learningartificial-intelligencebackpropagation

What happens after backpropagation in the learning phase within Neural Network?


Within a Neural Network during the learning phase ,there are two mechanisms happening. Feedforward and Backpropagation. Taking an example of XOR operation.

A   B   Q
0   0   0
0   1   1
1   0   1
1   1   0

For the first pass(0,0->0) feedforward takes place, then backpropagation happens.After this step all the weights are recalculated .
What happens now?

Question-1:Again the same input 0,0 is feedforward with the new calculated weights(during the backpropagation) and then backprogated until the error becomes nill? If yes, what happens if the error is never nill? Which brings me to the next question.

Question-2:When would the learning for the next pass(0,1->1) happen?

Question-3: Suppose the concluded weights for the 1st pass is 10.3,-2.3,5.5 .The second pass(0,1->1) starts the feedforward with the concluded weights of the first pass?


If I have to describe via code which of the following code would be correct

Common Code for the below options

averageErrorUntilLearn = 0.002;
inputs = [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
];
outputs = [0, 1, 1, 0];
inputNeurons = 2;
hiddenLayers = 1;
outputNeurons = 1;

//initialize the Neural Net
neuralNetObj = NeuralNet();
neuralNetObj.createTopology(inputNeurons, hiddenLayers, outputNeurons);
weightsForTheNetwork = randomWeights();


Question 3 scenario- The weights calculated in each pass is sent down to the next pass

//learn 
while (averageError > averageErrorUntilLearn):
    for i in range(0,len(input)):
        console.log("Current Pass-" + i);
        neuralNetObj.learningParams(inputs[i], outputs[i]);
        neuralNetObj.initializeWeights(weightsForTheNetwork);
        neuralNetObj.feedforward();
        neuralNetObj.backPropagate();
        weightsForTheNetwork=neuralNetObj.getNewWeights();
        averageError = neuralNetObj.getAverageError();

Question 1 scenario- Each pass is calculated with its own weights till it reaches the desired learning and then the weights are sent down the new pass

//learn 
for i in range(0,len(input)):
    while (averageError > averageErrorUntilLearn):
        console.log("Current Pass-" + i);
        neuralNetObj.learningParams(inputs[i], outputs[i]);
        neuralNetObj.initializeWeights(weightsForTheNetwork);
        neuralNetObj.feedforward();
        neuralNetObj.backPropagate();
        weightsForTheNetwork = neuralNetObj.getNewWeights();
        averageError = neuralNetObj.getAverageError();


Or am I complete wrong here and none of the above scenarios are true?


Solution

  • 1) Yes, the backprop step will continue to update the weights until the feedforward step has a 0 error. Regardless of whether it reaches a 0 error or not, you must define a stopping criteria to tell it when to stop. The simplest is to set a fixed number of iterations to train. This is a good, simple way to get started. Real problems will use something more sophisticated such as periodically checking accuracy on a separate validation step and stopping when the accuracy stops improving. However, just use a fixed number of iterations for an XOR problem. It is up to you to decide and tell it what the stopping criteria is.

    2) You do not want to train repeatedly on one instance (such as 0,0->0), and then switch to a different instance. If you're training one instance at a time, go through first instance once (forward and backprop steps), then the next instance, and so on to the end of the training data. Then start over at the beginning again and go through each instance again. In this way it is interleaving the training for each case represented in the training data. As @Maxim said, it's more common to train in batches. In a batch, it will do all the forward passes simultaneously and collect all of the network's guesses about the answers. Then it will calculate the error for every instance of the batch, and backpropagate once to correct for the average error across all instances in the batch. Hence it is learning how to deal with all the cases in the batch simultaneously. This is faster than doing one instance at a time, but the network should be able to learn the problem either way. The important point here is that it is learning about all of the cases in the training data at the same time, rather than one-by-one.

    3) Yes, once you do the backprop step it will apply the updates to the weights, and the next training step will use the new weights.