Pytorch 的“Fold"是如何实现的?和“展开"工作? [英] How does Pytorch's "Fold" and "Unfold" work?

查看:17
本文介绍了Pytorch 的“Fold"是如何实现的?和“展开"工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了官方文档.我很难理解这个函数的用途以及它是如何工作的.有人可以用外行的方式解释吗?

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()

谢谢!

推荐答案

unfoldfold 用于促进滑动窗口"操作(如卷积).
假设您想将函数 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)

现在windowssize的batch-(5*5*x.size(1))-num_windows,可以申请<windows 上的 code>foo:

Now windows has size of batch-(5*5*x.size(1))-num_windows, you can apply foo on windows:

processed = foo(windows)

现在你需要将处理过的折叠"回x的原始大小:

Now you need to "fold" processed back to the original size of x:

out = f.fold(processed, x.shape[-2:], kernel_size=5)

您需要注意 paddingkernel_size 可能会影响您将 processed 折叠"回 processed 大小的能力代码>x.
此外,fold sums 在重叠元素上,因此您可能希望将 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 的“Fold"是如何实现的?和“展开"工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆