I cannot understand how to fine-tune ‘Transformer XL’ for sequence classification.
I am getting this error
RuntimeError: stack expects each tensor to be equal size, but got [1] at entry 0 and [8] at entry 2
and I understand that it is due to my sequences having varying lengths but I am not sure how this is intended to be remedied for this specific model. I have created a simple reproduceable toy example with the intention of understanding how this model is intended to be used:
!pip install transformers==4.10.0
!pip install datasets==1.9.0
from transformers import AutoTokenizer
from transformers import TransfoXLForSequenceClassification
from transformers import TrainingArguments, Trainer
import torch
class newDatasetSimple(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
# print(item)
return item
def __len__(self):
return len(self.labels)
tokenizer = AutoTokenizer.from_pretrained('transfo-xl-wt103')
model = TransfoXLForSequenceClassification.from_pretrained('transfo-xl-wt103')
texts = ['This is a sentence', 'Here is another', 'Short sentence', 'I ran', 'This is the longest sentence of the bunch', 'What?', 'Who?', 'Test.', 'Hey', 'A', 'The', 'So', 'yes', 'cool', 'beans', 'In the flesh']
labels = [0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1]
encodings = tokenizer(list(texts))
ds = newDatasetSimple(encodings, labels)
training_args = TrainingArguments(output_dir='.',
num_train_epochs=50,
per_device_train_batch_size=16,
warmup_steps=0,
logging_steps=1,
learning_rate=1e-5)
trainer = Trainer(model=model,
args=training_args,
train_dataset=ds)
trainer.train()
Could someone show me what changes need to be made in the code to get the model to train properly?
Thank you for reading!