第8章現代の畳み込みニューラルネットワーク
8.6.2. Residual Blocks8.6.2 残差ブロック
Let’s focus on a local part of a neural network, as depicted in Fig. 8.6.2. Denote the input by . We assume that , the desired underlying mapping we want to obtain by learning, is to be used as input to the activation function on the top. On the left, the portion within the dotted-line box must directly learn . On the right, the portion within the dotted-line box needs to learn the residual mapping , which is how the residual block derives its name. If the identity mapping is the desired underlying mapping, the residual mapping amounts to and it is thus easier to learn: we only need to push the weights and biases of the upper weight layer (e.g., fully connected layer and convolutional layer) within the dotted-line box to zero. The right figure illustrates the residual block of ResNet, where the solid line carrying the layer input to the addition operator is called a residual connection (or shortcut connection). With residual blocks, inputs can forward propagate faster through the residual connections across layers. In fact, the residual block can be thought of as a special case of the multi-branch Inception block: it has two branches one of which is the identity mapping.
英語のヒント
図8.6.2に描いたような、ニューラルネットワークの局所的な部分に注目しよう。入力をと表す。学習によって得たい本来の写像が、一番上の活性化関数への入力として使われるものとする。左では、点線の枠の中の部分がを直接学習しなければならない。右では、点線の枠の中の部分は残差写像を学習すればよく、残差ブロックという名前はここに由来する。恒等写像が求める本来の写像であれば、残差写像はとなり、したがって学習しやすい。点線の枠の中にある上側の重みの層(たとえば全結合層や畳み込み層)の重みとバイアスを0に押しやるだけでよいからである。右の図はResNetの残差ブロックを示しており、層の入力を加算の演算子へ運ぶ実線は残差接続(またはショートカット接続)と呼ばれる。残差ブロックがあると、入力は層をまたぐ残差接続を通って、より速く順伝播できる。実際、残差ブロックは多分岐のInceptionブロックの特別な場合とみなせる。二つの枝をもち、その一方が恒等写像なのである。
Fig. 8.6.2図8.6.2
In a regular block (left), the portion within the dotted-line box must directly learn the mapping . In a residual block (right), the portion within the dotted-line box needs to learn the residual mapping , making the identity mapping easier to learn.
英語のヒント
通常のブロック(左)では、点線の枠の中の部分が写像を直接学習しなければならない。残差ブロック(右)では、点線の枠の中の部分は残差写像を学習すればよく、これにより恒等写像が学習しやすくなる。
ResNet has VGG’s full convolutional layer design. The residual block has two convolutional layers with the same number of output channels. Each convolutional layer is followed by a batch normalization layer and a ReLU activation function. Then, we skip these two convolution operations and add the input directly before the final ReLU activation function. This kind of design requires that the output of the two convolutional layers has to be of the same shape as the input, so that they can be added together. If we want to change the number of channels, we need to introduce an additional convolutional layer to transform the input into the desired shape for the addition operation. Let’s have a look at the code below.
英語のヒント
ResNetは、VGGの、すべての畳み込み層で組む設計を受け継いでいる。残差ブロックには、出力チャネルの数が同じの畳み込み層が二つある。各畳み込み層の後には、バッチ正規化層とReLU活性化関数が続く。次に、これら二つの畳み込みの演算を飛び越えて、最後のReLU活性化関数の直前で入力を直接加える。この種の設計では、二つの畳み込み層の出力を入力と足し合わせられるよう、出力が入力と同じ形状でなければならない。チャネルの数を変えたい場合は、加算の演算のために入力を望みの形状に変換する、の畳み込み層を追加で導入する必要がある。下のコードを見てみよう。
class Residual(nn.Module): #@save
"""The Residual block of ResNet models."""
def __init__(self, num_channels, use_1x1conv=False, strides=1):
super().__init__()
self.conv1 = nn.LazyConv2d(num_channels, kernel_size=3, padding=1,
stride=strides)
self.conv2 = nn.LazyConv2d(num_channels, kernel_size=3, padding=1)
if use_1x1conv:
self.conv3 = nn.LazyConv2d(num_channels, kernel_size=1,
stride=strides)
else:
self.conv3 = None
self.bn1 = nn.LazyBatchNorm2d()
self.bn2 = nn.LazyBatchNorm2d()
def forward(self, X):
Y = F.relu(self.bn1(self.conv1(X)))
Y = self.bn2(self.conv2(Y))
if self.conv3:
X = self.conv3(X)
Y += X
return F.relu(Y)This code generates two types of networks: one where we add the input to the output before applying the ReLU nonlinearity whenever
use_1x1conv=False; and one where we adjust channels and resolution by means of a convolution before adding. Fig. 8.6.3 illustrates this.英語のヒント
このコードは2種類のネットワークを生成する。一つは、
use_1x1conv=Falseのときに、ReLUの非線形性を当てはめる前に入力を出力に加えるもの、もう一つは、加える前にの畳み込みによってチャネルと解像度を調整するものである。図8.6.3がこれを示している。Fig. 8.6.3図8.6.3
ResNet block with and without convolution, which transforms the input into the desired shape for the addition operation.
英語のヒント
の畳み込みがあるResNetブロックとないResNetブロック。この畳み込みは、加算の演算のために入力を望みの形状に変換する。
Now let’s look at a situation where the input and output are of the same shape, where convolution is not needed.
英語のヒント
次に、入力と出力が同じ形状で、の畳み込みが要らない状況を見てみよう。
blk = Residual(3)
X = torch.randn(4, 3, 6, 6)
blk(X).shapetorch.Size([4, 3, 6, 6])We also have the option to halve the output height and width while increasing the number of output channels. In this case we use convolutions via
use_1x1conv=True. This comes in handy at the beginning of each ResNet block to reduce the spatial dimensionality via strides=2.英語のヒント
出力チャネルの数を増やしながら、出力の高さと幅を半分にするという選択肢もある。この場合は、
use_1x1conv=Trueによっての畳み込みを使う。これは、各ResNetブロックの始めで、strides=2によって空間の次元を減らすのに重宝する。blk = Residual(6, use_1x1conv=True, strides=2)
blk(X).shapetorch.Size([4, 6, 3, 3])