プリンキピア

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

7.4.3. 1×1 Convolutional Layer7.4.3 1×1畳み込み層

At first, a 1×1 convolution, i.e., kh=kw=1, does not seem to make much sense. After all, a convolution correlates adjacent pixels. A 1×1 convolution obviously does not. Nonetheless, they are popular operations that are sometimes included in the designs of complex deep networks (Lin et al., 2013, Szegedy et al., 2017). Let’s see in some detail what it actually does.
英語のヒント
一見すると、1×1の畳み込み、つまりkh=kw=1は、あまり意味をなさないように思える。何しろ、畳み込みは隣り合う画素を関係づけるものである。1×1の畳み込みは明らかにそうしない。それでも、これは複雑な深いネットワークの設計にときどき含まれる、人気のある演算である(Lin et al., 2013、Szegedy et al., 2017)。それが実際に何をするのかを少し詳しく見てみよう。
Because the minimum window is used, the 1×1 convolution loses the ability of larger convolutional layers to recognize patterns consisting of interactions among adjacent elements in the height and width dimensions. The only computation of the 1×1 convolution occurs on the channel dimension.
英語のヒント
最小の窓を使うので、1×1の畳み込みは、高さと幅の次元で隣り合う要素のあいだの相互作用からなるパターンを認識するという、もっと大きな畳み込み層がもつ能力を失う。1×1の畳み込みの計算は、チャネルの次元でしか起きない。
Fig. 7.4.2 shows the cross-correlation computation using the 1×1 convolution kernel with 3 input channels and 2 output channels. Note that the inputs and outputs have the same height and width. Each element in the output is derived from a linear combination of elements at the same position in the input image. You could think of the 1×1 convolutional layer as constituting a fully connected layer applied at every single pixel location to transform the ci corresponding input values into co output values. Because this is still a convolutional layer, the weights are tied across pixel location. Thus the 1×1 convolutional layer requires co×ci weights (plus the bias). Also note that convolutional layers are typically followed by nonlinearities. This ensures that 1×1 convolutions cannot simply be folded into other convolutions.
英語のヒント
図7.4.2は、3個の入力チャネルと2個の出力チャネルをもつ1×1の畳み込みカーネルを使った相互相関の計算を示している。入力と出力は同じ高さと幅をもつことに注意してほしい。出力の各要素は、入力画像の同じ位置にある要素の線形結合から導かれる。1×1の畳み込み層は、画素の位置ごとに当てはめて、対応するci個の入力値をco個の出力値に変換する全結合層を構成していると考えられる。これはやはり畳み込み層なので、重みは画素の位置をまたいで共有されている。したがって、1×1の畳み込み層にはco×ci個の重み(とバイアス)が必要である。また、畳み込み層の後にはふつう非線形性が続くことにも注意してほしい。これにより、1×1の畳み込みを単純にほかの畳み込みに畳み込んでしまうことはできなくなる。
Fig. 7.4.2図7.4.2
The cross-correlation computation uses the 1×1 convolution kernel with three input channels and two output channels. The input and output have the same height and width.
英語のヒント
三つの入力チャネルと二つの出力チャネルをもつ1×1の畳み込みカーネルを使った相互相関の計算。入力と出力は同じ高さと幅をもつ。
Let’s check whether this works in practice: we implement a 1×1 convolution using a fully connected layer. The only thing is that we need to make some adjustments to the data shape before and after the matrix multiplication.
英語のヒント
これが実際にうまくいくかを確かめよう。全結合層を使って1×1の畳み込みを実装する。ただ一つ、行列の掛け算の前後でデータの形状を少し調整する必要がある。
def corr2d_multi_in_out_1x1(X, K):
    c_i, h, w = X.shape
    c_o = K.shape[0]
    X = X.reshape((c_i, h * w))
    K = K.reshape((c_o, c_i))
    # Matrix multiplication in the fully connected layer
    Y = torch.matmul(K, X)
    return Y.reshape((c_o, h, w))
When performing 1×1 convolutions, the above function is equivalent to the previously implemented cross-correlation function corr2d_multi_in_out. Let’s check this with some sample data.
英語のヒント
1×1の畳み込みを行うとき、上の関数は、前に実装した相互相関の関数corr2d_multi_in_outと同等である。いくつかの標本データで確かめてみよう。
X = torch.normal(0, 1, (3, 3, 3))
K = torch.normal(0, 1, (2, 3, 1, 1))
Y1 = corr2d_multi_in_out_1x1(X, K)
Y2 = corr2d_multi_in_out(X, K)
assert float(torch.abs(Y1 - Y2).sum()) < 1e-6