I want to perform a sentence similarity task and tried the following:
from transformers import XLNetTokenizer, XLNetModel
import torch
import scipy
import torch.nn as nn
import torch.nn.functional as F
tokenizer = XLNetTokenizer.from_pretrained('xlnet-large-cased')
model = XLNetModel.from_pretrained('xlnet-large-cased')
input_ids = torch.tensor(tokenizer.encode("Hello, my animal is cute", add_special_tokens=False)).unsqueeze(0)
outputs = model(input_ids)
last_hidden_states = outputs[0]
input_ids = torch.tensor(tokenizer.encode("I like your cat", add_special_tokens=False)).unsqueeze(0)
outputs1 = model(input_ids)
last_hidden_states1 = outputs1[0]
cos = nn.CosineSimilarity(dim=1, eps=1e-6)
output = cos(last_hidden_states, last_hidden_states1)
However, I get the following error:
RuntimeError: The size of tensor a (7) must match the size of tensor b (4) at non-singleton dimension 1
Can anybody tell me, what I am doing wrong? Is there a better way to do it?