- Cost Function and Back-propagation Algorithm
- Backpropagation Practice
- Let's first define a few variables that we will need to use:
- = total number of layers in the network
- = number of units (not counting bias unit) in layer
- = number of output units/classes
- For example:
- Binary classification
- 1 output (0 or 1)
- Multi-class classification(K classes)
- K output units
- The (regularized) logistic regression cost function is as follows:
- For neural networks, it is:
- the double sum simply adds up the logistic regression costs calculated for each cell in the output layer.
- the triple sum simply adds up the squares of all the individual Θs in the entire network.
- Like three loops to fetch all of the Θs
- the i in the triple sum does not refer to training example i
- The Backpropagation algorithm is used to learn the weights of a multilayer neural network with a ﬁxed architecture. It performs gradient descent to try to minimize the sum squared error between the network’s output values and the given target values.
- Our goal is to compute: . In this section, it is
- Forward Propagation:
- Takes your neural network and the initial input into that network and pushes the input through the network.
- It leads to the generation of an output hypothesis, which may be a single real number, or a vector.
- Back Propagation:
- Compare the output to the real value(y) and calculates how wrong the weights were.
- Then, using the error you've just calculated, back-calculates the error associated with each unit from the preceding layer(i.e. layer L-1) to the input layer.
- These "error" measurements for each unit can be used to calculate the partial derivatives
- Use the partial derivatives with gradient descent to try minimize the cost function and update all the s.
- This repeats until gradient descent reports convergence.
- Given training set
- Set . (use to capture )
- For training example
- Perform forward propagation to compute .
- Using , compute
Lis the total number of layers
- is the vector of outputs of the activation units for the last layer.
- So our "error values" for the last layer are .
- Compute using that steps us back from right to left.
- 算出每一层每个节点的偏差之和，即节点中每个元素乘以相应的 后出现的偏差之和
- : has the same dimensionality with .
- : [5 X 4]; : [4 X 1], then : [5 X 1]
- : the derivative of the activation function
gwith the input values given by .
- There will be no , because the first layer corresponds to the input layer.
- or with vectorization,
- Last step, we got for the nodes in every layer. This step we split the to every in the node, i.e.
- Note this:
iis the error of the affected node of layer
jis the node of layer
- the number of nodes(
m) in layer
lequals the number of the errors of the affected(
n) in layer
- and will be a
m*nmatrix, same as .
- Check the Derivation of Backpropagation in Refers.
- Hence we update our new matrix.
- , if .
- , if .
- The capital-delta matrix
Dis used as an "accumulator" to add up our values as we go along and eventually compute our partial derivative. Thus we get
- The detail of forward propagation:
- Recall that the cost function for a neural network. If we consider simple non-multiclass classification (k=1) and disregard regularization, the cost is computed with:
- Intuitively, is the "error" for (unit
l). More formally, the delta values are actually the derivative of the cost function:
- Recall that our derivative is the slope of a line tangent to the cost function, so the steeper the slope the more incorrect we are. Let us consider the following neural network below and see how we could calculate some :
- To calculate every single possible , we could start from the right of our diagram. We can think of our edges as our . Going from right to left, to calculate the value of , you can just take the over all sum of each weight times the it is coming from. For example: .
- With neural networks, we are working with sets of matrices:
In order to use optimizing functions such as "fminunc()", we will want to "unroll" all the elements and put them into one long vector:
thetaVector = [ Theta1(:); Theta2(:); Theta3(:); ] deltaVector = [ D1(:); D2(:); D3(:) ]
If the dimensions of Theta1 is 10x11, Theta2 is 10x11 and Theta3 is 1x11, then we can get back our original matrices from the "unrolled" versions as follows:
Theta1 = reshape(thetaVector(1:110),10,11) Theta2 = reshape(thetaVector(111:220),10,11) Theta3 = reshape(thetaVector(221:231),1,11)
- Have initial parameters .
Unroll to get initialTheta to pass to fminumc(@costFunction, initialTheta, options)
- function [jval, gradientVec] = costFunction(thetaVec)
- From thetaVec, get .
- Use forward prop/back prop to compute and .
- Unroll to get gradientVec.
- function [jval, gradientVec] = costFunction(thetaVec)
- Gradient checking will assure that our backpropagation works as intended. We can approximate the derivative of our cost function with:
- With multiple theta matrices, we can approximate the derivative with respect to as follows:
- A small value for ϵ (epsilon) such as , guarantees that the math works out properly. If the value for ϵ is too small, we can end up with numerical problems.
Hence, we are only adding or subtracting epsilon to the matrix. In octave we can do it as follows:
epsilon = 1e-4; for i = 1:n, thetaPlus = theta; thetaPlus(i) += epsilon; thetaMinus = theta; thetaMinus(i) -= epsilon; gradApprox(i) = (J(thetaPlus) - J(thetaMinus))/(2*epsilon) end;
We previously saw how to calculate the deltaVector. So once we compute our gradApprox vector, we can check that gradApprox ≈ deltaVector.
Once you have verified once that your backpropagation algorithm is correct, you don't need to compute gradApprox again. The code to compute gradApprox can be very slow.
Initializing all theta weights to zero does not work with neural networks. When we backpropagate, all nodes will update to the same value repeatedly. Instead we can randomly initialize our weights for our matrices using the following method:
Initialize each to a random value in (i.e. )
Theta1 = rand(10,11)*(2*INIT+EPSILON) - INIT_EPSILON; Theta2 = rand(1,11)*(2*INIT+EPSILON) - INIT_EPSILON;
- (Note: the used above is unrelated to the epsilon from Gradient Checking)
Hence, we initialize each to a random value between . Using the above formula guarantees that we get the desired bound. The same procedure applies to all the Θ's. Below is some working code you could use to experiment.
If the dimensions of Theta1 is 10x11, Theta2 is 10x11 and Theta3 is 1x11. Theta1 = rand(10,11) * (2 * INIT_EPSILON) - INIT_EPSILON; Theta2 = rand(10,11) * (2 * INIT_EPSILON) - INIT_EPSILON; Theta3 = rand(1,11) * (2 * INIT_EPSILON) - INIT_EPSILON;
rand(x,y) is just a function in octave that will initialize a matrix[xy] of random real numbers between 0 and *1.
choose the layout of your neural network, including how many hidden units in each layer and how many layers in total you want to have.
- Number of input units = dimension of features
- Number of output units = number of classes
- Number of hidden units per layer = usually more the better (must balance with cost of computation as it increases with more hidden units)
- Defaults: 1 hidden layer. If you have more than 1 hidden layer, then it is recommended that you have the same number of units in every hidden layer.
- Randomly initialize the weights
- Implement forward propagation to get for any
- Implement the cost function
- Implement backpropagation to compute partial derivatives
- Use gradient checking to confirm that your backpropagation works. Then disable gradient checking.
Use gradient descent or a built-in optimization function to minimize the cost function with the weights in theta.
When we perform forward and back propagation, we loop on every training example:
for i = 1:m, Perform forward propagation and backpropagation using example (x(i),y(i)) (Get activations a(l) and delta terms d(l) for l = 2,...,L
The following image gives us an intuition of what is happening as we are implementing our neural network:
- Ideally, you want . This will minimize our cost function. However, keep in mind that is not convex and thus we can end up in a local minimum instead.