プリンキピア

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

7.3.2. Stride7.3.2 ストライド

When computing the cross-correlation, we start with the convolution window at the upper-left corner of the input tensor, and then slide it over all locations both down and to the right. In the previous examples, we defaulted to sliding one element at a time. However, sometimes, either for computational efficiency or because we wish to downsample, we move our window more than one element at a time, skipping the intermediate locations. This is particularly useful if the convolution kernel is large since it captures a large area of the underlying image.
英語のヒント
相互相関を計算するときは、畳み込みの窓を入力テンソルの左上の隅に置いて始め、それから下へも右へも、すべての位置にわたってすべらせていく。前の例では、既定として一度に一つの要素ずつすべらせた。しかし、ときには、計算の効率のため、あるいはダウンサンプリングしたいために、一度に二つ以上の要素ずつ窓を動かし、途中の位置を飛ばすこともある。これは、畳み込みカーネルが大きいときに特に役立つ。カーネルが下にある画像の広い範囲をとらえるからである。
We refer to the number of rows and columns traversed per slide as stride. So far, we have used strides of 1, both for height and width. Sometimes, we may want to use a larger stride. Fig. 7.3.3 shows a two-dimensional cross-correlation operation with a stride of 3 vertically and 2 horizontally. The shaded portions are the output elements as well as the input and kernel tensor elements used for the output computation: 0×0+0×1+1×2+2×3=8, 0×0+6×1+0×2+0×3=6. We can see that when the second element of the first column is generated, the convolution window slides down three rows. The convolution window slides two columns to the right when the second element of the first row is generated. When the convolution window continues to slide two columns to the right on the input, there is no output because the input element cannot fill the window (unless we add another column of padding).
英語のヒント
1回すべらせるごとに通り過ぎる行と列の数を、ストライドと呼ぶ。ここまでは、高さにも幅にもストライド1を使ってきた。ときには、もっと大きなストライドを使いたいこともある。図7.3.3は、縦にストライド3、横にストライド2の2次元の相互相関の演算を示している。網かけの部分は、出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。0×0+0×1+1×2+2×3=8、0×0+6×1+0×2+0×3=6。1列目の2番目の要素が生成されるとき、畳み込みの窓が3行下にすべることがわかる。1行目の2番目の要素が生成されるときには、畳み込みの窓は2列右にすべる。畳み込みの窓が入力の上をさらに2列右にすべり続けると、入力の要素が窓を埋められないので、出力はない(パディングの列をもう一つ加えないかぎり)。
Fig. 7.3.3図7.3.3
Cross-correlation with strides of 3 and 2 for height and width, respectively.
英語のヒント
高さと幅のストライドがそれぞれ3と2の相互相関。
In general, when the stride for the height is sh and the stride for the width is sw, the output shape is
英語のヒント
一般に、高さのストライドがsh、幅のストライドがswのとき、出力の形状は次のようになる。
⌊(nh−kh+ph+sh)/sh⌋×⌊(nw−kw+pw+sw)/sw⌋.(7.3.2)
If we set ph=kh−1 and pw=kw−1, then the output shape can be simplified to ⌊(nh+sh−1)/sh⌋×⌊(nw+sw−1)/sw⌋. Going a step further, if the input height and width are divisible by the strides on the height and width, then the output shape will be (nh/sh)×(nw/sw).
英語のヒント
ph=kh−1とpw=kw−1に設定すれば、出力の形状は⌊(nh+sh−1)/sh⌋×⌊(nw+sw−1)/sw⌋と簡単にできる。さらに一歩進んで、入力の高さと幅が、高さと幅のストライドで割り切れるなら、出力の形状は(nh/sh)×(nw/sw)になる。
Below, we set the strides on both the height and width to 2, thus halving the input height and width.
英語のヒント
下では、高さと幅の両方のストライドを2にして、入力の高さと幅を半分にする。
conv2d = nn.LazyConv2d(1, kernel_size=3, padding=1, stride=2)
comp_conv2d(conv2d, X).shape
torch.Size([4, 4])
Let’s look at a slightly more complicated example.
英語のヒント
もう少し複雑な例を見てみよう。
conv2d = nn.LazyConv2d(1, kernel_size=(3, 5), padding=(0, 1), stride=(3, 4))
comp_conv2d(conv2d, X).shape
torch.Size([2, 2])