How to correcty form a list of convolutions

I am currently trying to form a list of 1d Convolutions in my definition of a neural net of say the same type. If I write something like

class nerual_net(torch.nn.Module):
    def __init__(self, D_in, inp_channels):
        super(nerual_net, self).__init__()
        self.conv1_list = []
        for i in range(inp_channels):
            self.conv1_list.append(torch.nn.Conv1d(1, 6, f, stride=s))

And then I later call n1 = neural_net(D, inp_channels) follwoed by n1.cuda(), I run into the error RuntimeError: Input type (CUDAFloatTensor) and weight type (CPUFloatTensor) should be the same. This doesn’t happen if I explicitly write all the convolution inside the class neural_net.

Even if I am to make all the elements of the list into cuda, the weights are not being updated and the network doesn’t learn anything at all.

What is the correct way to make a list of convolutions in pytorch?

I’m not sure I quite understand what you are aiming at, you probably want to either use the groups and in_channels arguments to Conv1d or use ModuleList instead of the list.

Best regards


@tom. ModuleList was exactly what I was looking for. Thanks.