简体   繁体   English

如何使用 nn.conv2d 做与 nn.conv1d 一样的 function?

[英]How to use nn.conv2d to do the same function as nn.conv1d?

I'm working on deploying python CNNs on FPGAs, and i'm facing an issue.我正在努力在 FPGA 上部署 python CNN,我遇到了一个问题。

I have a 1d CNN which uses torch.conv1d layer function, and it's not supported by the software I'm using: vitis-ai 1.3 .我有一个使用torch.conv1d层 function 的 1d CNN,我正在使用的软件不支持它: vitis-ai 1.3

I'm figuring out if there is some way to use nn.conv2d instead to do the same job as nn.conv1d ?我正在弄清楚是否有某种方法可以使用nn.conv2d来代替nn.conv1d做同样的工作?

for example:例如:

Conv1d(1, 32, 3, bias = True) => Conv2d(... 

Is there a way to do that?有没有办法做到这一点? (without loosing too much performance?) (不会损失太多性能?)

Thank you.谢谢你。

Given 1d input, a 2d convolution of kernel-size (n,1) is equivalent to a 1d convolution of kernel-size (n,) :给定一维输入,内核大小(n,1)的二维卷积等效于内核大小(n,)的一维卷积:

nn.Conv1d(in_channels, out_channels, n)

# for 1d input, is equivalent to:
nn.Conv2d(in_channels, out_channels, (n,1))

Note you will have to unsqueeze(-1) the input to get it of the correct shape for a 2d convolution, but you can return the output to the original shape by applying squeeze(-1) :请注意,您必须unsqueeze(-1)输入以使其具有正确的 2d 卷积形状,但您可以通过应用 Squeeze squeeze(-1)将 output 恢复为原始形状:

F.conv1d(inputs, filters)

# is equivalent to:
F.conv2d(inputs.unsqueeze(-1), filters.unsqueeze(-1)).squeeze(-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM