プリンキピア

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

7.4.2. Multiple Output Channels7.4.2 複数の出力チャネル

Regardless of the number of input channels, so far we always ended up with one output channel. However, as we discussed in Section 7.1.4, it turns out to be essential to have multiple channels at each layer. In the most popular neural network architectures, we actually increase the channel dimension as we go deeper in the neural network, typically downsampling to trade off spatial resolution for greater channel depth. Intuitively, you could think of each channel as responding to a different set of features. The reality is a bit more complicated than this. A naive interpretation would suggest that representations are learned independently per pixel or per channel. Instead, channels are optimized to be jointly useful. This means that rather than mapping a single channel to an edge detector, it may simply mean that some direction in channel space corresponds to detecting edges.
英語のヒント
入力チャネルの数にかかわらず、ここまではいつも出力チャネルが一つになっていた。しかし、7.1.4節で論じたように、各層に複数のチャネルをもつことが欠かせないとわかっている。最もよく使われるニューラルネットワークの構造では、実際、ニューラルネットワークの深くへ進むにつれてチャネルの次元を増やし、ふつうはダウンサンプリングして、空間的な解像度と引き換えにチャネルの深さを大きくする。直観的には、各チャネルがそれぞれ異なる特徴の組に反応すると考えられる。現実はこれより少し複雑である。素朴に解釈すれば、表現は画素ごとやチャネルごとに独立に学習されることになる。実際には、チャネルは組み合わさって役に立つように最適化される。つまり、一つのチャネルを縁の検出器に対応させるのではなく、単に、チャネル空間のある方向が縁の検出に対応するということなのかもしれない。
Denote by ci and co the number of input and output channels, respectively, and by kh and kw the height and width of the kernel. To get an output with multiple channels, we can create a kernel tensor of shape ci×kh×kw for every output channel. We concatenate them on the output channel dimension, so that the shape of the convolution kernel is co×ci×kh×kw. In cross-correlation operations, the result on each output channel is calculated from the convolution kernel corresponding to that output channel and takes input from all channels in the input tensor.
英語のヒント
入力チャネルと出力チャネルの数をそれぞれciとcoで、カーネルの高さと幅をkhとkwで表す。複数のチャネルをもつ出力を得るには、出力チャネルごとに形状ci×kh×kwのカーネルテンソルを作ればよい。それらを出力チャネルの次元で連結すると、畳み込みカーネルの形状はco×ci×kh×kwになる。相互相関の演算では、各出力チャネルでの結果は、その出力チャネルに対応する畳み込みカーネルから計算され、入力テンソルのすべてのチャネルから入力を受け取る。
We implement a cross-correlation function to calculate the output of multiple channels as shown below.
英語のヒント
下に示すように、複数のチャネルの出力を計算する相互相関の関数を実装する。
def corr2d_multi_in_out(X, K):
    # Iterate through the 0th dimension of K, and each time, perform
    # cross-correlation operations with input X. All of the results are
    # stacked together
    return torch.stack([corr2d_multi_in(X, k) for k in K], 0)
We construct a trivial convolution kernel with three output channels by concatenating the kernel tensor for K with K+1 and K+2.
英語のヒント
KのカーネルテンソルをK+1とK+2と連結して、三つの出力チャネルをもつごく単純な畳み込みカーネルを作る。
K = torch.stack((K, K + 1, K + 2), 0)
K.shape
torch.Size([3, 2, 2, 2])
Below, we perform cross-correlation operations on the input tensor X with the kernel tensor K. Now the output contains three channels. The result of the first channel is consistent with the result of the previous input tensor X and the multi-input channel, single-output channel kernel.
英語のヒント
下では、入力テンソルXに対して、カーネルテンソルKで相互相関の演算を行う。今度は出力に三つのチャネルが含まれる。1番目のチャネルの結果は、前の入力テンソルXと、複数の入力チャネルと一つの出力チャネルをもつカーネルでの結果と一致する。
corr2d_multi_in_out(X, K)
tensor([[[ 56.,  72.],
         [104., 120.]],

        [[ 76., 100.],
         [148., 172.]],

        [[ 96., 128.],
         [192., 224.]]])