Torch Tensor & Input Conflicting: "Tensor Object Is Not Callable"
Due to the code &quot;torch.tensor,&quot; I am getting the error &quot;Tensor object is not callable&quot; when I add &quot;input.&quot; 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 = &quot;In order to&quot;
text = tokenizer.encode(&quot;In order to&quot;)
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(&quot;Pick a Option:&quot;)
z = text0.append(option)
print(z)

Error stacktrace:
TypeError                                 Traceback (most recent call last)

&lt;ipython-input-2-82e8d88e81c1&gt; in &lt;module&gt;()
     25 
     26 
---&gt; 27 option = input(&quot;Pick a Option:&quot;)
     28 z = text0.append(option)
     29 print(z)

TypeError: 'Tensor' object is not callable


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 = &quot;In order to&quot;
text = tokenizer.encode(&quot;In order to&quot;)
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(&quot;Pick a Option:&quot;)
z = text0 + ' ' + option
print(z)

