pytorch-sentiment-analysis-classification
pytorch-sentiment-analysis-classification copied to clipboard
predict analysis
I added this code to try predict a sentence:
`def predict_sentiment(model, sentence, dataset):
nlp = spacy.load('en_core_web_sm')
dataset = MyDataset(batch_size=1)
tokenized = [tok.text for tok in nlp.tokenizer(sentence)]
indexed = [dataset.TEXT.vocab.stoi[t] for t in tokenized]
tensor = torch.LongTensor(indexed).to(device)
tensor = tensor.unsqueeze(1)
prediction = F.sigmoid(model(tensor))
return prediction.item()`
`if name == 'main':
args = parse_args()
device = torch.device('cuda' if torch.cuda.is_available() and args.cuda else 'cpu')
dataset = MyDataset(batch_size=args.batch_size, use_vector=args.word_vector, pdevice = device)
print(args)
model = main(args, dataset, device)
print(predict_sentiment(model, " smart , provocative and blisteringly funny", dataset))`
but I got the error
File "C:\Users\roifo\PycharmProjects\pytorch-sentiment-analysis-classification\model.py", line 101, in attention merged_state = merged_state.squeeze(0).unsqueeze(2) IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
I tried to change the line
tensor = tensor.unsqueeze(1)
to tensor = tensor.unsqueeze(0)
but then the error was
warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.") Traceback (most recent call last): File "C:/Users/roifo/PycharmProjects/pytorch-sentiment-analysis-classification/train.py", line 237, in <module> print(predict_sentiment(model, " smart , provocative and blisteringly funny", dataset)) File "C:/Users/roifo/PycharmProjects/pytorch-sentiment-analysis-classification/train.py", line 226, in predict_sentiment return prediction.item() ValueError: only one element tensors can be converted to Python scalars
thanks
version details:
In [2]: torchtext.version Out[2]: '0.10.0'
In [2]: torch.version Out[2]: '1.9.0'
@slaysd thanks!
did the change you set in requirements, still got error when running lstm_attn:
merged_state = merged_state.squeeze(0).unsqueeze(2) IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
@roiDaniela @slaysd
I have solved this function by the following code :
def predict_class(text, model, sentence):
model.eval()
tokenized = [tok.text for tok in NLP.tokenizer(sentence)]
indexed = [text.vocab.stoi[t] for t in tokenized]
tensor = torch.LongTensor(indexed).to(DEVICE)
tensor = tensor.unsqueeze(1)
tensor = torch.cat(2*[tensor], dim=1)
preds = model(tensor)
max_preds = preds.argmax(dim=1)
return max_preds[0].item()
Simply, I just duplicated the row (acted as batch_size=2) to pass into the model, and extracted one of the result 😃 ~