プリンキピア

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

7.5.2. Padding and Stride7.5.2 パディングとストライド

As with convolutional layers, pooling layers change the output shape. And as before, we can adjust the operation to achieve a desired output shape by padding the input and adjusting the stride. We can demonstrate the use of padding and strides in pooling layers via the built-in two-dimensional max-pooling layer from the deep learning framework. We first construct an input tensor X whose shape has four dimensions, where the number of examples (batch size) and number of channels are both 1.
英語のヒント
畳み込み層と同じく、プーリング層も出力の形状を変える。そして前と同じく、入力をパディングしてストライドを調整することで、望む出力の形状を得るよう演算を調整できる。深層学習のフレームワークに組み込まれた2次元の最大プーリング層を使って、プーリング層でのパディングとストライドの使い方を示そう。まず、形状が4次元で、事例の数(バッチサイズ)とチャネルの数がどちらも1である入力テンソルXを作る。
X = torch.arange(16, dtype=torch.float32).reshape((1, 1, 4, 4))
X
tensor([[[[ 0.,  1.,  2.,  3.],
          [ 4.,  5.,  6.,  7.],
          [ 8.,  9., 10., 11.],
          [12., 13., 14., 15.]]]])
Since pooling aggregates information from an area, deep learning frameworks default to matching pooling window sizes and stride. For instance, if we use a pooling window of shape (3, 3) we get a stride shape of (3, 3) by default.
英語のヒント
プーリングは一つの範囲から情報を集約するので、深層学習のフレームワークは既定でプーリングの窓の大きさとストライドを一致させる。たとえば、形状(3, 3)のプーリングの窓を使うと、既定ではストライドの形状は(3, 3)になる。
pool2d = nn.MaxPool2d(3)
# Pooling has no model parameters, hence it needs no initialization
pool2d(X)
tensor([[[[10.]]]])
Needless to say, the stride and padding can be manually specified to override framework defaults if required.
英語のヒント
言うまでもなく、必要なら、ストライドとパディングを手作業で指定して、フレームワークの既定を上書きできる。
pool2d = nn.MaxPool2d(3, padding=1, stride=2)
pool2d(X)
tensor([[[[ 5.,  7.],
          [13., 15.]]]])
Of course, we can specify an arbitrary rectangular pooling window with arbitrary height and width respectively, as the example below shows.
英語のヒント
もちろん、下の例が示すように、高さと幅がそれぞれ任意の、任意の長方形のプーリングの窓を指定することもできる。
pool2d = nn.MaxPool2d((2, 3), stride=(2, 3), padding=(0, 1))
pool2d(X)
tensor([[[[ 5.,  7.],
          [13., 15.]]]])