プリンキピア

第14章コンピュータビジョン

14.10.3. Connection to Matrix Transposition14.10.3 行列の転置とのつながり

The transposed convolution is named after the matrix transposition. To explain, let’s first see how to implement convolutions using matrix multiplications. In the example below, we define a 3×3 input X and a 2×2 convolution kernel K, and then use the corr2d function to compute the convolution output Y.
英語のヒント
転置畳み込みは、行列の転置にちなんで名づけられている。説明のため、まず行列の掛け算を使って畳み込みをどう実装するかを見よう。下の例では、3×3の入力Xと2×2の畳み込みカーネルKを定義し、それからcorr2d関数を使って畳み込みの出力Yを計算する。
X = torch.arange(9.0).reshape(3, 3)
K = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
Y = d2l.corr2d(X, K)
Y
tensor([[27., 37.],
        [57., 67.]])
Next, we rewrite the convolution kernel K as a sparse weight matrix W containing a lot of zeros. The shape of the weight matrix is (4, 9), where the non-zero elements come from the convolution kernel K.
英語のヒント
次に、畳み込みカーネルKを、ゼロを多く含む疎な重み行列Wとして書き直す。重み行列の形状は(4, 9)であり、ゼロでない要素は畳み込みカーネルKから来る。
def kernel2matrix(K):
    k, W = torch.zeros(5), torch.zeros((4, 9))
    k[:2], k[3:5] = K[0, :], K[1, :]
    W[0, :5], W[1, 1:6], W[2, 3:8], W[3, 4:] = k, k, k, k
    return W

W = kernel2matrix(K)
W
tensor([[1., 2., 0., 3., 4., 0., 0., 0., 0.],
        [0., 1., 2., 0., 3., 4., 0., 0., 0.],
        [0., 0., 0., 1., 2., 0., 3., 4., 0.],
        [0., 0., 0., 0., 1., 2., 0., 3., 4.]])
Concatenate the input X row by row to get a vector of length 9. Then the matrix multiplication of W and the vectorized X gives a vector of length 4. After reshaping it, we can obtain the same result Y from the original convolution operation above: we just implemented convolutions using matrix multiplications.
英語のヒント
入力Xを行ごとに連結して、長さ9のベクトルを得る。すると、Wとベクトル化したXの行列の掛け算により、長さ4のベクトルが得られる。それを形状を変えると、上の元の畳み込みの操作と同じ結果Yが得られる。いま、行列の掛け算を使って畳み込みを実装したのである。
Y == torch.matmul(W, X.reshape(-1)).reshape(2, 2)
tensor([[True, True],
        [True, True]])
Likewise, we can implement transposed convolutions using matrix multiplications. In the following example, we take the 2×2 output Y from the above regular convolution as input to the transposed convolution. To implement this operation by multiplying matrices, we only need to transpose the weight matrix W with the new shape (9,4).
英語のヒント
同じように、行列の掛け算を使って転置畳み込みを実装できる。次の例では、上の通常の畳み込みの2×2の出力Yを、転置畳み込みの入力とする。この操作を行列の掛け算で実装するには、重み行列Wを新しい形状(9,4)に転置するだけでよい。
Z = trans_conv(Y, K)
Z == torch.matmul(W.T, Y.reshape(-1)).reshape(3, 3)
tensor([[True, True, True],
        [True, True, True],
        [True, True, True]])
Consider implementing the convolution by multiplying matrices. Given an input vector 𝐱 and a weight matrix 𝐖, the forward propagation function of the convolution can be implemented by multiplying its input with the weight matrix and outputting a vector 𝐲=𝐖𝐱. Since backpropagation follows the chain rule and ∇𝐱𝐲=𝐖⊤, the backpropagation function of the convolution can be implemented by multiplying its input with the transposed weight matrix 𝐖⊤. Therefore, the transposed convolutional layer can just exchange the forward propagation function and the backpropagation function of the convolutional layer: its forward propagation and backpropagation functions multiply their input vector with 𝐖⊤ and 𝐖, respectively.
英語のヒント
行列を掛けて畳み込みを実装することを考えよう。入力ベクトル𝐱と重み行列𝐖が与えられたとき、畳み込みの順伝播の関数は、入力に重み行列を掛けてベクトル𝐲=𝐖𝐱を出力することで実装できる。逆伝播は連鎖律に従い、∇𝐱𝐲=𝐖⊤なので、畳み込みの逆伝播の関数は、入力に転置した重み行列𝐖⊤を掛けることで実装できる。したがって、転置畳み込み層は、畳み込み層の順伝播の関数と逆伝播の関数を入れ替えるだけでよい。その順伝播と逆伝播の関数は、それぞれ入力ベクトルに𝐖⊤と𝐖を掛ける。