Backpropagation
local graph
Chapter 5 — Backpropagation
Section titled “Chapter 5 — Backpropagation”The previous chapter described neural network training. There, the gradient of a weight parameter in the neural network (i.e., the gradient of the loss function for a weight parameter) was obtained by using numerical differentiation. Numerical differentiation is simple, and its implementation is easy, but it has the disadvantage that calculation takes time. This chapter covers backpropagation, which is a more efficient way to calculate the gradients of weight parameters.
There are two ways to understand backpropagation correctly. One of them uses “equations,” while the other uses computational graphs. The former is a common way, and many books about machine learning expand on this by focusing on formulas. This is good because it is strict and simple, but it may hide essential details or end in a meaningless list of equations.
Therefore, this chapter will use computational graphs so that you can understand backpropagation “visually.” Writing code will deepen your understanding further and convince you of this. The idea of using computational graphs to explain backpropagation is based on Andrej Karpathy’s blog (Hacker’s guide to Neural Networks, (http://karpathy.github.io/neuralnets/)) and the deep learning course (CS231n: Convolutional Neural Networks for Visual Recognition (http://cs231n.github.io/)) provided by him and Professor Fei-Fei Li at Stanford University.
Computational Graphs
Section titled “Computational Graphs”A computational graph shows the process of calculation. This graph is used as a graph of data structure and is represented by multiple nodes and edges (meaning, straight lines that connect nodes). In this section, we will solve easy problems to familiarize ourselves with computational graphs before advancing step by step into more complex backpropagation.
Using Computational Graphs to Solve Problems
Section titled “Using Computational Graphs to Solve Problems”The problems in this section are simple enough that you can solve them with mental arithmetic, but the purpose here is to get familiar with computational graphs. Learning to use computational graphs will be helpful for the complicated calculations we will cover later, so it’s important to first master how to use them here.
Question 1: Taro bought 2 apples that were 100 yen apiece. Calculate the amount of money he paid if a 10% consumption tax was applied.
A computational graph shows the process of calculation with nodes and arrows. A node is represented by a circle, and an operation is described in it. The intermediate result of the calculation above an arrow shows the result of each node that flows from left to right. The following diagram shows the computational graph that solves Question 1:

Figure 5.1: Answer to Question 1 using a computational graph
Section titled “Figure 5.1: Answer to Question 1 using a computational graph”As shown in the preceding diagram, 100 yen for an apple that flows to the “x2” node becomes 200 yen, which is passed to the next node. Then, 200 yen is passed to the ”× 1.1” node and becomes 220 yen. The result of this computational graph shows that the answer is 220 yen.
In the preceding diagram, each circle contains ”× 2” or ”× 1.1” as one operation. You can also place only “x” in a circle to show the operation. In that case, as shown in the following diagram, you can place “2” and “1.1” outside the circles as the “Number of apples” and “Consumption tax” variables.
The solution to this problem can be observed in the figure below:

Figure 5.2: Answer to Question 1 using a computational graph: the “Number of apples” and “Consumption tax” variables are placed outside circles
Section titled “Figure 5.2: Answer to Question 1 using a computational graph: the “Number of apples” and “Consumption tax” variables are placed outside circles”Question 2: Taro bought 2 apples and 3 oranges. An apple was 100 yen, and the orange was 150. A 10% consumption tax was applied. Calculate the amount of money he paid.
As in Question 1, we will use a computational graph to solve Question 2. The following diagram shows the computational graph for this:

Figure 5.3: Answer to Question 2 using a computational graph
Section titled “Figure 5.3: Answer to Question 2 using a computational graph”In this question, an addition node, ”+”, was added to sum the amounts of apples and oranges. After creating a computational graph, we advance the calculation from left to right. The calculation result moves from left to right, just like an electric current flows in a circuit, and the calculation ends when the result reaches the rightmost side. The preceding diagram shows that the answer is 715 yen.
To solve a problem using a computational graph, then, you must perform the following:
- Create a computational graph.
- Advance the calculation from left to right on the computational graph.
Step 2 is known as propagating in the forward direction or forward propagation. In forward propagation, calculation propagates from start to finish in a computational graph. If forward propagation exists, we can also consider propagation in the backward direction—from right to left. This is called backward propagation and is known as backpropagation. It will play an important role when we calculate derivatives later.
Local Calculation
Section titled “Local Calculation”The main characteristic of a computational graph is that it can obtain the final result by propagating “local calculation.” The word “local” means “a small range related to the node.” A local calculation can return the next result (subsequent result) from information related to the node, no matter what is happening on the whole.
We can break down local calculations using a specific example. For example, let’s assume that we bought two apples and many other things at a supermarket. To visualize this, you can create a computational graph like so:

Figure 5.4: Example of buying two apples and many other things
Section titled “Figure 5.4: Example of buying two apples and many other things”Let’s assume that we bought many things and that the total amount was 4,000 yen (after a complicated calculation), as shown in the preceding computational graph. What is important here is that the calculation in each node is a local calculation. To sum the amounts of the apples and the other purchases (4,000 + 200 -> 4,200), you can add the two figures without thinking about how 4,000 was obtained. In other words, what to calculate in each node is only the calculation related to the node—in this example, the addition of the two numbers provided. We do not need to think about the whole graph.
Thus, you can focus on local calculation in a computational graph. However complicated the whole calculation is, what is done at each step is “local calculation” for the target node. By passing the results of simple local calculations, you can obtain the result of the complicated calculations that constitute the whole graph.
For example, the assembly of a car is complicated, but it is usually conducted based on the division of labor on an “assembly line.” Each worker (machine) conducts simple work. The outcome of the workflows to the next worker, and finally, a car is built. A computational graph also divides complicated calculations into “simple and local calculations” and passes the calculation result to the next node, just like a car is passed down an assembly line. Like the assembly of a car, complicated calculations can be divided into simple calculations.
Why Do We Use Computational Graphs?
Section titled “Why Do We Use Computational Graphs?”We have solved two problems by using computational graphs and may now consider the advantages. One of them is “local calculation,” as described earlier. However complicated the whole calculation is, local calculation enables you to focus on the simple calculations in each node in order to simplify the problem as a whole.
Another advantage is that you can keep all the results of intermediate calculations in a computational graph (for example, 200 yen after 2 apples are calculated and 650 yen before consumption tax is added). However, the largest reason for using computational graphs is that you can calculate “derivatives” efficiently by propagating in the backward direction.
To describe backward propagation in a computational graph, consider Question 1 again. In this problem, you calculated the final amount paid regarding two apples and the consumption tax. Now say that you need to know how the final amount paid will be affected when the price of an apple goes up. This corresponds to obtaining the “derivative of the amount paid with respect to the price of an apple.” It corresponds to obtaining
when the price of an apple is x and the amount paid is L. The value of this derivative indicates how much the amount paid increases when the price of an apple goes up “slightly.”
As we mentioned earlier, you can use backward propagation in a computational graph to obtain a value, such as the “derivative of the amount paid with respect to the price of an apple.” First, we will only look at the result. As shown in the following diagram, you can obtain derivatives by using backpropagation in a computational graph:

Figure 5.5: Propagating differential values using backward propagation
Section titled “Figure 5.5: Propagating differential values using backward propagation”As shown in the preceding diagram, backward propagation is shown graphically with arrows (thick lines) in the opposite direction to the forward direction. Backward propagation passes “local differentials,” and the values are placed below the arrows. In this example, derivative values are passed from right to left, as in 1 -> 1.1 -> 2.2. The result shows that the value of the “derivative of the amount paid with respect to the price of an apple” is 2.2.
This indicates that the final amount paid increases by 2.2 yen when the price of an apple goes up 1 yen. This means that when the price of an apple goes up by a small amount, the final amount increases by 2.2 times that of the small value. Here, only the derivative with respect to the price of an apple was obtained, but you can also obtain the “derivative of the amount paid with respect to consumption tax” and the “derivative of the amount paid with respect to the number of apples” by using similar steps.
During these steps, you can share the intermediate results of the derivatives (the derivatives that are passed halfway) so that you can calculate multiple derivatives efficiently. Thus, the advantage of a computational graph is that forward, and backward propagations enable you to obtain the derivative value of each variable efficiently.
Chain Rule
Section titled “Chain Rule”Forward propagation in a computational graph propagates the calculation result in the forward direction from left to right. These calculations seem natural because they are usually conducted. On the other hand, in backward propagation, a “local derivative” is propagated in the backward direction from right to left. The principle that propagates the “local derivative” is based on the chain rule. Let’s look at the chain rule and clarify how it corresponds to backward propagation in a computational graph.
Backward Propagation in a Computational Graph
Section titled “Backward Propagation in a Computational Graph”We will now look at an example of backward propagation using a computational graph. Let’s assume that a calculation, y = f (x), exists. The following diagram shows the backward propagation of this calculation:

Figure 5.6: Backward propagation in a computational graph – the local derivative is multiplied in the backward direction
Section titled “Figure 5.6: Backward propagation in a computational graph – the local derivative is multiplied in the backward direction”As shown in the preceding diagram, backward propagation multiplies the signal E by the local derivative of the node,
, and propagates it to the next node. The local derivative here means obtaining the derivative of the calculation, y = f (x), in forward propagation and indicates obtaining the derivative, y, with respect to x
; for example, y = f (x) = x2,
. The local derivative is multiplied by the value propagated from the upper stream (E, in this example) and passed to the previous node.
This is the procedure of backward propagation. It can obtain the target derivative values efficiently. The reason why this is possible can be explained by the principle of the chain rule, defined in the next section.
What Is the Chain Rule?
Section titled “What Is the Chain Rule?”Before explaining the chain rule, we need to talk about composite functions. A composite function is a function that consists of multiple functions. For example, the equation z = (x + y)2 consists of two equations, as shown in equation (5.1):
![]() | (5.1) |
|---|
The chain rule is the characteristic related to the derivative of a composite function and is defined as follows.
When a function is expressed by a composite function, the derivative of the composite function can be expressed by the product of the derivative of each function that constitutes the composite function.
This is called the principle of the chain rule. Although it may seem difficult, it is actually quite simple. In the example given in equation (5.1),
(a derivative of z with respect to x) is the product of
(a derivative of z with respect to t) and
(a derivative of t with respect to x). You can express this with the following equation (5.2):
![]() | (5.2) |
|---|
You can remember equation (5.2) easily because ∂t’s cancel each other out, as shown here: 
Now, let’s use the chain rule to obtain the derivative of equation (5.2),
. First, obtain the local differential (partial differential) of equation (5.1):
![]() | (5.3) |
|---|
As shown in equation (5.3),
is 2t and
is 1. This result is analytically obtained from the differentiation formula. The final result,
, can be calculated by the product of the derivatives obtained in equation (5.3):
![]() | (5.4) |
|---|
The Chain Rule and Computational Graphs
Section titled “The Chain Rule and Computational Graphs”Now, let’s use a computational graph to express the calculation of the chain rule in equation (5.4). When we represent a square with a node “**2”, we can write a graph for it, as follows:

Figure 5.7: Computational graph of equation (5.4) – local derivatives are multiplied and passed in the backward direction
Section titled “Figure 5.7: Computational graph of equation (5.4) – local derivatives are multiplied and passed in the backward direction”As shown in the preceding diagram, backward propagation in a computational graph propagates signals from right to left. Backward propagation multiplies the signal provided to a node by the local derivative (partial derivative) of the node and passes it to the next node. For example, the input to “**2” in backward propagation is
. It is multiplied by the local derivative,
(in forward propagation, the input is t and the output is z, so the (local) derivative at this node is
) and then multiplied and passed to the next node. In the preceding diagram, the first signal in backward propagation
did not appear in the previous equation. It was omitted there because
= 1.
What we should note from the preceding diagram is the result of backward propagation at the leftmost position. It corresponds to the “derivative of z with respect to x” because
due to the chain rule. What backward propagation performs is based on the principle of the chain rule.
When you assign the result of equation (5.3), as shown in the preceding diagram, the result is as follows. Thus,
is 2(x + y):

Figure 5.8: Based on the result of backward propagation in the computational graph, is 2(x + y)
Section titled “Figure 5.8: Based on the result of backward propagation in the computational graph, is 2(x + y)” Bookmark
Backward Propagation
Section titled “Backward Propagation”The previous section described how backward propagation in a computational graph is based on the chain rule. We will now cover how backward propagation works by taking operations, such as ”+” and “x”, as examples.
Backward Propagation in an Addition Node
Section titled “Backward Propagation in an Addition Node”First, let’s consider backward propagation in an additional node. Here, we will look at backward propagation for the equation z = x + y. We can obtain the derivatives of z = x + y (analytically) as follows:
![]() | (5.5) |
|---|
As equation (5.5) shows, both
and
are 1. Therefore, we can represent them in a computational graph, as shown in the following diagram. In backward propagation, the derivative from the upper stream—
, in this example—is multiplied by 1 and passed downstream. In short, backward propagation in an addition node multiplies 1, so it only passes the input value to the next node.
In this example, the differential value from the upper stream is expressed as
. This is because we assume a large computational graph that finally outputs L, as shown in Figure 5.10. The calculation, z = x + y, exists somewhere in the large computational graph, and the value of
is passed from the upper stream. The values of
and
are propagated downstream:

Figure 5.9: Backward propagation in an addition node – forward propagation on the left and backward propagation on the right.
Section titled “Figure 5.9: Backward propagation in an addition node – forward propagation on the left and backward propagation on the right.”As shown on the right, backward propagation in an addition node passes a value from the upper stream to the lower stream without changing it.

Figure 5.10: This addition node exists somewhere in the final output calculation.
Section titled “Figure 5.10: This addition node exists somewhere in the final output calculation.”In backward propagation, starting from the rightmost output, local derivatives are propagated from node to node in the backward direction
Now, let’s look at an example of backward propagation. For example, say that a calculation, “10 + 5 = 15”, exists and that a value of 1.3 flows from the upper stream in backward propagation. The following diagram shows this in terms of a computational graph:

Figure 5.11: Example of backward propagation in an addition node
Section titled “Figure 5.11: Example of backward propagation in an addition node”Because backward propagation in an addition node only outputs the input signal to the next node, it passes 1.3 to the next node.
Backward Propagation in a Multiplication Node
Section titled “Backward Propagation in a Multiplication Node”Let’s take a look at backward propagation in a multiplication node by taking an equation, z = xy, as an example. The differential of this equation is expressed by the following equation (5.6):
![]() | (5.6) |
|---|
Based on the preceding equation (5.6), you can write a computational graph as follows.
For the backward propagation of multiplication, the upstream value multiplied by the “reversed value” of the input signal for forward propagation is passed downstream. A reversed value means that if the signal is x in forward propagation, the value to multiply is y in backward propagation; and that if the signal is y in forward propagation, the value to multiply is x in backward propagation, as shown in the following diagram.
Let’s look at an example. Assume that a calculation 10 x 5 = 50 exists and that the value of 1.3 flows from the upper stream in backward propagation. Figure 5.13 shows this in the form of a computational graph.
In the backward propagation of multiplication, the reversed input signals are multiplied, so 1.3 x 5 = 6.5 and 1.3 x 10 = 13 are obtained. In the backward propagation of addition, the upstream value was only passed downstream. Therefore, the value of the input signal in forward propagation is not required. On the other hand, for the backward propagation of multiplication, the value of the input signal in forward propagation is required. Therefore, to implement a multiplication node, the input signal of forward propagation is retained:

Figure 5.12: Backward propagation in a multiplication node – forward propagation on the left and backward propagation on the right
Section titled “Figure 5.12: Backward propagation in a multiplication node – forward propagation on the left and backward propagation on the right”
Figure 5.13: Example of backward propagation in a multiplication node
Section titled “Figure 5.13: Example of backward propagation in a multiplication node”Apples Example
Section titled “Apples Example”Let’s think about the example of buying apples—two apples and consumption tax—from the beginning of this chapter again. The problem to solve here is how each of the three variables (the price of an apple, the number of apples, and consumption tax) affect the final amount paid. This corresponds to obtaining the “derivative of the amount paid with respect to the price of an apple,” the “derivative of the amount paid with respect to the number of apples,” and the “derivative of the amount paid with respect to consumption tax.” We can solve this by using backward propagation in a computational graph, as shown in the following diagram:

Figure 5.14: Example of backward propagation for purchasing apples
Section titled “Figure 5.14: Example of backward propagation for purchasing apples”As mentioned previously, input signals are reversed and passed downstream in the backward propagation of a multiplication node. According to the result shown in the preceding diagram, the differential of the price of an apple is 2.2, there are 110 apples, and the consumption tax is 200. They indicate that when consumption tax and the price of an apple increase by the same quantity, the consumption tax affects the final amount paid in the size of 200 and that the price of an apple affects it in the size of 2.2. However, this result is brought about because the consumption tax and the price of an apple in this example are different in terms of units (1 for the consumption tax is 100%, while 1 for the price of an apple is 1 yen).
Lastly, let’s solve the backward propagation of “buying apples and oranges” as an exercise. Please obtain the derivatives of individual variables and put the numbers in the squares provided in the following diagram (you can find the answer in the next section):

Figure 5.15: Example of backward propagation for purchasing apples and oranges – complete this calculation by putting figures in the squares
Section titled “Figure 5.15: Example of backward propagation for purchasing apples and oranges – complete this calculation by putting figures in the squares” Bookmark
Implementing a Simple Layer
Section titled “Implementing a Simple Layer”In this section, we will implement the apple example we’ve described in Python using the multiplication node in a computational graph as the multiplication layer (MulLayer) and the addition node as the addition layer (AddLayer).
In the next section, we will implement the “layers” that constitute a neural network in one class. The “layer” here is a functional unit in a neural network—the Sigmoid layer for a sigmoid function, and the Affine layer for matrix multiplication. Therefore, we will also implement multiplication and addition nodes here on a “layer” basis.
Implementing a Multiplication Layer
Section titled “Implementing a Multiplication Layer”We will implement a layer so that it has two common methods (interfaces): forward() and backward(), which correspond to forward propagation and backward propagation, respectively. Now, you can implement a multiplication layer as a class called MulLayer, as follows (the source code is located at ch05/layer_naive.py):
class MulLayer: def __init__ (self): self.x = None self.y = None def forward(self, x, y): self.x = x self.y =y out = x * y return out def backward(self, dout): dx = dout * self.y # Reverse x and y dy = dout * self.x return dx, dy__init__() initializes the instance variables, x, and y, which are used to retain the input values in forward propagation. forward() takes two variables, x and y, and multiplies and outputs their product. On the other hand, backward() multiplies the derivative from the upper stream (dout) by the “reversed value” of forward propagation and passes the result downstream.
Now, use MulLayer to implement the “purchase of apples”—two apples and consumption tax. In the previous section, we used forward and backward propagations in a computational graph for this calculation, as shown in the following diagram:

Figure 5.16: Purchasing two apples
Section titled “Figure 5.16: Purchasing two apples”By using the multiplication layer, we can implement forward propagation for this as follows (the source code is located at ch05/buy_apple.py):
apple = 100apple_num = 2tax = 1.1# layermul_apple_layer = MulLayer()mul_tax_layer = MulLayer()# forwardapple_price = mul_apple_layer.forward(apple, apple_num)price = mul_tax_layer.forward(apple_price, tax)print(price) # 220You can use backward() to obtain the differential of each variable.
# backward
dprice = 1dapple_price, dtax = mul_tax_layer.backward(dprice)dapple, dapple_num = mul_apple_layer.backward(dapple_price)print(dapple, dapple_num, dtax) # 2.2 110 200Here, the order of calling backward() is the opposite of that of calling forward(). Note that the argument of backward() is the “derivative with respect to the output variable in forward propagation.” For example, the multiplication layer, mul_apple_layer, returns apple_price in forward propagation, while it takes the derivative value of apple_price (dapple_price) as an argument in backward propagation. The execution result of this program matches the result shown in the preceding diagram.
Implementing an Addition Layer
Section titled “Implementing an Addition Layer”Now, we will implement an addition layer, which is an addition node, as follows:
class AddLayer: def __init__ (self): pass def forward(self, x, y): out = x + y return out def backward(self, dout): dx = dout * 1 dy = dout * 1 return dx, dyAn addition layer requires no initialization, so __init__ () does nothing (the pass statement is “does nothing”). forward() in the addition layer takes two arguments, x and y, and adds them for output. backward() passes the differential (dout) from the upper stream to the lower stream.
Now, let’s use the addition and multiplication layers to implement the purchase of two apples and three oranges, as shown in the following diagram.

Figure 5.17: Purchasing two apples and three oranges
Section titled “Figure 5.17: Purchasing two apples and three oranges”You can implement this computational graph in Python as follows (the source code is located at ch05/buy_apple_orange.py):
apple = 100apple_num = 2orange = 150orange_num = 3tax = 1.1# layermul_apple_layer = MulLayer()mul_orange_layer = MulLayer()add_apple_orange_layer = AddLayer()mul_tax_layer = MulLayer()# forwardapple_price = mul_apple_layer.forward(apple, apple_num) #(1)orange_price = mul_orange_layer.forward(orange, orange_num) #(2)all_price = add_apple_orange_layer.forward(apple_price, orange_price) #(3)price = mul_tax_layer.forward(all_price, tax) #(4)# backwarddprice = 1dall_price, dtax = mul_tax_layer.backward(dprice) #(4)dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price) #(3)dorange, dorange_num = mul_orange_layer.backward(dorange_price) #(2)dapple, dapple_num = mul_apple_layer.backward(dapple_price) #(1)print(price) # 715print(dapple_num, dapple, dorange, dorange_num, dtax) # 110 2.2 3.3 165 650This implementation is a little long, but each statement is simple. The required layers are created and the forward propagation method, forward(), is called in an appropriate order. Then, the backward propagation method, backward(), is called in the opposite order to forward propagation to obtain the desired derivatives.
In this way, implementing layers (here, the addition and multiplication layers) in a computational graph are easy, and you can use them to obtain complicated derivatives. Next, we will implement the layers that are used in a neural network.
Bookmark
Implementing the Activation Function Layer
Section titled “Implementing the Activation Function Layer”Now, we will apply the idea of a computational graph to a neural network. Here, we will implement the “layers” that constitute a neural network in one class using the ReLU and Sigmoid layers, which are activation functions.
ReLU Layer
Section titled “ReLU Layer”A Rectified Linear Unit (ReLU) is used as an activation function and is expressed by the following equation (5.7):
![]() | (5.7) |
|---|
From the preceding equation (5.7), you can obtain the derivative of y with respect to x with equation (5.8):
![]() | (5.8) |
|---|
As equation (5.8) shows, if the input in forward propagation, x, is larger than 0, backward propagation passes the upstream value downstream without changing it. Meanwhile, if x is 0 or smaller in forward propagation, the signal stops there in backward propagation. You can express this in a computational graph, as shown in the following diagram:

Figure 5.18: Computational graph of the ReLU layer
Section titled “Figure 5.18: Computational graph of the ReLU layer”Next, let’s implement the ReLU layer. When implementing a layer in a neural network, we assume that forward() and backward() take NumPy arrays as arguments. The implementation of the ReLU layer is located at common/layers.py:
class Relu: def __init__ (self): self.mask = None def forward(self, x): self.mask = (x <= 0) out = x.copy() out[self.mask] = 0 return out def backward(self, dout): dout[self.mask] = 0 dx = dout return dxThe Relu class has an instance variable, mask. The mask variable is a NumPy array that consists of True/False values. If an element of the input, x, in forward propagation is 0 or smaller, the mask’s corresponding element is True. Otherwise (if it is larger than 0), the element is False. For example, the mask variable contains a NumPy array that consists of True and False, as shown in the following code:
>>> x = np.array( [[1.0, -0.5], [-2.0, 3.0]] )
print(x) [[ 1. -0.5] [-2. 3. ]] mask = (x <= 0) print(mask) [[False True] [ True False]]
As shown in the preceding diagram, the value of backward propagation is 0 when the input value in forward propagation is 0 or smaller. Therefore, in backward propagation, the mask variable stored in forward propagation is used to set dout from the upper stream. If an element of the mask is True, the corresponding element in dout is set to 0.
The ReLU layer works as a “switch” in a circuit. In forward propagation, it turns on the switch if an electric current flows through it, and turns off the switch if an electric current does not flow through it. In backward propagation, the electric current keeps on flowing if the switch is ON and does not flow any longer if the switch is OFF.
Sigmoid Layer
Section titled “Sigmoid Layer”Next, let’s implement a sigmoid function. This is expressed by equation (5.9):
![]() | (5.9) |
|---|
The following diagram shows the computational graph that represents equation (5.9):

Figure 5.19: Computational graph of the Sigmoid layer (forward propagation only)
Section titled “Figure 5.19: Computational graph of the Sigmoid layer (forward propagation only)”Here, the exp and / nodes appear in addition to the X and + nodes. The exp node calculates y = exp(x), while the / node calculates
.
The calculation of equation (5.9) consists of the propagation of local calculations. Next, let’s consider the backward propagation shown in the preceding computational graph, looking at the flow of backward propagation step by step to summarize what we have described so far.
Step 1:
The / node represents
. Its derivative is analytically expressed by the following equation:
 | (5.10) |
|---|
Based on equation (5.10), in backward propagation, the node multiplies the upstream value by −y2 (the additive inverse of the square of the output in forward propagation) and passes the value to the lower stream. The following computational graph shows this:

Figure 5.20: Computational graph of the Sigmoid layer (with the additive inverse of the square)
Section titled “Figure 5.20: Computational graph of the Sigmoid layer (with the additive inverse of the square)”Step 2:
The + node only passes the upstream value to the lower stream. The following computational graph shows this:

Figure 5.21: Computational graph of the Sigmoid layer (with passing upstream value)
Section titled “Figure 5.21: Computational graph of the Sigmoid layer (with passing upstream value)”Step 3:
The “exp” node represents y = exp(x), and its derivative is expressed by the following equation:
![]() | (5.11) |
|---|
In the following computational graph, the node multiplies the upstream value by the output in forward propagation (exp(-x), in this example) and passes the value to the lower stream:

Figure 5.22: Computational graph of the Sigmoid layer
Section titled “Figure 5.22: Computational graph of the Sigmoid layer”Step 4:
The X node reverses the values in forward propagation for multiplication. Therefore, −1 is multiplied here:

Figure 5.23: Computational graph of the Sigmoid layer (reversed values)
Section titled “Figure 5.23: Computational graph of the Sigmoid layer (reversed values)”Thus, we can show the backward propagation of the Sigmoid layer in the computational graph shown in the preceding diagram. According to the result of the preceding computational graph, the output of backward propagation is
and it is propagated to the downstream nodes. Note here that
can be calculated from the input, x, and output, y, of forward propagation. Therefore, we can draw the computational graph shown in the preceding diagram as a grouped “sigmoid” node, as follows:

Figure 5.24: Computational graph of the Sigmoid layer (simple version)
Section titled “Figure 5.24: Computational graph of the Sigmoid layer (simple version)”The computational graph in Figure 5.23 and the simplified computational graph in Figure 5.24 provide the same calculation result. The simple version is more efficient because it can omit the intermediate calculation in backward propagation. It is also important to note that you can only concentrate on the input and output, without caring about the details of the Sigmoid layer, by grouping the nodes.
You can also organize
as follows:
![]() | (5.12) |
|---|
Therefore, you can only calculate the backward propagation in the Sigmoid layer shown in the preceding diagram from the output of forward propagation:

Figure 5.25: Computational graph of the Sigmoid layer – you can use the output, y, of forward propagation to calculate the backward propagation
Section titled “Figure 5.25: Computational graph of the Sigmoid layer – you can use the output, y, of forward propagation to calculate the backward propagation”Now, let’s implement the Sigmoid layer in Python. Based on the preceding diagram, you can implement it as follows (this implementation is located at common/layers.py):
class Sigmoid: def __init__ (self): self.out = None def forward(self, x): out = 1 / (1 + np.exp(-x)) self.out = out return outdef backward(self, dout): dx = dout * (1.0 - self.out) * self.out return dxThis implementation retains the output of forward propagation in the out instance variable, then uses the out variable for calculation purposes in backward propagation.
Implementing the Affine and Softmax Layers
Section titled “Implementing the Affine and Softmax Layers”Affine Layer
Section titled “Affine Layer”In forward propagation in a neural network, the product of matrices (np.dot(), in NumPy) was used to sum the weighted signals (for details, refer to the Calculating Multidimensional Arrays section in Chapter 3, Neural Networks). For example, do you remember the following implementation in Python?
>>> X = np.random.rand(2) # Input values
W = np.random.rand(2,3) # Weights B = np.random.rand(3) # Biases
X.shape # (2,) W.shape # (2, 3) B.shape # (3,)
Y = np.dot(X, W) + B
Here, assume that X, W, and B are multidimensional arrays of the shape (2,), (2, 3), and (3,), respectively. With this, you can calculate the weighted sum of neurons as Y = np.dot(X, W) + B. Y is converted by the activation function and propagated to the next layer, which is the flow of forward propagation in a neural network. Note that the number of elements in corresponding dimensions must be the same for matrix multiplication. This means that in the product of X and W, the number of elements in corresponding dimensions must be the same, as shown in the following image. Here, the shape of a matrix is represented in parentheses as (2, 3) (this is for consistency with the output of NumPy’s shape):

Figure 5.26: The number of elements in corresponding dimensions must be the same for matrix multiplication
Section titled “Figure 5.26: The number of elements in corresponding dimensions must be the same for matrix multiplication”The product of matrices in forward propagation in a neural network is called an “affine transformation” in the field of geometry. Therefore, we will implement the process that performs an affine transformation as an “Affine layer.”
Now, let’s take a look at the calculation—the product of matrices and the sum of biases—in a computational graph. When we represent the node that calculates the product of matrices as “dot,” the following computational graph can show the calculation np.dot(X, W) + B. Above each variable, the shape of the variable is indicated (for example, the shape of X is (2,) and that of X – W is (3,) are shown here):

Figure 5.27: Computational graph of the Affine layer. Note that the variables are matrices. Above each variable, the shape of the variable is shown
Section titled “Figure 5.27: Computational graph of the Affine layer. Note that the variables are matrices. Above each variable, the shape of the variable is shown”The preceding is a relatively simple computational graph. However, note that X, W, and B are multidimensional arrays. In the computational graphs we’ve looked at so far, “scalar values” flow between nodes, while in this example, “multidimensional arrays” propagate between nodes.
Let’s think about the backward propagation of the preceding computational graph. To obtain the backward propagation for multidimensional arrays, you can use the same procedure as the previous computational graphs used for scalar values by writing each element of the multidimensional arrays. Doing this, we can obtain the following equation (how we can obtain equation (5.13) is omitted here):
![]() | (5.13) |
|---|
In equation (5.13), T in WT indicates transpose. Transpose switches the (i, j) elements of W to the (j, i) elements, shown in the following equation:
![]() | (5.14) |
|---|
As shown in equation (5.14), when the shape of W is (2, 3), the shape of WT becomes (3, 2).
Based on equation (5.13), let’s write backward propagation in the computational graph. The following diagram shows the result:

Figure 5.28: Backward propagation of the Affine layer. Note that the variables are matrices. Below each variable, the shape of the variable is shown
Section titled “Figure 5.28: Backward propagation of the Affine layer. Note that the variables are matrices. Below each variable, the shape of the variable is shown”Let’s consider the shape of each variable carefully. Please note that X and
are the same shape, and that W and
are the same in terms of shape because of the following equation:
![]() | (5.15) |
|---|
We pay attention to the shapes of matrices because the number of elements in the corresponding dimensions must be the same for matrix multiplication, and checking that they’re the same can lead to equation (5.13). For example, consider the product of
and W so that the shape of
becomes (2,) when the shape of
is (3,) and the shape of W is (2,3). Then, equation (5.13) follows. This can be seen in the following diagram:

Figure 5.29: Product of matrices (you can create backward propagation of the “dot” node by configuring a product so that the number of elements in the corresponding dimensions is the same in the matrices)
Section titled “Figure 5.29: Product of matrices (you can create backward propagation of the “dot” node by configuring a product so that the number of elements in the corresponding dimensions is the same in the matrices)”Batch-Based Affine Layer
Section titled “Batch-Based Affine Layer”The Affine layer takes one piece of data as input, X. In this section, we will consider a batch-based Affine layer, which propagates N pieces of data collectively (a group of data is called a “batch”). Let’s start by looking at the computational graph of the batch-based Affine layer (Figure 5.30).
The only difference from the previous explanation is that the shape of the input, X, is now (N, 2). All we have to do is to calculate the matrices in the computational graph in the same way as we did previously. For backward propagation, we must be careful regarding the shapes of the matrices. Only after that can we obtain
and
in the same way.
You must be careful when adding bias. When adding biases in forward propagation, a bias is added to each piece of data for X · W. For example, when N = 2 (two pieces of data), biases are added to each of the two pieces of data (to each calculation result). The following diagram shows a specific example of this:

Figure 5.30: Computational graph of the batch-based Affine layer
Section titled “Figure 5.30: Computational graph of the batch-based Affine layer”>>> X_dot_W = np.array([[0, 0, 0], [10, 10, 10]])
B = np.array([1, 2, 3])
X_dot_W
array([[ 0, 0, 0], [ 10, 10, 10]])X_dot_W + B
array([[ 1, 2, 3], [11, 12, 13]])In forward propagation, the biases are added to each piece of data (the first, the second, and so on). Therefore, in backward propagation, the values of each piece of data in backward propagation must be integrated into the elements of biases. The following code shows this:
>>> dY = np.array([[1, 2, 3,], [4, 5, 6]])
dY
array([[1, 2, 3],[4, 5, 6]])dB = np.sum(dY, axis=0) dB array([5, 7, 9])
In this example, we’re assuming that there are two pieces of data (N = 2). In backward propagation of biases, the derivatives with respect to the two pieces of data are summed for each piece of data. To do that, np.sum() sums the elements of axis 0.
Thus, the implementation of the Affine layer is as follows. The Affine implementation in common/layers.py is slightly different from the implementation described here because it considers the case when input data is a tensor (four-dimensional array):
class Affine: def __init__ (self, W, b): self.W = W self.b = b self.x = None self.dW = None self.db = None def forward(self, x): self.x = x out = np.dot(x, self.W) + self.b return out def backward(self, dout): dx = np.dot(dout, self.W.T) self.dW = np.dot(self.x.T, dout) self.db = np.sum(dout, axis=0) return dxSoftmax-with-Loss Layer
Section titled “Softmax-with-Loss Layer”Finally, we should consider a softmax function, which is the output layer. The softmax function normalizes entered values and outputs them (as described earlier). For example, the following diagram shows the output of the Softmax layer for handwritten digit recognition.
As we can see, the Softmax layer normalizes the entered values (meaning it converts them so that the total of the output values is 1) and outputs them. The Softmax layer has 10 inputs because handwritten digit recognition classifies data into 10 classes.
Neural network processing consists of two phases: inference and training. Usually, inference in a neural network does not use the Softmax layer. For example, for inference in the network shown in the following diagram, the output of the final Affine layer is used as the inference result. The unnormalized output result from a neural network (the output of the Affine layer before the Softmax layer in the following diagram) is sometimes called a “score.” To obtain only one answer in neural network inference, you only need to calculate the maximum score, so you do not need a Softmax layer. However, you do need a Softmax layer in neural network training.

Figure 5.31: The images are converted by the Affine and ReLU layers and 10 input values are normalized by the Softmax layer
Section titled “Figure 5.31: The images are converted by the Affine and ReLU layers and 10 input values are normalized by the Softmax layer”In this example, the score for “0” is 5.3, which is converted into 0.008 (0.8%) by the Softmax layer. The score for “2” is 10.1, which is converted into 0.991 (99.1%).
Now, we will implement the “Softmax-with-Loss layer,” which also contains a cross-entropy error, which is a loss function. The following diagram shows the computational graph of the Softmax-with-Loss layer (the softmax function and cross-entropy error):

Figure 5.32: Computational graph of the Softmax-with-Loss layer
Section titled “Figure 5.32: Computational graph of the Softmax-with-Loss layer”As you can see, the Softmax-with-Loss layer is slightly complicated. Only the result is shown here. If you are interested in how the Softmax-with-Loss layer is created, refer to the Computational Graph of the Softmax-with-Loss Layer section in the Appendix.
We can simplify the computational graph shown in the preceding diagram as follows:

Figure 5.33: “Simplified” computational graph of the Softmax-with-Loss layer
Section titled “Figure 5.33: “Simplified” computational graph of the Softmax-with-Loss layer”In the preceding computational graph, the Softmax layer indicates the Softmax function, while the Cross-Entropy Error layer indicates the cross-entropy error. Here, we’re assuming that the data, which is classified into three classes and three inputs (scores), is received from the previous layer. As we can see, the Softmax layer normalizes the input (a1, a2, a3) and outputs (y1, y2, and y3). The Cross-Entropy Error layer receives the output from Softmax, (y1, y2, y3), and the label, (t1, t2, t3), and outputs the loss, L, based on this data.
Backward propagation from the Softmax layer returns (y1 t1, y2 t2, y3 t3), which is a “clean” result. Because (y1, y2, y3) is the output of the Softmax layer and (t1, t2, t3) is the label, (y1 − t1, y2 − t2, y3 − t3) is the difference between the output of the Softmax layer and the label. When backward propagating a neural network, an error, which is the difference, is passed to the previous layer. This characteristic is important when training a neural network.
Note that the purpose of neural network training is to adjust weight parameters so that the neural network’s output (output of Softmax) approaches the label. To do that, we need to pass the error between the neural network’s output and the label to the previous layer in an efficient manner. The previous result, (y1 − t1, y2 − t2, y3 − t3), is exactly the difference between the output of the Softmax layer and the label, and clearly shows the current error between the neural network’s output and the label.
When we use a “cross-entropy error” as the loss function for the “softmax function,” backward propagation returns a “pretty” result, (y1 − t1, y2 − t2, y3 − t3). This “pretty” result is not accidental. A function that calls a cross-entropy error is designed to do this. In a regression problem, an “identity function” is used for the output layer, and the “sum of squared errors” is used as the loss function (refer to the Designing the Output Layer section of Chapter 3, Neural Networks) for the same reason. When we use the sum of squared errors as the loss function of an “identity function”, backward propagation provides a “pretty” result, (y1 − t1, y2 − t2, y3 − t3).
Let’s consider a specific example here. Say that, for one piece of data, the label is (0, 1, 0) and the output of the Softmax layer is (0.3, 0.2, 0.5). At this time, the neural network does not recognize it correctly because the probability of it being the correct label is 0.2 (20%). Here, backward propagating from the Softmax layer propagates a large error, (0.3, −0.8, 0.5). Because this large error propagates to the previous layers, the layers before the Softmax layer learn a lot from the large error.
As another example, let’s assume that, for one piece of data, the label is (0, 1, 0) and the output of the Softmax layer is (0.01, 0.99, 0) (this neural network recognizes quite accurately). In this case, backward propagating from the Softmax layer propagates a small error, (0.01, −0.01, 0). This small error propagates to the previous layers. The layers before the Softmax layer learn only small pieces of information because the error is small.
Now, let’s implement the Softmax-with-Loss layer. You can implement the Softmax-with-Loss layer as follows:
class SoftmaxWithLoss: def __init__ (self): self.loss = None # Loss self.y = None # Output of softmax self.t = None # Label data (one-hot vector) def forward(self, x, t): self.t = t self.y = softmax(x) self.loss = cross_entropy_error(self.y, self.t) return self.loss def backward(self, dout=1): batch_size = self.t.shape[0] dx = (self.y - self.t) / batch_size return dxThis implementation uses the softmax() and cross_entropy_error() functions. They were implemented in the sub-section, Issues when Implementing the Softmax Function, of Chapter 3, Neural networks and sub-section, Implementing Cross-Entropy Error (Using Batches), of Chapter 4, Neural Network Training. Therefore, the implementation here is very easy. Note that the error per data propagates to the previous layers in backward propagation because the value of propagation is divided by the number of batches (batch_size).
Bookmark
Implementing Backpropagation
Section titled “Implementing Backpropagation”You can build a neural network by combining the layers implemented in the previous sections as if you were assembling Lego blocks. Here, we will build a neural network by combining the layers we’ve implemented so far.
Overall View of Neural Network Training
Section titled “Overall View of Neural Network Training”Because my description was a little long, let’s check the overall view of neural network training again before proceeding with its implementation. Now we will take a look at the procedure for neural network training.
Presupposition
Section titled “Presupposition”A neural network has adaptable weights and biases. Adjusting them so that they fit the training data is called “training.” Neural network training consists of the following four steps:
Step 1 (mini-batch):
Select some data at random from the training data.
Step 2 (calculating the gradients):
Obtain the gradient of the loss function for each weight parameter.
Step 3 (updating the parameters):
Update the parameters slightly in the gradient’s direction.
Step 4 (repeating):
Repeat steps 1, 2, and 3.
Backpropagation occurs in Step 2, Calculating the gradients. In the previous chapter, we used numerical differentiation to obtain a gradient. Numerical differentiation is easy to implement, but calculation takes a lot of time. If we use backpropagation, we can obtain a gradient much more quickly and efficiently.
Implementing a Neural Network That Supports Backpropagation
Section titled “Implementing a Neural Network That Supports Backpropagation”In this section, we will implement a two-layer neural network called TwoLayerNet. First, we will look at the instance variables and methods of this class in Tables 5.1 and 5.2.
The implementation of this class is a little long but has many sections in common with the implementation as in the Implementing a Training Algorithm section, of Chapter 4, Neural Network Training. A large change from the previous chapter is that layers are being used here. If you use layers, you can obtain recognition results (predict( )) and gradients (gradient( )) by propagating between the layers:

Table 5.1: Instance variables in the TwoLayerNet class
Section titled “Table 5.1: Instance variables in the TwoLayerNet class”
Table 5.2: Methods in the TwoLayerNet class
Section titled “Table 5.2: Methods in the TwoLayerNet class”Now, let’s implement TwoLayerNet:
Note the code in bold here. Retaining a neural network layer as OrderedDict (i,e, an ordered dictionary) is especially important, as it means that the dictionary can remember the order of the elements added to it. Therefore, in forward propagation in the neural network, you can complete processing by calling the forward() method of the layer in the order of addition. In backward propagation, you only have to call the layers in reverse order. The Affine and ReLU layers internally process forward propagation and backward propagation properly. So, all you have to do is combine the layers in the correct order and call them in order (or in reverse order).
Thus, by implementing the components of the neural network as “layers,” you can build the neural network easily. The advantage of modular implementation using “layers” is enormous. If you want to create a large network containing five, 10, or 20 layers, you can create it by adding the required layers (as if you were assembling Lego blocks). In this way, the gradients required for recognition and learning are obtained properly by forward propagation and backward propagation being implemented in each layer.
Gradient Check
Section titled “Gradient Check”So far, we have seen two methods for calculating a gradient. One of them uses numerical differentiation, while the other solves the equation analytically. The latter method enables efficient calculation by using backpropagation, even if many parameters exist. Therefore, we’ll be using backpropagation instead of slow numerical differentiation to calculate a gradient from now on.
Numerical differentiation takes time to calculate. If the correct implementation of backpropagation exists, we do not need the implementation of numerical differentiation. So, what is numerical differentiation useful for? The fact is that numerical differentiation is required to check whether the implementation of backpropagation is correct.
The advantage of numerical differentiation is that it is easy to implement, making mistakes infrequent compared to the far more complicated backpropagation. So, the result of numerical differentiation is often compared with that of backpropagation to check whether the implementation of backpropagation is correct. The process of this verification is called a gradient check. The following code shows the implementation of a gradient check (the source code is located at ch05/gradient_check.py):
import sys, ossys.path.append(os.pardir)import numpy as npfrom dataset.mnist import load_mnistfrom two_layer_net import TwoLayerNet# Load data(x_train, t_train), (x_test, t_test) = \
load_mnist(normalize=True, one_hot_label=True)network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)x_batch = x_train[:3]t_batch = t_train[:3]grad_numerical = network.numerical_gradient(x_batch, t_batch)grad_backprop = network.gradient(x_batch, t_batch)# Calculate the average of the absolute errors of individual weightsfor key in grad_numerical.keys( ): diff = np.average( np.abs(grad_backprop[key] - grad_numerical[key]) ) print(key + ":" + str(diff))Here, the MNIST dataset is loaded as usual. Next, part of the training data is used to check the error between the gradient by numerical differentiation and that of backpropagation. As the error, the absolute values of the differences between the elements in the individual weight parameters are averaged. When the preceding code is executed, the following result is returned:
b1:9.70418809871e-13 W2:8.41139039497e-13 b2:1.1945999745e-10 W1:2.2232446644e-13
The result shows that the differences between the gradients by numerical differentiation and those by backpropagation are quite small. Case in point, the error of the biases for layer 1 is 9.7e-13 (0.00000000000097). This indicates that the gradient by backpropagation is also correct and improves the reliability of its accuracy.
The error between the calculation result of numerical differentiation and that of backpropagation rarely becomes 0. This is because the accuracy of the calculations that are performed by a computer is finite (for example, 32-bit floating-point numbers are used). Because the numerical precision is limited, the error is not usually 0. However, if the implementation is correct, the error is expected to be a small value near 0. If the value is large, the implementation of backpropagation is incorrect.
Training Using Backpropagation
Section titled “Training Using Backpropagation”Lastly, we will see how we can implement neural network training using backpropagation. The only difference is that gradients are calculated via backpropagation. We will only see the code and omit the description (the source code is located at ch05/train_neuralnet.py):
# Use backpropagation to obtain a gradientSummary
Section titled “Summary”In this chapter, we learned about computational graphs, which show calculation processes visually. We looked at a computational graph that described backpropagation in a neural network and implemented processing in a neural network with layers, including the ReLU layer, Softmax-with-Loss layer, Affine layer, and Softmax layer. These layers have forward and backward methods and can calculate the gradients of weight parameters efficiently by propagating data both forward and backward in direction. By using layers as modules, you can combine them freely in a neural network so that you can build the desired network easily. The following points were covered in this chapter:
- We can use computational graphs to show calculation processes visually.
- A node in a computational graph consists of local calculations. Local calculations constitute the whole calculation.
- Performing forward propagation in a computational graph leads to a regular calculation. Meanwhile, performing backward propagation in a computational graph can calculate the differential of each node.
- By implementing components in a neural network as layers, you can calculate gradients efficiently (backpropagation).
- By comparing the results of numerical differentiation and backpropagation, you can check that the implementation of backpropagation is correct (gradient check).













