手工计算很容易出错(至少对我自己而言)
我找到的最可靠的方法:
import torch
from torch import nn
import functools
import operator
def shape_of_output(shape_of_input, list_of_layers):
sequential = nn.Sequential(*list_of_layers)
return tuple(sequential(torch.rand(1, *shape_of_input)).shape)
def size_of_output(shape_of_input, list_of_layers):
return functools.reduce(operator.mul, list(shape_of_output(shape_of_input, list_of_layers)))
它只是简单地通过层运行一次输入,然后打印输出的大小。所以这有点浪费,但即使在 pytorch 中添加了新功能/选项,也基本上可以保证是正确的。
示例(复制+粘贴时运行)
#
# example setup
#
import random
out_channel_of_first = random.randint(1,16)
kernel_size_of_first = random.choice([3,5,7,11])
grayscale_image_shape = (1, 48, 48)
color_image_shape = (3, 48, 48) # alternative example
#
# example usage
#
print('the output shape will be', shape_of_output(
shape_of_input=grayscale_image_shape,
list_of_layers=[
nn.Conv2d(
in_channels=grayscale_image_shape[0],
out_channels=out_channel_of_first,
kernel_size=kernel_size_of_first,
),
nn.ReLU(),
nn.MaxPool2d(2,2),
# next major layer
nn.Conv2d(
in_channels=out_channel_of_first,
out_channels=5,
kernel_size=3
),
nn.ReLU(),
nn.MaxPool2d(2,2),
],
))