MaxUnpool2d¶
- class torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)[原始碼][原始碼]¶
計算
MaxPool2d的部分逆運算。MaxPool2d不是完全可逆的,因為非最大值會丟失。MaxUnpool2d將MaxPool2d的輸出(包括最大值的索引)作為輸入,並計算一個部分逆運算,其中所有非最大值都被設定為零。注意
當輸入索引包含重複值時,此操作可能會表現出不確定性行為。有關更多資訊,請參閱 https://github.com/pytorch/pytorch/issues/80827 和 可復現性。
注意
MaxPool2d可以將多個輸入大小對映到相同的輸出大小。因此,逆向過程可能會產生歧義。為了解決這個問題,您可以在前向呼叫中將所需的輸出大小作為額外的引數output_size提供。請參閱下面的輸入和示例。- 引數
- 輸入
input: 要進行逆運算的輸入 Tensor
indices: 由
MaxPool2d輸出的索引output_size (可選): 目標輸出大小
- 形狀
輸入形狀: 或 。
輸出形狀: 或 ,其中
或者由呼叫運算子中的
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.]]]])