[英]What is the algebraic expression for PyTorch's ConvTranspose2d's output shape?
When using PyTorch's ConvTranspose2d as such:当使用 PyTorch 的 ConvTranspose2d 时:
w = 5 # input width
h = 5 # output height
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=k, stride=s, padding=p)
What is the formula for the dimensions of the output in each channel? output各通道尺寸计算公式是什么? I tried a few examples and cannot derive the pattern.
我尝试了一些示例,但无法得出模式。 For some reason adding padding seems to shrink the output size (example starts with 5 x 5 as above):
由于某种原因,添加填充似乎会缩小 output 大小(示例以 5 x 5 开头,如上):
# yields an 11 x 11 image
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=3, stride=2, padding=0)
# yields a 7 x 7 image
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=3, stride=2, padding=2)
Using a larger kernel or stride both increase (expected) but not at the rate that I expected:使用更大的 kernel 或跨步都增加(预期)但不是以我预期的速度:
# yields an 11 x 11 image
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=3, stride=2, padding=0)
# yields a 13 x 13 image
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=5, stride=2, padding=0)
# yields a 15 x 15 image
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=3, stride=3, padding=0)
I'm sure there's a pretty simple math equation involving w, h, k, s, p
but I can't find it in the documentation and I haven't been able to derive it myself.我确信有一个非常简单的数学方程涉及
w, h, k, s, p
但我在文档中找不到它,我自己也无法推导出它。 Normally I wouldn't ask for a math equation, but it completely affects the ability of a CNN to compile and generate the correct size.通常我不会要求数学方程,但它完全影响了 CNN 编译和生成正确大小的能力。 Thanks in advance!
提前致谢!
The formula to calculate ConvTranspose2d
output sizes is mentioned on the documentation page:文档页面上提到了计算
ConvTranspose2d
output 大小的公式:
H_out = (H_in−1)*stride[0] − 2×padding[0] + dilation[0]×(kernel_size[0]−1) + output_padding[0] + 1
H_out = (H_in−1)*stride[0] − 2×padding[0] + dilation[0]×(kernel_size[0]−1) + output_padding[0] + 1
W_out = (Win−1)×stride[1] − 2×padding[1] + dilation[1]×(kernel_size[1]−1) + output_padding[1] + 1
W_out = (Win−1)×stride[1] − 2×padding[1] + dilation[1]×(kernel_size[1]−1) + output_padding[1] + 1
By default, stride=1, padding=0, and output_padding=0.默认情况下,stride=1,padding=0,output_padding=0。
For example, for例如,对于
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=3, stride=2, padding=0)
the H_out
will be H_out
将是
H_out = (5-1)*2 - 2*0 + 1*(3-1) + 0 + 1 = 11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.