Reputation: 23
I have different sizes or shapes for each tensor like
torch.Size([1, 12, 1000])
torch.Size([1, 12, 1000])
torch.Size([1, 10, 1000])
torch.Size([1, 11, 1000])
torch.Size([1, 11, 1000])
torch.Size([1, 15, 1000])
torch.Size([1, 10, 1000])
....
and need to be like torch.Size((12+12+10+11+11+15+ .... ),1000)
my code is
def extract():
for y in sentences:
tokenized_text = tokenizer.tokenize(y)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
tokens_tensor = torch.tensor([indexed_tokens])
model.eval()
outputs = model(tokens_tensor)
hidden_states = outputs[2]
my_tensor = torch.cat([hidden_states[i] for i in [-1,-2,-3,-4]], dim=-1)
return my_tensor
Upvotes: 0
Views: 852
Reputation: 2280
Concatenate them:
tensors = [t1, t2, t3, ...]
result = torch.cat(tensors, dim=1)
# result.size(): torch.Size([1, 12+12+10+..., 1000])
If you also want to remove the first dimension, as it has size 1
:
result = result.squeeze()
# result.size(): torch.Size([12+12+10+..., 1000])
Upvotes: 1