第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 , the number of input channels of the convolution kernel also needs to be . If our convolution kernel’s window shape is , then, when , we can think of our convolution kernel as just a two-dimensional tensor of shape .
英語のヒント
入力データが複数のチャネルを含むときには、入力データと相互相関をとれるよう、入力データと同じ数の入力チャネルをもつ畳み込みカーネルを作る必要がある。入力データのチャネル数をとすると、畳み込みカーネルの入力チャネルの数もでなければならない。畳み込みカーネルの窓の形状がなら、のときには、畳み込みカーネルを形状の単なる2次元テンソルと考えられる。
However, when , we need a kernel that contains a tensor of shape for every input channel. Concatenating these tensors together yields a convolution kernel of shape . Since the input and convolution kernel each have 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 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.
英語のヒント
しかし、のときには、入力チャネルごとに形状のテンソルを含むカーネルが必要である。これら個のテンソルを連結すると、形状の畳み込みカーネルが得られる。入力と畳み込みカーネルはどちらも個のチャネルをもつので、チャネルごとに、入力の2次元テンソルと畳み込みカーネルの2次元テンソルとで相互相関の演算を行い、個の結果を足し合わせて(チャネルにわたって和をとって)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: .
英語のヒント
図7.4.1は、二つの入力チャネルをもつ2次元の相互相関の例を示している。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。。
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.]])