プリンキピア

第7章畳み込みニューラルネットワーク

7.2.1. The Cross-Correlation Operation7.2.1 相互相関の演算

Recall that strictly speaking, convolutional layers are a misnomer, since the operations they express are more accurately described as cross-correlations. Based on our descriptions of convolutional layers in Section 7.1, in such a layer, an input tensor and a kernel tensor are combined to produce an output tensor through a cross-correlation operation.
英語のヒント
厳密に言えば、畳み込み層という呼び方は誤りであることを思い出してほしい。それが表す演算は、もっと正確には相互相関として記述されるからである。7.1節での畳み込み層の説明にもとづけば、そうした層では、入力テンソルとカーネルテンソルが組み合わされ、相互相関の演算を通じて出力テンソルが生み出される。
Let’s ignore channels for now and see how this works with two-dimensional data and hidden representations. In Fig. 7.2.1, the input is a two-dimensional tensor with a height of 3 and width of 3. We mark the shape of the tensor as 3×3 or (3, 3). The height and width of the kernel are both 2. The shape of the kernel window (or convolution window) is given by the height and width of the kernel (here it is 2×2).
英語のヒント
今のところチャネルは無視して、2次元のデータと隠れ表現でこれがどう働くかを見よう。図7.2.1では、入力は高さ3、幅3の2次元のテンソルである。テンソルの形状を3×3または(3, 3)と記す。カーネルの高さと幅はどちらも2である。カーネルの窓(または畳み込みの窓)の形状は、カーネルの高さと幅で与えられる(ここでは2×2である)。
Fig. 7.2.1図7.2.1
Two-dimensional cross-correlation operation. The shaded portions are the first output element as well as the input and kernel tensor elements used for the output computation: 0×0+1×1+3×2+4×3=19.
英語のヒント
2次元の相互相関の演算。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。0×0+1×1+3×2+4×3=19。
In the two-dimensional cross-correlation operation, we begin with the convolution window positioned at the upper-left corner of the input tensor and slide it across the input tensor, both from left to right and top to bottom. When the convolution window slides to a certain position, the input subtensor contained in that window and the kernel tensor are multiplied elementwise and the resulting tensor is summed up yielding a single scalar value. This result gives the value of the output tensor at the corresponding location. Here, the output tensor has a height of 2 and width of 2 and the four elements are derived from the two-dimensional cross-correlation operation:
英語のヒント
2次元の相互相関の演算では、畳み込みの窓を入力テンソルの左上の隅に置いたところから始め、入力テンソルの上を左から右へ、上から下へとすべらせていく。畳み込みの窓がある位置にすべっていくと、その窓に含まれる入力の部分テンソルとカーネルテンソルが成分ごとに掛け合わされ、得られたテンソルの和をとって一つのスカラー値が得られる。この結果が、対応する位置での出力テンソルの値になる。ここでは、出力テンソルは高さ2、幅2で、四つの要素は2次元の相互相関の演算から導かれる。
0×0+1×1+3×2+4×3=19,1×0+2×1+4×2+5×3=25,3×0+4×1+6×2+7×3=37,4×0+5×1+7×2+8×3=43.(7.2.1)
Note that along each axis, the output size is slightly smaller than the input size. Because the kernel has width and height greater than 1, we can only properly compute the cross-correlation for locations where the kernel fits wholly within the image, the output size is given by the input size nh×nw minus the size of the convolution kernel kh×kw via
英語のヒント
どの軸に沿っても、出力の大きさは入力の大きさより少し小さいことに注意してほしい。カーネルの幅と高さは1より大きいので、相互相関を正しく計算できるのは、カーネルが画像の中にすっぽり収まる位置だけである。出力の大きさは、入力の大きさnh×nwから畳み込みカーネルの大きさkh×kwを引いて、次のように与えられる。
(nh−kh+1)×(nw−kw+1).(7.2.2)
This is the case since we need enough space to “shift” the convolution kernel across the image. Later we will see how to keep the size unchanged by padding the image with zeros around its boundary so that there is enough space to shift the kernel. Next, we implement this process in the corr2d function, which accepts an input tensor X and a kernel tensor K and returns an output tensor Y.
英語のヒント
そうなるのは、畳み込みカーネルを画像の上で「ずらす」のに十分な空間が必要だからである。後で、カーネルをずらすのに十分な空間ができるよう、画像の境界の周りを0で埋めて、大きさを変えずに保つ方法を見る。次に、この過程をcorr2d関数に実装する。この関数は、入力テンソルXとカーネルテンソルKを受け取り、出力テンソルYを返す。
def corr2d(X, K):  #@save
    """Compute 2D cross-correlation."""
    h, w = K.shape
    Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))
    for i in range(Y.shape[0]):
        for j in range(Y.shape[1]):
            Y[i, j] = (X[i:i + h, j:j + w] * K).sum()
    return Y
We can construct the input tensor X and the kernel tensor K from Fig. 7.2.1 to validate the output of the above implementation of the two-dimensional cross-correlation operation.
英語のヒント
図7.2.1の入力テンソルXとカーネルテンソルKを組み立てて、上の2次元の相互相関の演算の実装の出力を確かめられる。
X = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
corr2d(X, K)
tensor([[19., 25.],
        [37., 43.]])