Torch Tensor & Input Conflicting: "Tensor Object Is Not Callable"

Viewed 669

Due to the code "torch.tensor," I am getting the error "Tensor object is not callable" when I add "input." Does anyone know how I can fix this?

import torch
from torch.nn import functional as F
from transformers import GPT2Tokenizer, GPT2LMHeadModel


tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')

text0 = "In order to"
text = tokenizer.encode("In order to")
input, past = torch.tensor([text]), None


logits, past = model(input, past = past)
logits = logits[0,-1]
probabilities = torch.nn.functional.softmax(logits)
best_logits, best_indices = logits.topk(5)
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
text.append(best_indices[0].item())
best_probabilities = probabilities[best_indices].tolist()

for i in range(5):
    f = ('Generated {}: {}'.format(i, best_words[i]))
    print(f)


option = input("Pick a Option:")
z = text0.append(option)
print(z)

Error stacktrace:

TypeError                                 Traceback (most recent call last)

<ipython-input-2-82e8d88e81c1> in <module>()
     25 
     26 
---> 27 option = input("Pick a Option:")
     28 z = text0.append(option)
     29 print(z)

TypeError: 'Tensor' object is not callable
1 Answers

The problem is that you have already defined a variable with the name input which will be used instead of the input function. Just use a different name for your variable and it will work as expected.

Also a python string does not have an append method.

import torch
from torch.nn import functional as F
from transformers import GPT2Tokenizer, GPT2LMHeadModel


tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')

text0 = "In order to"
text = tokenizer.encode("In order to")
myinput, past = torch.tensor([text]), None


logits, past = model(myinput, past = past)
logits = logits[0,-1]
probabilities = torch.nn.functional.softmax(logits)
best_logits, best_indices = logits.topk(5)
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
text.append(best_indices[0].item())
best_probabilities = probabilities[best_indices].tolist()

for i in range(5):
    f = ('Generated {}: {}'.format(i, best_words[i]))
    print(f)


option = input("Pick a Option:")
z = text0 + ' ' + option
print(z)
Related