MaxUnpool2d¶
- class torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)[source][source]¶
計算
MaxPool2d
的部分反運算。MaxPool2d
並非完全可逆,因為非最大值會遺失。MaxUnpool2d
接受MaxPool2d
的輸出作為輸入,包括最大值的索引,並計算一個部分反運算,其中所有非最大值都會被設為零。注意
當輸入索引具有重複值時,此操作可能會表現出非確定性行為。 參見 https://github.com/pytorch/pytorch/issues/80827 和 重現性 以獲取更多資訊。
注意
MaxPool2d
可以將多個輸入尺寸映射到相同的輸出尺寸。 因此,反運算過程可能會變得不明確。 為了適應這種情況,您可以提供所需的輸出尺寸作為正向呼叫中的額外參數output_size
。 請參閱下面的輸入和範例。- 參數
kernel_size (int 或 tuple) – 最大池化視窗的大小。
stride (int 或 tuple) – 最大池化視窗的步幅。 預設設為
kernel_size
。padding (int 或 tuple) – 添加到輸入的填充。
- 輸入
input: 要反運算的輸入張量
indices: 由
MaxPool2d
給出的索引output_size (optional): 目標輸出尺寸
- 形狀
輸入: 或 .
輸出: 或 ,其中
或者由呼叫運算符中的
output_size
給定
範例
>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True) >>> unpool = nn.MaxUnpool2d(2, stride=2) >>> input = torch.tensor([[[[ 1., 2., 3., 4.], [ 5., 6., 7., 8.], [ 9., 10., 11., 12.], [13., 14., 15., 16.]]]]) >>> output, indices = pool(input) >>> unpool(output, indices) tensor([[[[ 0., 0., 0., 0.], [ 0., 6., 0., 8.], [ 0., 0., 0., 0.], [ 0., 14., 0., 16.]]]]) >>> # Now using output_size to resolve an ambiguous size for the inverse >>> input = torch.tensor([[[[ 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10.], [11., 12., 13., 14., 15.], [16., 17., 18., 19., 20.]]]]) >>> output, indices = pool(input) >>> # This call will not work without specifying output_size >>> unpool(output, indices, output_size=input.size()) tensor([[[[ 0., 0., 0., 0., 0.], [ 0., 7., 0., 9., 0.], [ 0., 0., 0., 0., 0.], [ 0., 17., 0., 19., 0.]]]])