1. Derivation from exponential function to sigmoid 2. Logarithm function and sigmoid 2. Sigmoid function 3. Neural network loss function derivation
1. Sigmoid function
Sigmoid function, i.e. S-shaped curve function, is as follows:
Function: F (z) = 11 + e − Z
Derivative: F ‘(z) = f (z) (1 − f (z))
The above is our common form. Although we know this form, we also know the calculation process. It’s not intuitive enough. Let’s analyze it.
1.1 from exponential function to sigmoid
First, let’s draw the basic graph of exponential function
From the figure above, we get the following information: the exponential function passes (0,1) point, monotonically increasing / decreasing, and the definition field is
, the range is
Let’s take a look at the image of the sigmoid function
If you just
If you put it on the denominator, it’s the same as
The image is the same, so add 1 to the denominator to get the image above. The domain of definition is
, the range is
Then there is a good feature, that is, no matter what
We can get the value between (0,1) for whatever it is;
1.2 logarithmic function and sigmoid
First, let’s look at the image of the logarithmic function
Logarithmic function of the image above, monotone decreasing, there is a better feature is in the
If we put the sigmoid function in front of us in the position of the independent variable, we will get the result
The image of the image;
How can we measure the difference between a result and the actual calculation? One idea is that if the result is closer, the difference will be smaller, otherwise, it will be larger. This function provides such an idea. If the calculated value is closer to 1, then it means that it is closer to the world result, otherwise, it is farther away. Therefore, this function can be used as the loss function of logistic regression classifier. If all the results are close to the result value, then The closer it is to 0. If the result is close to 0 after all the samples are calculated, it means that the calculated result is very close to the actual result.
2. Derivation of sigmoid function
The derivation process of sigmoid derivative is as follows:
3. Derivation of neural network loss function
The loss function of neural network can be understood as a multi-level composite function, and the chain rule is used for derivation.
Let’s talk about the process of conventional derivation
This is a simple composite function, as shown in the figure above. C is a function of a and E is a function of C. if we use the chain derivation rule to derive a and B respectively, then we will find out the derivative of e to C and C to a, multiply it, find out the derivative of e to C and D respectively, find out the derivative of C and D to B respectively, and then add it up, One of the problems is that in the process of solving, e calculates the derivative of C twice. If the equation is particularly complex, then the amount of calculation becomes very large. How can we only calculate the derivative once?
As shown in the figure above, we start from top to bottom, calculate the value of each cell, then calculate the partial derivative of each cell, and save it;
Next, continue to calculate the value of the sub unit, and save the partial derivatives of the sub unit; multiply all the partial derivatives of the path from the last sub unit to the root node, that is, the partial derivatives of the function to this variable. The essence of calculation is from top to bottom. When calculating, save the value and multiply it to the following unit, so that the partial derivatives of each path only need to be calculated once, from top to bottom All the partial derivatives are obtained by calculating them from top to bottom.
In fact, BP (back propagation algorithm) is calculated in this way. If there is a three-layer neural network with input layer, hidden layer and output layer, we can calculate the partial derivative of the weight of the loss function. It is a complex composite function. If we first calculate the partial derivative of the weight of the first layer, and then calculate the partial derivative of the weight of the second layer, we will find that there are some problems A lot of repeated calculation steps, like the example of simple function above, so, in order to avoid this kind of consumption, we use to find the partial derivative from the back to the front, find out the function value of each unit, find out the partial derivative of the corresponding unit, save it, multiply it all the time, and input the layer.
The following is a simple example to demonstrate the process of calculating partial derivative by back propagation
Then we will have two initial weight matrices:
We got the matrix above, and now we’re using
Function as the activation function to calculate the excitation of each layer of the network (assuming that we have only one sample, the input is
The output is
The first level is input, and the incentive is the eigenvalue of the sample
Is the bias term, which is 1
The second layer is the hidden layer. The excitation is obtained by multiplying the eigenvalue with the region, and then the sigmoid function is used to transform the region
In the above, we add a bias term at the end;
Next, the third layer is the output layer
Because it is the output layer, there is no need to calculate further, so the bias term is not added;
The above calculation process, from input to output, is also called forward propagation.
Then, we write the formula of the loss function according to the loss function. Here, there is only one input and one output, so the loss function is relatively simple
Here, M = 1;
In fact, it is the sum of squares of all the weights. Generally, the one multiplied by the offset term will not be put in. This term is very simple. Ignore it for the time being, and do not write this term for the time being (this is regularization).
Then we get the above formula, and here we know if we want to ask for it
If we use the partial derivative of, we will find that this formula is actually a composite function,
It’s a constant. A3 is a constant
Function transformation, and
Now that we have found where the weight is, we can start to find the partial derivative,
finish writing sth.
Then, we get the following derivation:
According to the above derivation, we can get the following formula:
So, remember what I said earlier, we will seek the derivative from top to bottom and save the partial derivative of the current multiple subunits. According to the above formula, we know that the partial derivative of the second weight matrix can be obtained by
It is obtained by multiplying the excitation of the previous layer of network and dividing it by the number of samples, so sometimes we call the difference as
Then, the partial derivatives of the second weight matrix are obtained by multiplying them in the form of matrix;
Now that we have obtained the partial derivatives of the second weight matrix, how can we find the partial derivatives of the first weight matrix?
For example, we’re going to
From the formula on the line, we can see that the derivative we saved can be directly multiplied. If there is a multi-layer network, in fact, the following process is the same as this one, so we get the formula:
if there is a multi-layer network, the following process is the same as this one
Because this network is three layers, so we can get all the partial derivatives. If it is multi-layer, the principle is the same. Multiply it continuously. Starting from the second formula, the following forms are the same.