How to build my own modules(change cnn network structure)?

    I want to build a model like that. But I don't know how to use PyTorch basic modules to do that. I plan to train the  horizontal  parameters(CNN) at first. Then,train the vertical parameters(RNN). But,I don't know how to combine them together and train this.

This model looks like a Stacked Convolutional Recurrent Neural Network.

I couldn’t find an implementation of this in PyTorch but it should be easily done. If you see cell 8 in this iPython Notebook you have:

import torch.nn as nn
from torch.autograd import Variable

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
        self.i2o = nn.Linear(input_size + hidden_size, output_size)
        self.softmax = nn.LogSoftmax()
    def forward(self, input, hidden):
        combined =, hidden), 1)
        hidden = self.i2h(combined)
        output = self.i2o(combined)
        output = self.softmax(output)
        return output, hidden

    def initHidden(self):
        return Variable(torch.zeros(1, self.hidden_size))

n_hidden = 128
rnn = RNN(n_letters, n_hidden, n_categories)

Here the i2h and i2o are Linear layers. You’d have to change them to be Convolutional Layers to get a ConvRNN (or if you apply the LSTM update equation a ConvLSTM)

1 Like

Thank you! I’ll try.