Pytorch的“折叠"效果如何和“展开"工作? [英] How does Pytorch's "Fold" and "Unfold" work?
问题描述
我已经阅读了官方文档.我很难理解此功能的用途以及它的工作方式.有人可以用Layman的方式解释吗?
I've gone through the official doc. I'm having a hard time understanding what this function is used for and how it works. Can someone explain this in Layman terms?
尽管我使用的Pytorch版本与文档匹配,但我提供的示例却出现错误.也许修复错误(我所做的错误)应该教给我一些东西?文档中给出的代码段为:
I get an error for the example they provide, although the Pytorch version I'm using matches the documentation. Perhaps fixing the error, which I did, is supposed to teach me something? The snippet given in the documentation is:
fold = nn.Fold(output_size=(4, 5), kernel_size=(2, 2))
input = torch.randn(1, 3 * 2 * 2, 1)
output = fold(input)
output.size()
,固定的代码段是:
fold = nn.Fold(output_size=(4, 5), kernel_size=(2, 2))
input = torch.randn(1, 3 * 2 * 2, 3 * 2 * 2)
output = fold(input)
output.size()
谢谢!
推荐答案
unfold
和 fold
用于促进滑动窗口"操作(如卷积).
假设您要将功能foo
应用于要素地图/图像中的每个5x5窗口:
unfold
and fold
are used to facilitate "sliding window" operation (like convolutions).
Suppose you want to apply a function foo
to every 5x5 window in a feature map/image:
from torch.nn import functional as f
windows = f.unfold(x, kernel_size=5)
现在windows
具有批处理-(5 * 5 * x.size(1)
)-num_windows的size
,您可以在windows
上应用foo
:
Now windows
has size
of batch-(5*5*x.size(1)
)-num_windows, you can apply foo
on windows
:
processed = foo(windows)
现在,您需要将"processed
"折回"到x
的原始大小:
Now you need to "fold" processed
back to the original size of x
:
out = f.fold(processed, x.shape[-2:], kernel_size=5)
您需要注意padding
和kernel_size
可能会影响您将processed
折回x
大小的能力.
此外,fold
求和在重叠的元素上,因此您可能想将fold
的输出除以补丁大小.
You need to take care of padding
, and kernel_size
that may affect your ability to "fold" back processed
to the size of x
.
Moreover, fold
sums over overlapping elements, so you might want to divide the output of fold
by patch size.
这篇关于Pytorch的“折叠"效果如何和“展开"工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!