プリンキピア

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

7.3. Padding and Stride7.3 パディングとストライド

Recall the example of a convolution in Fig. 7.2.1. The input had both a height and width of 3 and the convolution kernel had both a height and width of 2, yielding an output representation with dimension 2×2. Assuming that the input shape is nh×nw and the convolution kernel shape is kh×kw, the output shape will be (nh−kh+1)×(nw−kw+1): we can only shift the convolution kernel so far until it runs out of pixels to apply the convolution to.
英語のヒント
図7.2.1の畳み込みの例を思い出してほしい。入力は高さも幅も3で、畳み込みカーネルは高さも幅も2だったので、次元が2×2の出力表現が得られた。入力の形状がnh×nwで、畳み込みカーネルの形状がkh×kwだとすると、出力の形状は(nh−kh+1)×(nw−kw+1)になる。畳み込みを当てはめる画素がなくなるまでしか、畳み込みカーネルをずらせないのである。
In the following we will explore a number of techniques, including padding and strided convolutions, that offer more control over the size of the output. As motivation, note that since kernels generally have width and height greater than 1, after applying many successive convolutions, we tend to wind up with outputs that are considerably smaller than our input. If we start with a 240×240 pixel image, ten layers of 5×5 convolutions reduce the image to 200×200 pixels, slicing off 30% of the image and with it obliterating any interesting information on the boundaries of the original image. Padding is the most popular tool for handling this issue. In other cases, we may want to reduce the dimensionality drastically, e.g., if we find the original input resolution to be unwieldy. Strided convolutions are a popular technique that can help in these instances.
英語のヒント
以下では、パディングやストライド付きの畳み込みなど、出力の大きさをもっと制御できるいくつかの手法を探っていく。動機として、カーネルの幅と高さはふつう1より大きいので、畳み込みを何回も続けて当てはめると、入力よりかなり小さい出力に行き着きがちであることに注意してほしい。240×240画素の画像から始めると、5×5の畳み込みを10層重ねれば画像は200×200画素に縮み、画像の30%が切り落とされて、それとともに元の画像の境界にあるおもしろい情報もすべて消し去られる。パディングは、この問題を扱うための最も人気のある道具である。別の場合には、たとえば元の入力の解像度が扱いにくいとわかったときなど、次元を大幅に減らしたいこともある。ストライド付きの畳み込みは、そうした場合に役立つ人気の手法である。
import torch
from torch import nn