プリンキピア

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

7.4.1. Multiple Input Channels7.4.1 複数の入力チャネル

When the input data contains multiple channels, we need to construct a convolution kernel with the same number of input channels as the input data, so that it can perform cross-correlation with the input data. Assuming that the number of channels for the input data is ci, the number of input channels of the convolution kernel also needs to be ci. If our convolution kernel’s window shape is kh×kw, then, when ci=1, we can think of our convolution kernel as just a two-dimensional tensor of shape kh×kw.
英語のヒント
入力データが複数のチャネルを含むときには、入力データと相互相関をとれるよう、入力データと同じ数の入力チャネルをもつ畳み込みカーネルを作る必要がある。入力データのチャネル数をciとすると、畳み込みカーネルの入力チャネルの数もciでなければならない。畳み込みカーネルの窓の形状がkh×kwなら、ci=1のときには、畳み込みカーネルを形状kh×kwの単なる2次元テンソルと考えられる。
However, when ci>1, we need a kernel that contains a tensor of shape kh×kw for every input channel. Concatenating these ci tensors together yields a convolution kernel of shape ci×kh×kw. Since the input and convolution kernel each have ci channels, we can perform a cross-correlation operation on the two-dimensional tensor of the input and the two-dimensional tensor of the convolution kernel for each channel, adding the ci results together (summing over the channels) to yield a two-dimensional tensor. This is the result of a two-dimensional cross-correlation between a multi-channel input and a multi-input-channel convolution kernel.
英語のヒント
しかし、ci>1のときには、入力チャネルごとに形状kh×kwのテンソルを含むカーネルが必要である。これらci個のテンソルを連結すると、形状ci×kh×kwの畳み込みカーネルが得られる。入力と畳み込みカーネルはどちらもci個のチャネルをもつので、チャネルごとに、入力の2次元テンソルと畳み込みカーネルの2次元テンソルとで相互相関の演算を行い、ci個の結果を足し合わせて(チャネルにわたって和をとって)2次元のテンソルを得られる。これが、複数チャネルの入力と、複数の入力チャネルをもつ畳み込みカーネルとのあいだの2次元の相互相関の結果である。
Fig. 7.4.1 provides an example of a two-dimensional cross-correlation with two input channels. The shaded portions are the first output element as well as the input and kernel tensor elements used for the output computation: (1×1+2×2+4×3+5×4)+(0×0+1×1+3×2+4×3)=56.
英語のヒント
図7.4.1は、二つの入力チャネルをもつ2次元の相互相関の例を示している。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。(1×1+2×2+4×3+5×4)+(0×0+1×1+3×2+4×3)=56。
Fig. 7.4.1図7.4.1
Cross-correlation computation with two input channels.
英語のヒント
二つの入力チャネルによる相互相関の計算。
To make sure we really understand what is going on here, we can implement cross-correlation operations with multiple input channels ourselves. Notice that all we are doing is performing a cross-correlation operation per channel and then adding up the results.
英語のヒント
ここで何が起きているかを本当に理解できているか確かめるため、複数の入力チャネルをもつ相互相関の演算を自分で実装してみよう。やっていることは、チャネルごとに相互相関の演算を行い、それから結果を足し合わせることだけである点に注意してほしい。
def corr2d_multi_in(X, K):
    # Iterate through the 0th dimension (channel) of K first, then add them up
    return sum(d2l.corr2d(x, k) for x, k in zip(X, K))
We can construct the input tensor X and the kernel tensor K corresponding to the values in Fig. 7.4.1 to validate the output of the cross-correlation operation.
英語のヒント
図7.4.1の値に対応する入力テンソルXとカーネルテンソルKを組み立てて、相互相関の演算の出力を確かめられる。
X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
               [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]])
K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]])

corr2d_multi_in(X, K)
tensor([[ 56.,  72.],
        [104., 120.]])