0

Given a batch of samples, I would like to convolve each of them with different filters. I have implemented the idea with keras and the code works:

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)

Is there any way to do this with pytorch?

zhf061
  • 311
  • 2
  • 7

1 Answers1

0

You can try this

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)

You can convolve it with whatever filter you want.

  • Thanks for the reply. But what I demand is that each sample can be convolved with a specific filter. For example, given 10 samples, there should be 10 filters, and each of them takes care of a sample. – zhf061 Nov 13 '20 at 01:50