Change last layer on pretrained huggingface model
I want to re-finetuned a transformer model but I get an unknown error when I tried to train the model.
I can't change the &quot;num_labels&quot; on loading the model.
So, I tried to change it manually
model_name = &quot;mrm8488/flaubert-small-finetuned-movie-review-sentiment-analysis&quot;
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name).to('cuda')

num_labels = 3
model.sequence_summary.summary = torch.nn.Linear(in_features=model.sequence_summary.summary.in_features, out_features=num_labels, bias=True)




trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_train['train'],
    eval_dataset=tokenized_test['train'],
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
    #data_collator=data_collator,
)

trainer.train()

The error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-93-8139f38c5ec6&gt; in &lt;module&gt;()
     20 )
     21 
---&gt; 22 trainer.train()

7 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
   2844     if size_average is not None or reduce is not None:
   2845         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2846     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
   2847 
   2848 

ValueError: Expected input batch_size (24) to match target batch_size (16).


So, There is a solution for this
Just add ignore_mismatched_sizes=True when loading the model as:
model = AutoModelForSequenceClassification.from_pretrained(model_name,num_labels=3, ignore_mismatched_sizes=True).to('cuda')

