I am using pytorch for CNN and now I need to use RNN, however, after few days labor, still could not run it successfully. The network looks like below
class Net(nn.Module):
    def __init__(self, input_dim, nb_lstm_units, layer_dim, output_dim, batch_size=20):
        super(Net, self).__init__()
        self.nb_lstm_units = nb_lstm_units
        self.layer_dim = layer_dim
        self.batch_size = batch_size

        self.rnn = nn.RNN(input_dim, nb_lstm_units, layer_dim, batch_first=True, nonlinearity='relu')
        self.fc_out = nn.Linear(nb_lstm_units, output_dim)        
    
    def init_hidden(self):
        hidden_a = torch.randn(self.layer_dim, self.batch_size, self.nb_lstm_units)        
        hidden_a = hidden_a.cuda()            
        return Variable(hidden_a)
        
    def forward(self, X): 
        batch_size, seq_len, _ = X.size()

        # dimension (batch, n_features, timestamp) -> (batch, timestamp, n_features)
        X = X.transpose(1, 2)

        self.hidden = self.init_hidden()
        out, self.hidden = self.rnn(X, self.hidden)

        out = self.fc_out( out[:, -1, :] )
        
        return out


# Create RNN
input_dim = 1
nb_lstm_units = 100
layer_dim = 2
output_dim = 2
    
model = Net(input_dim, nb_lstm_units, layer_dim, output_dim)

Input is 1D ECG signal segmented by 217 samples, batch size 20. The forward is passed and I can print the output of self.fc_out but the error is shown afterward. Hidden output of RNN need extra processing?