'int' object is not callable in RNN

This is my code

import torch
import torchvision
from torch import nn
import numpy as np
from torch import optim

num_layers = 2
batch_size = 1
no_hidden_units = 128
input_size = 1
output_size = 1
sequence_length = 21
num_epochs = 2

x = np.linspace(0,20,21)
y = np.linspace(1,21,21)
print(x)
print(y)

class Lstm(nn.Module):
def init(self , input_size , no_hidden_units , output_size , num_layers):
super(Lstm , self).init()
self.no_hidden_units = no_hidden_units
self.num_layers = num_layers
self.lstm = nn.RNN(input_size , self.no_hidden_units ,
self.num_layers , batch_first = True)
self.fc = nn.Linear(self.no_hidden_units , output_size)

def forward(self , x , h_state):
    out = self.lstm(x , h_state)
    out = out.reshape(-1,self.no_hidden_units)
    # or out = out[:,-1,:]
    output = self.fc(out)
    print(output.shape)
    output = output.reshape(-1,sequence_length,1)
    print(output.shape)
    return output , h_state

lst = Lstm(input_size , no_hidden_units , output_size , num_layers)
h_state = torch.zeros(num_layers , batch_size , no_hidden_units)
print(lst)

criterion = nn.MSELoss()
optimizer = optim.Adam(lst.parameters() , lr = 0.001)

for i in range(num_epochs):
xin = x.reshape(batch_size , sequence_length , input_size)
y = y.reshape(batch_size , sequence_length , input_size)
print(xin.shape)

optimizer.zero_grad()
logits , _ = lst(xin ,h_state)
loss = criterion(logits , y)
print(loss)
loss.backward()
optimizer.step()

TypeError Traceback (most recent call last)

in ()
5
6 optimizer.zero_grad()
----> 7 logits , _ = lst(xin ,h_state)
8 loss = criterion(logits , y)
9 print(loss)

3 frames

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in forward(self, input, hx)
191 else:
192 batch_sizes = None
–> 193 max_batch_size = input.size(0) if self.batch_first else input.size(1)
194 sorted_indices = None
195 unsorted_indices = None

TypeError: ‘int’ object is not callable

May be x and y should be Pytorch tensors, not Numpy arrays

Hi, have you solve the problem? I have stuck with the same one…