Torch Tensor and Input Conflicting

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]
best_probabilities = probabilities[best_indices].tolist()

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

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

Hey, you can ask the tokenizer to return tensor instead of creating it manually. Pass return_tensors="pt" and it’ll return a tensor instead of list

1 Like