给出一批样本,我想用不同的过滤器对每个样本进行卷积。我已经用keras实现了这个想法,并且代码有效:
import keras.backend as K
def single_conv(tupl):
inp, kernel = tupl
outputs = K.conv1d(inp, kernel, padding='same')
return outputs
# inputs and filters are given in some way
res = K.squeeze(K.map_fn(single_conv, (inputs, filters), dtype=K.floatx()), axis=1)
有什么方法可以用pytorch做到吗?
答案 0 :(得分:0)
您可以尝试
import torch.nn as nn
import torch
conv2d = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
inp = torch.ones((1, 3, 5, 5))
conv2d.weight = nn.Parameter(torch.ones((3, 3, 3, 3))) # You can set anything you want.
model = nn.Sequential(conv2d)
res = model(inp)
print(res.shape)
# print(res)
您可以将其与所需的任何过滤器进行卷积。