LSTM TypeError: 'int' object is not callable

Joey3020: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/2/228620325eae1ffc523fa1671c66372c8ae2d940.png" data-download-href="https://discuss.pytorch.org/uploads/default/228620325eae1ffc523fa1671c66372c8ae2d940" title="03%20PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/228620325eae1ffc523fa1671c66372c8ae2d940_2_690x308.png" alt="03%20PM" data-base62-sha1="4VpB1mAZ3yRzOn8AwP1ySKKVmBq" width="690" height="308" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/228620325eae1ffc523fa1671c66372c8ae2d940_2_690x308.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/228620325eae1ffc523fa1671c66372c8ae2d940_2_1035x462.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/228620325eae1ffc523fa1671c66372c8ae2d940_2_1380x616.png 2x" data-dominant-color="EBECEC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">03%20PM</span><span class="informations">2034×908 155 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/f/afb1c9ce641d815ed7c962bcd8e5c969ff03416a.png" data-download-href="https://discuss.pytorch.org/uploads/default/afb1c9ce641d815ed7c962bcd8e5c969ff03416a" title="58%20PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afb1c9ce641d815ed7c962bcd8e5c969ff03416a_2_690x371.png" alt="58%20PM" data-base62-sha1="p4gsXd5QMyczhlBfrTlZQIjKORc" width="690" height="371" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afb1c9ce641d815ed7c962bcd8e5c969ff03416a_2_690x371.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afb1c9ce641d815ed7c962bcd8e5c969ff03416a_2_1035x556.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afb1c9ce641d815ed7c962bcd8e5c969ff03416a_2_1380x742.png 2x" data-dominant-color="F4F7F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">58%20PM</span><span class="informations">2028×1092 347 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>As far as I know, int object is not callable is an error when you use a function name already assigned to a variable.</p>
<p>However, I cannot find any aliasing in my code</p>
<p>What could have been possibly gone wrong???</p>

ptrblck: <p>Are you passing a tensor to the model or some other object?<br>
Could you post a small, executable code snippet so that we could have a look?</p>

Joey3020: <p>import pandas as pd<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
import numpy as np<br>
from torch.utils.data import Dataset, DataLoader<br>
import time<br>
import math</p>
<p>USE_GPU = False<br>
if USE_GPU and torch.cuda.is_available():<br>
device = torch.device(‘cuda’)<br>
else:<br>
device = torch.device(‘cpu’)</p>
<p>vocab = open(‘vocab.txt’).read().splitlines()<br>
n_vocab = len(vocab)<br>
torch.manual_seed(1)</p>
<p>def text2int(csv_file, dname, vocab):<br>
ret = []<br>
data = csv_file[dname].values<br>
for datum in data:<br>
for char in str(datum):<br>
idx = vocab.index(char)<br>
ret.append(idx)<br>
ret = np.array(ret)<br>
return ret</p>
<p>class NewsDataset(Dataset):<br>
def <strong>init</strong>(self, csv_file, vocab):<br>
self.csv_file = pd.read_csv(csv_file, sep=’|’)<br>
self.vocab = vocab<br>
self.len = len(self.csv_file)<br>
self.x_data = torch.tensor(text2int(self.csv_file, ‘x_data’, self.vocab))<br>
self.y_data = torch.tensor(text2int(self.csv_file, ‘y_data’, self.vocab))<br>
def <strong>len</strong>(self):<br>
return self.len<br>
def <strong>getitem</strong>(self, idx):<br>
return self.x_data[idx], self.y_data[idx]</p>
<p>dataset = NewsDataset(csv_file = ‘data.csv’, vocab = vocab)<br>
train_loader = DataLoader(dataset=dataset,<br>
batch_size=64,<br>
shuffle=False,<br>
num_workers=1)</p>
<p>from torch.autograd import Variable<br>
import numpy as np</p>
<p>class selfModule(nn.Module):<br>
def <strong>init</strong>(self, inputdim, hiddendim, batchsize, outputdim, numlayers):<br>
super(selfModule, self).<strong>init</strong>()<br>
self.inputdim = inputdim<br>
self.hiddendim = hiddendim<br>
self.numlayers = numlayers<br>
self.outputdim = outputdim<br>
self.batchsize = batchsize<br>
self.lstm = nn.LSTM(self.inputdim, self.hiddendim, self.numlayers, bias=True, batch_first=False, bidirectional=False)<br>
self.fc = nn.Linear(self.hiddendim, self.outputdim)<br>
def forward(self, input):<br>
onehot_input = np.zeros((len(input), 86)) <span class="hashtag">#86</span> = dictionary size<br>
onehot_input[np.arange(len(input)), input] = 1<br>
lstm_out, self.hidden = self.lstm(onehot_input)<br>
prediction = self.fc(lstm_out[-1].view(self.batchsize, -1))<br>
return prediction.view(-1)<br>
def init_hidden_cell(self):<br>
return (torch.zeros(self.numlayers, self.batchsize, self.hiddendim), torch.zeros(self.numlayers, self.batchsize, self.hiddendim))</p>
<p>def timeSince(since):<br>
now = time.time()<br>
s = now - since<br>
m = math.floor(s / 60)<br>
s -= m * 60<br>
return ‘%dm %ds’ % (m, s)</p>
<p>def train(dataset, model, optimizer, n_iters):<br>
model.to(device=device)<br>
model.train()<br>
start = time.time()<br>
print_every = 50<br>
for e in range(n_iters):<br>
model.hidden = model.init_hidden_cell()<br>
for i, (x, y) in enumerate(dataset):<br>
x = x.to(device=device)<br>
y = y.to(device=device)<br>
model.zero_grad()<br>
output = model(x) #####<br>
loss = loss_fcn(output, y)<br>
loss.backward()<br>
optimizer.step()<br>
if e % print_every == 0:<br>
print(’%s (%d %d%%) %.4f’ % (timeSince(start), e, e / n_iters * 100, loss))</p>
<p>def test(start_letter):<br>
max_length = 1000<br>
with torch.no_grad():<br>
idx = vocab.index(start_letter)<br>
input_nparray = [idx]<br>
input_nparray = np.reshape(input_nparray, (1, len(input_nparray)))<br>
inputs = torch.tensor(input_nparray, device=device, dtype=torch.long)<br>
output_sen = start_letter<br>
for i in range(max_length):<br>
output = model(inputs)<br>
topv, topi = output.topk(1)<br>
topi = topi[-1]<br>
letter = vocab[topi]<br>
output_sen += letter<br>
idx = vocab.index(letter)<br>
input_nparray = np.append(input_nparray, [idx])<br>
inputs = torch.tensor(input_nparray, device=device, dtype=torch.long)<br>
return output_sen</p>
<p>print(‘using device:’, device)<br>
inputdim = 86<br>
hiddendim = 100<br>
batchsize = 64<br>
outputdim = 86<br>
numlayers = 128<br>
loss_fcn = nn.NLLLoss()<br>
model = selfModule(inputdim, hiddendim, batchsize, outputdim, numlayers)</p>
<p>do_restore = False</p>
<p>if do_restore:<br>
model.load_state_dict(torch.load(‘fng_pt.pt’))<br>
model.eval()<br>
model.to(device=device)<br>
else:<br>
n_iters = 500<br>
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999), eps=2e-16, weight_decay=0)<br>
train(train_loader, model, optimizer, n_iters)<br>
torch.save(model.state_dict(), ‘fng_pt.pt’)</p>
<p>print(test(‘W’))</p>

Joey3020: <p>here is all my code!!<br>
I think I’m passing a tensor to the model but I’m not sure.</p>

Sam_Shamsan: <p>I am having the same exact error, I am passing numpy.ndarray to the model after successfully train it. Can anybody help diagnose this please.</p>
<p>/<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/f/ef721436c7a7ac9b8c8e0bc2c3ee912e51943586.png" data-download-href="https://discuss.pytorch.org/uploads/default/ef721436c7a7ac9b8c8e0bc2c3ee912e51943586" title="Screen Shot 2020-11-18 at 2.06.06 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/f/ef721436c7a7ac9b8c8e0bc2c3ee912e51943586_2_690x350.png" alt="Screen Shot 2020-11-18 at 2.06.06 PM" data-base62-sha1="yaewLutRE7GUnDFKyECnwjUlJMW" width="690" height="350" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/f/ef721436c7a7ac9b8c8e0bc2c3ee912e51943586_2_690x350.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/f/ef721436c7a7ac9b8c8e0bc2c3ee912e51943586_2_1035x525.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/f/ef721436c7a7ac9b8c8e0bc2c3ee912e51943586_2_1380x700.png 2x" data-dominant-color="F7F7F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-11-18 at 2.06.06 PM</span><span class="informations">2188×1112 261 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Pass a PyTorch tensor to the model, since the <code>.size</code> returns an <code>int</code> in numpy while it’s a function in PyTorch.<br>
You can convert a numpy array to a tensor via <code>tensor = torch.from_numpy(array)</code>.</p>