【发布时间】:2021-08-06 20:25:05
【问题描述】:
我正在开发字符嵌入,为此,我使用了 1D-CNN(参考:TowardsDataScience 上的文章)。
上面附加的文章考虑了卷积操作的多个内核/滤波器大小。
我的问题是:Pytorch 中有没有一种方法可以直接输入多个内核大小作为参数并得到结果?
【问题讨论】:
标签: python pytorch conv-neural-network
我正在开发字符嵌入,为此,我使用了 1D-CNN(参考:TowardsDataScience 上的文章)。
上面附加的文章考虑了卷积操作的多个内核/滤波器大小。
我的问题是:Pytorch 中有没有一种方法可以直接输入多个内核大小作为参数并得到结果?
【问题讨论】:
标签: python pytorch conv-neural-network
没有一个模块可以接受内核大小列表,但您可以轻松地自己构建一个。您基本上需要创建多个并行作用的卷积块,这是一种方法:
class MultiScaleConv1d(nn.Module):
def __init__(self, in_channels: int, out_channels: int, kernel_sizes: Iterable[int] = (3, 5, 7)):
# iterate the list and create a ModuleList of single Conv1d blocks
self.kernels = nn.ModuleList()
for k in kernel_sizes:
self.kernels.append(nn.Conv1d(in_channels, out_channels, k))
def forward(self, inputs):
# now you can build a single output from the list of convs
out = [module(inputs) for module in self.kernels]
# you can return a list, or even build a single tensor, like so
return torch.cat(out, dim=1)
【讨论】: