第22章付録:深層学習のための数学
22.4.5. The Backpropagation Algorithm22.4.5 誤差逆伝播のアルゴリズム
Let’s return to the example of (22.4.14) the previous section where
英語のヒント
前の節の式(22.4.14)の例に戻ろう。そこでは次のようになっていた。
(22.4.20)
If we want to compute say we may apply the multi-variate chain rule to see:
英語のヒント
たとえばを計算したいなら、多変数の連鎖律を適用して次がわかる。
(22.4.21)
Let’s try using this decomposition to compute . Notice that all we need here are the various single step partials:
英語のヒント
この分解を使ってを計算してみよう。ここで必要なのは、さまざまな1段の偏導関数だけであることに注意しよう。
(22.4.22)
If we write this out into code this becomes a fairly manageable expression.
英語のヒント
これをコードに書き出すと、かなり扱いやすい式になる。
# Compute the value of the function from inputs to outputs
w, x, y, z = -1, 0, -2, 1
a, b = (w + x + y + z)**2, (w + x - y - z)**2
u, v = (a + b)**2, (a - b)**2
f = (u + v)**2
print(f' f at {w}, {x}, {y}, {z} is {f}')
# Compute the single step partials
df_du, df_dv = 2*(u + v), 2*(u + v)
du_da, du_db, dv_da, dv_db = 2*(a + b), 2*(a + b), 2*(a - b), -2*(a - b)
da_dw, db_dw = 2*(w + x + y + z), 2*(w + x - y - z)
# Compute the final result from inputs to outputs
du_dw, dv_dw = du_da*da_dw + du_db*db_dw, dv_da*da_dw + dv_db*db_dw
df_dw = df_du*du_dw + df_dv*dv_dw
print(f'df/dw at {w}, {x}, {y}, {z} is {df_dw}') f at -1, 0, -2, 1 is 1024
df/dw at -1, 0, -2, 1 is -4096However, note that this still does not make it easy to compute something like . The reason for that is the way we chose to apply the chain rule. If we look at what we did above, we always kept in the denominator when we could. In this way, we chose to apply the chain rule seeing how changed every other variable. If that is what we wanted, this would be a good idea. However, think back to our motivation from deep learning: we want to see how every parameter changes the loss. In essence, we want to apply the chain rule keeping in the numerator whenever we can!
英語のヒント
しかし、これでもまだのようなものを計算するのは簡単にならないことに注意しよう。その理由は、連鎖律の適用のしかたを選んだやり方にある。上でしたことを見ると、できるときはいつもを分母に置いていた。このようにして、がほかのすべての変数をどう変えるかを見るように連鎖律を適用することを選んだ。それが望むことなら、これはよい考えだろう。しかし、深層学習からの動機を思い出そう。各パラメータが損失をどう変えるかを見たいのである。要するに、できるときはいつもを分子に置いたまま連鎖律を適用したいのである。
To be more explicit, note that we can write
英語のヒント
もっと明示的にするため、次のように書けることに注意しよう。
(22.4.23)
Note that this application of the chain rule has us explicitly compute . Nothing stops us from also including the equations:
英語のヒント
この連鎖律の適用では、を明示的に計算することに注意しよう。次の式も含めるのを妨げるものは何もない。
(22.4.24)
and then keeping track of how changes when we change any node in the entire network. Let’s implement it.
英語のヒント
そして、ネットワーク全体のどのノードを変えたときにもがどう変わるかを追いかけるのである。これを実装しよう。
# Compute the value of the function from inputs to outputs
w, x, y, z = -1, 0, -2, 1
a, b = (w + x + y + z)**2, (w + x - y - z)**2
u, v = (a + b)**2, (a - b)**2
f = (u + v)**2
print(f'f at {w}, {x}, {y}, {z} is {f}')
# Compute the derivative using the decomposition above
# First compute the single step partials
df_du, df_dv = 2*(u + v), 2*(u + v)
du_da, du_db, dv_da, dv_db = 2*(a + b), 2*(a + b), 2*(a - b), -2*(a - b)
da_dw, db_dw = 2*(w + x + y + z), 2*(w + x - y - z)
da_dx, db_dx = 2*(w + x + y + z), 2*(w + x - y - z)
da_dy, db_dy = 2*(w + x + y + z), -2*(w + x - y - z)
da_dz, db_dz = 2*(w + x + y + z), -2*(w + x - y - z)
# Now compute how f changes when we change any value from output to input
df_da, df_db = df_du*du_da + df_dv*dv_da, df_du*du_db + df_dv*dv_db
df_dw, df_dx = df_da*da_dw + df_db*db_dw, df_da*da_dx + df_db*db_dx
df_dy, df_dz = df_da*da_dy + df_db*db_dy, df_da*da_dz + df_db*db_dz
print(f'df/dw at {w}, {x}, {y}, {z} is {df_dw}')
print(f'df/dx at {w}, {x}, {y}, {z} is {df_dx}')
print(f'df/dy at {w}, {x}, {y}, {z} is {df_dy}')
print(f'df/dz at {w}, {x}, {y}, {z} is {df_dz}')f at -1, 0, -2, 1 is 1024
df/dw at -1, 0, -2, 1 is -4096
df/dx at -1, 0, -2, 1 is -4096
df/dy at -1, 0, -2, 1 is -4096
df/dz at -1, 0, -2, 1 is -4096The fact that we compute derivatives from back towards the inputs rather than from the inputs forward to the outputs (as we did in the first code snippet above) is what gives this algorithm its name: backpropagation. Note that there are two steps: 1. Compute the value of the function, and the single step partials from front to back. While not done above, this can be combined into a single forward pass. 2. Compute the gradient of from back to front. We call this the backwards pass.
英語のヒント
入力から出力へと前向きに(上の最初のコード片でしたように)ではなく、から入力に向かって後ろ向きに導関数を計算するという事実が、このアルゴリズムに誤差逆伝播(backpropagation)という名前を与えている。手順は二つあることに注意しよう。1. 関数の値と1段の偏導関数を前から後ろへ計算する。上ではしなかったが、これは1回の順伝播にまとめられる。2. の勾配を後ろから前へ計算する。これを逆伝播と呼ぶ。
This is precisely what every deep learning algorithm implements to allow the computation of the gradient of the loss with respect to every weight in the network at one pass. It is an astonishing fact that we have such a decomposition.
英語のヒント
これはまさに、ネットワークのすべての重みについての損失の勾配を1回のパスで計算できるように、あらゆる深層学習のアルゴリズムが実装していることである。このような分解があるのは驚くべき事実である。
To see how to encapsulated this, let’s take a quick look at this example.
英語のヒント
これをどうカプセル化するかを見るため、この例を手早く見よう。
# Initialize as ndarrays, then attach gradients
w = torch.tensor([-1.], requires_grad=True)
x = torch.tensor([0.], requires_grad=True)
y = torch.tensor([-2.], requires_grad=True)
z = torch.tensor([1.], requires_grad=True)
# Do the computation like usual, tracking gradients
a, b = (w + x + y + z)**2, (w + x - y - z)**2
u, v = (a + b)**2, (a - b)**2
f = (u + v)**2
# Execute backward pass
f.backward()
print(f'df/dw at {w.data.item()}, {x.data.item()}, {y.data.item()}, '
f'{z.data.item()} is {w.grad.data.item()}')
print(f'df/dx at {w.data.item()}, {x.data.item()}, {y.data.item()}, '
f'{z.data.item()} is {x.grad.data.item()}')
print(f'df/dy at {w.data.item()}, {x.data.item()}, {y.data.item()}, '
f'{z.data.item()} is {y.grad.data.item()}')
print(f'df/dz at {w.data.item()}, {x.data.item()}, {y.data.item()}, '
f'{z.data.item()} is {z.grad.data.item()}')df/dw at -1.0, 0.0, -2.0, 1.0 is -4096.0
df/dx at -1.0, 0.0, -2.0, 1.0 is -4096.0
df/dy at -1.0, 0.0, -2.0, 1.0 is -4096.0
df/dz at -1.0, 0.0, -2.0, 1.0 is -4096.0All of what we did above can be done automatically by calling
f.backwards().英語のヒント
上でしたことはすべて、
f.backwards()を呼べば自動的に行える。