第5章多層パーセプトロン
5.1.2. Activation Functions5.1.2 活性化関数
Activation functions decide whether a neuron should be activated or not by calculating the weighted sum and further adding bias to it. They are differentiable operators for transforming input signals to outputs, while most of them add nonlinearity. Because activation functions are fundamental to deep learning, let’s briefly survey some common ones.
英語のヒント
活性化関数は、重み付きの和を計算し、さらにそれにバイアスを加えることで、ニューロンを活性化すべきかどうかを決める。活性化関数は、入力信号を出力へ変換する微分可能な演算子であり、そのほとんどは非線形性を加える。活性化関数は深層学習の根幹なので、よく使われるものをいくつか手短に見ていこう。
5.1.2.1. ReLU Function5.1.2.1 ReLU関数
The most popular choice, due to both simplicity of implementation and its good performance on a variety of predictive tasks, is the rectified linear unit (ReLU) (Nair and Hinton, 2010). ReLU provides a very simple nonlinear transformation. Given an element , the function is defined as the maximum of that element and :
英語のヒント
実装が簡単であることと、さまざまな予測の課題で性能がよいことの両方から、最もよく選ばれるのは正規化線形ユニット(ReLU)である(Nair and Hinton, 2010)。ReLUは、とても単純な非線形変換を与える。要素が与えられたとき、この関数は、その要素とのうち大きいほうとして定義される。
(5.1.4)
Informally, the ReLU function retains only positive elements and discards all negative elements by setting the corresponding activations to 0. To gain some intuition, we can plot the function. As you can see, the activation function is piecewise linear.
英語のヒント
くだけた言い方をすれば、ReLU関数は正の要素だけを残し、負の要素は対応する活性を0にしてすべて捨てる。直観をつかむため、関数をプロットしてみよう。見てのとおり、この活性化関数は区分的に線形である。
x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)
y = torch.relu(x)
d2l.plot(x.detach(), y.detach(), 'x', 'relu(x)', figsize=(5, 2.5))When the input is negative, the derivative of the ReLU function is 0, and when the input is positive, the derivative of the ReLU function is 1. Note that the ReLU function is not differentiable when the input takes value precisely equal to 0. In these cases, we default to the left-hand-side derivative and say that the derivative is 0 when the input is 0. We can get away with this because the input may never actually be zero (mathematicians would say that it is nondifferentiable on a set of measure zero). There is an old adage that if subtle boundary conditions matter, we are probably doing (real) mathematics, not engineering. That conventional wisdom may apply here, or at least, the fact that we are not performing constrained optimization (Mangasarian, 1965, Rockafellar, 1970). We plot the derivative of the ReLU function below.
英語のヒント
入力が負のとき、ReLU関数の導関数は0であり、入力が正のとき、ReLU関数の導関数は1である。入力の値がちょうど0に等しいとき、ReLU関数は微分可能でないことに注意してほしい。そうした場合には、左側の導関数を既定として使い、入力が0のとき導関数は0だとする。こうしても問題にならないのは、入力が実際に0になることはまずないからである(数学者なら、測度0の集合の上で微分不可能だと言うだろう)。微妙な境界条件が問題になるなら、それはたぶん工学ではなく(本物の)数学をやっているのだ、という古い格言がある。その通念はここにも当てはまるかもしれないし、少なくとも、制約付き最適化を行っているわけではないという事実は当てはまる(Mangasarian, 1965、Rockafellar, 1970)。下で、ReLU関数の導関数をプロットする。
y.backward(torch.ones_like(x), retain_graph=True)
d2l.plot(x.detach(), x.grad, 'x', 'grad of relu', figsize=(5, 2.5))The reason for using ReLU is that its derivatives are particularly well behaved: either they vanish or they just let the argument through. This makes optimization better behaved and it mitigated the well-documented problem of vanishing gradients that plagued previous versions of neural networks (more on this later).
英語のヒント
ReLUを使う理由は、その導関数がとりわけ素直にふるまうことである。導関数は消えるか、引数をそのまま通すかのどちらかなのである。これにより最適化のふるまいがよくなり、以前のニューラルネットワークを悩ませていた、よく知られた勾配消失の問題が和らいだ(これについては後で詳しく述べる)。
Note that there are many variants to the ReLU function, including the parametrized ReLU (pReLU) function (He et al., 2015). This variation adds a linear term to ReLU, so some information still gets through, even when the argument is negative:
英語のヒント
ReLU関数には、パラメータ化ReLU(pReLU)関数(He et al., 2015)をはじめ、多くの変種があることに注意してほしい。この変種はReLUに線形の項を加えるので、引数が負のときでも、いくらかの情報は通り抜ける。
(5.1.5)
5.1.2.2. Sigmoid Function5.1.2.2 シグモイド関数
The sigmoid function transforms those inputs whose values lie in the domain , to outputs that lie on the interval (0, 1). For that reason, the sigmoid is often called a squashing function: it squashes any input in the range (-inf, inf) to some value in the range (0, 1):
英語のヒント
シグモイド関数は、値が定義域にある入力を、区間(0, 1)にある出力へ変換する。そのため、シグモイドはしばしば押しつぶし関数と呼ばれる。範囲(-inf, inf)のどんな入力も、範囲(0, 1)のある値へ押しつぶすのである。
(5.1.6)
In the earliest neural networks, scientists were interested in modeling biological neurons that either fire or do not fire. Thus the pioneers of this field, going all the way back to McCulloch and Pitts, the inventors of the artificial neuron, focused on thresholding units (McCulloch and Pitts, 1943). A thresholding activation takes value 0 when its input is below some threshold and value 1 when the input exceeds the threshold.
英語のヒント
最初期のニューラルネットワークでは、科学者たちは、発火するかしないかのどちらかである生物のニューロンをモデル化することに関心をもっていた。そのため、人工ニューロンの発明者であるMcCullochとPittsにまでさかのぼるこの分野の先駆者たちは、閾値ユニットに焦点を当てた(McCulloch and Pitts, 1943)。閾値による活性化は、入力がある閾値より下なら値0を、入力が閾値を超えれば値1をとる。
When attention shifted to gradient-based learning, the sigmoid function was a natural choice because it is a smooth, differentiable approximation to a thresholding unit. Sigmoids are still widely used as activation functions on the output units when we want to interpret the outputs as probabilities for binary classification problems: you can think of the sigmoid as a special case of the softmax. However, the sigmoid has largely been replaced by the simpler and more easily trainable ReLU for most use in hidden layers. Much of this has to do with the fact that the sigmoid poses challenges for optimization (LeCun et al., 1998) since its gradient vanishes for large positive and negative arguments. This can lead to plateaus that are difficult to escape from. Nonetheless sigmoids are important. In later chapters (e.g., Section 10.1) on recurrent neural networks, we will describe architectures that leverage sigmoid units to control the flow of information across time.
英語のヒント
関心が勾配にもとづく学習に移ったとき、シグモイド関数は自然な選択だった。閾値ユニットをなめらかで微分可能な形で近似したものだからである。二値分類の問題で出力を確率と解釈したいときには、シグモイドは今でも出力ユニットの活性化関数として広く使われている。シグモイドはソフトマックスの特別な場合と考えることができる。しかし、隠れ層での使い道のほとんどでは、シグモイドはより単純で訓練しやすいReLUにおおむね取って代わられた。その大きな理由は、シグモイドが最適化を難しくすることにある(LeCun et al., 1998)。正や負の大きな引数に対して勾配が消えてしまうからである。これは、抜け出しにくい停滞域につながりうる。それでも、シグモイドは重要である。リカレントニューラルネットワークを扱う後の章(たとえば10.1節)では、時間をまたぐ情報の流れを制御するのにシグモイドのユニットを活用する構造を説明する。
Below, we plot the sigmoid function. Note that when the input is close to 0, the sigmoid function approaches a linear transformation.
英語のヒント
下でシグモイド関数をプロットする。入力が0に近いとき、シグモイド関数は線形変換に近づくことに注意してほしい。
y = torch.sigmoid(x)
d2l.plot(x.detach(), y.detach(), 'x', 'sigmoid(x)', figsize=(5, 2.5))The derivative of the sigmoid function is given by the following equation:
英語のヒント
シグモイド関数の導関数は、次の式で与えられる。
(5.1.7)
The derivative of the sigmoid function is plotted below. Note that when the input is 0, the derivative of the sigmoid function reaches a maximum of 0.25. As the input diverges from 0 in either direction, the derivative approaches 0.
英語のヒント
シグモイド関数の導関数を下にプロットする。入力が0のとき、シグモイド関数の導関数は最大値0.25に達することに注意してほしい。入力がどちらの方向にでも0から離れていくにつれて、導関数は0に近づく。
# Clear out previous gradients
x.grad.data.zero_()
y.backward(torch.ones_like(x),retain_graph=True)
d2l.plot(x.detach(), x.grad, 'x', 'grad of sigmoid', figsize=(5, 2.5))5.1.2.3. Tanh Function5.1.2.3 tanh関数
Like the sigmoid function, the tanh (hyperbolic tangent) function also squashes its inputs, transforming them into elements on the interval between and :
英語のヒント
シグモイド関数と同じく、tanh(双曲線正接)関数も入力を押しつぶし、からまでの区間にある要素へ変換する。
(5.1.8)
We plot the tanh function below. Note that as input nears 0, the tanh function approaches a linear transformation. Although the shape of the function is similar to that of the sigmoid function, the tanh function exhibits point symmetry about the origin of the coordinate system (Kalman and Kwasny, 1992).
英語のヒント
下でtanh関数をプロットする。入力が0に近づくにつれて、tanh関数は線形変換に近づくことに注意してほしい。関数の形はシグモイド関数のものに似ているが、tanh関数は座標系の原点について点対称である(Kalman and Kwasny, 1992)。
y = torch.tanh(x)
d2l.plot(x.detach(), y.detach(), 'x', 'tanh(x)', figsize=(5, 2.5))The derivative of the tanh function is:
英語のヒント
tanh関数の導関数は次のとおりである。
(5.1.9)
It is plotted below. As the input nears 0, the derivative of the tanh function approaches a maximum of 1. And as we saw with the sigmoid function, as input moves away from 0 in either direction, the derivative of the tanh function approaches 0.
英語のヒント
これを下にプロットする。入力が0に近づくにつれて、tanh関数の導関数は最大値1に近づく。そして、シグモイド関数で見たように、入力がどちらの方向にでも0から離れていくにつれて、tanh関数の導関数は0に近づく。
# Clear out previous gradients
x.grad.data.zero_()
y.backward(torch.ones_like(x),retain_graph=True)
d2l.plot(x.detach(), x.grad, 'x', 'grad of tanh', figsize=(5, 2.5))