简体   繁体   English

如何让 PyTorch Conv1d 在向量上工作?

[英]How can I have a PyTorch Conv1d work over a vector?

I understand Conv1d strides in one dimension.我理解Conv1d在一维上大步前进。 But my input is of shape [64, 20, 161] , where 64 is the batches, 20 is the sequence length and 161 is the dimension of my vector.但是我的输入是[64, 20, 161]形状,其中 64 是批次,20 是序列长度,161 是我的向量的维度。

I'm not sure how to set up my Conv1d to stride over the vector.我不确定如何设置我的Conv1d以跨越向量。

I'm trying:我想:

self.conv1 = torch.nn.Conv1d(batch_size, 20, 161, stride=1)

but getting:但得到:

RuntimeError: Given groups=1, weight of size 20 64 161, expected input[64, 20, 161] to have 64 channels, but got 20 channels instead

According to the documentation :根据文档

torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')

in_channels is the number of channels in your input, number of channels usually an computer vision term, in your case this number is 20. out_channels size of your output, it depends on how much output you want. in_channels 是输入中的通道数,通道数通常是计算机视觉术语,在您的情况下,此数字是 20。 out_channels 输出的大小,这取决于您想要多少输出。

For 1D convolution, you can think of number of channels as "number of input vectors" and "number of output feature vectors".对于一维卷积,您可以将通道数视为“输入向量数”和“输出特征向量数”。 And size (not number) of output feature vectors are decided from other parameters like kernel_size, strike, padding, dilation.输出特征向量的大小(不是数量)由其他参数决定,如 kernel_size、strike、padding、dilation。

An example usage:示例用法:

t = torch.randn((64, 20, 161))
conv = torch.nn.Conv1d(20, 100)
conv(t)

Note: You never specify batch size in torch.nn modules, first dimension is always assumed to be batch size.注意:您永远不会在 torch.nn 模块中指定批量大小,第一个维度始终假定为批量大小。

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

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