第16章自然言語処理:応用
16.3.1. One-Dimensional Convolutions16.3.1 1次元の畳み込み
Before introducing the model, let’s see how a one-dimensional convolution works. Bear in mind that it is just a special case of a two-dimensional convolution based on the cross-correlation operation.
英語のヒント
モデルを紹介する前に、1次元の畳み込みがどう働くかを見よう。それは、相互相関の演算にもとづく2次元の畳み込みの特別な場合にすぎないことを覚えておこう。
Fig. 16.3.2図16.3.2
One-dimensional cross-correlation operation. The shaded portions are the first output element as well as the input and kernel tensor elements used for the output computation: .
英語のヒント
1次元の相互相関の演算。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。。
As shown in Fig. 16.3.2, in the one-dimensional case, the convolution window slides from left to right across the input tensor. During sliding, the input subtensor (e.g., and in Fig. 16.3.2) contained in the convolution window at a certain position and the kernel tensor (e.g., and in Fig. 16.3.2) are multiplied elementwise. The sum of these multiplications gives the single scalar value (e.g., in Fig. 16.3.2) at the corresponding position of the output tensor.
英語のヒント
We implement one-dimensional cross-correlation in the following
corr1d function. Given an input tensor X and a kernel tensor K, it returns the output tensor Y.英語のヒント
1次元の相互相関を、次の
corr1d関数で実装する。入力テンソルXとカーネルテンソルKが与えられると、この関数は出力テンソルYを返す。def corr1d(X, K):
w = K.shape[0]
Y = torch.zeros((X.shape[0] - w + 1))
for i in range(Y.shape[0]):
Y[i] = (X[i: i + w] * K).sum()
return YWe can construct the input tensor
X and the kernel tensor K from Fig. 16.3.2 to validate the output of the above one-dimensional cross-correlation implementation.英語のヒント
図16.3.2から入力テンソル
XとカーネルテンソルKを組み立てて、上の1次元の相互相関の実装の出力を確かめられる。X, K = torch.tensor([0, 1, 2, 3, 4, 5, 6]), torch.tensor([1, 2])
corr1d(X, K)tensor([ 2., 5., 8., 11., 14., 17.])For any one-dimensional input with multiple channels, the convolution kernel needs to have the same number of input channels. Then for each channel, perform a cross-correlation operation on the one-dimensional tensor of the input and the one-dimensional tensor of the convolution kernel, summing the results over all the channels to produce the one-dimensional output tensor. Fig. 16.3.3 shows a one-dimensional cross-correlation operation with 3 input channels.
英語のヒント
複数のチャネルをもつ任意の1次元の入力について、畳み込みカーネルは同じ数の入力チャネルをもつ必要がある。それから、各チャネルについて、入力の1次元テンソルと畳み込みカーネルの1次元テンソルに相互相関の演算を行い、すべてのチャネルにわたって結果を足し合わせて、1次元の出力テンソルを作る。図16.3.3は、入力チャネルが3の1次元の相互相関の演算を示している。
Fig. 16.3.3図16.3.3
One-dimensional cross-correlation operation with 3 input channels. The shaded portions are the first output element as well as the input and kernel tensor elements used for the output computation: .
英語のヒント
入力チャネルが3の1次元の相互相関の演算。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。。
We can implement the one-dimensional cross-correlation operation for multiple input channels and validate the results in Fig. 16.3.3.
英語のヒント
複数の入力チャネルに対する1次元の相互相関の演算を実装し、図16.3.3の結果を確かめられる。
def corr1d_multi_in(X, K):
# First, iterate through the 0th dimension (channel dimension) of `X` and
# `K`. Then, add them together
return sum(corr1d(x, k) for x, k in zip(X, K))
X = torch.tensor([[0, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 7],
[2, 3, 4, 5, 6, 7, 8]])
K = torch.tensor([[1, 2], [3, 4], [-1, -3]])
corr1d_multi_in(X, K)tensor([ 2., 8., 14., 20., 26., 32.])Note that multi-input-channel one-dimensional cross-correlations are equivalent to single-input-channel two-dimensional cross-correlations. To illustrate, an equivalent form of the multi-input-channel one-dimensional cross-correlation in Fig. 16.3.3 is the single-input-channel two-dimensional cross-correlation in Fig. 16.3.4, where the height of the convolution kernel has to be the same as that of the input tensor.
英語のヒント
Fig. 16.3.4図16.3.4
Two-dimensional cross-correlation operation with a single input channel. The shaded portions are the first output element as well as the input and kernel tensor elements used for the output computation: .
英語のヒント
入力チャネルが一つの2次元の相互相関の演算。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。。
Both the outputs in Fig. 16.3.2 and Fig. 16.3.3 have only one channel. Same as two-dimensional convolutions with multiple output channels described in Section 7.4.2, we can also specify multiple output channels for one-dimensional convolutions.