Fine-tuning a pretrained model did not follow as expected from the blog posting
Author: changyeli
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.21.0
- Platform: Linux-5.13.0-52-generic-x86_64-with-glibc2.29
- Python version: 3.8.10
- Huggingface_hub version: 0.8.1
- PyTorch version (GPU?): 1.12.0+cu116 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help?

@LysandreJik, @sgugger, @stevhliu

### Information

- [X] The official example scripts
- [X] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

I was following the [blog](https://huggingface.co/docs/transformers/training) with BERT for sequential classification on my own dataset. Here is the snippet I used for the fine-tuning:

```python
def tokenize_function(element):
    """
    batchfy the tokenization
    :param element: a frament of dataset
    :type element: transformers.Dataset
    """
    return tokenizer(
        element["tran"],
        return_attention_mask=True,
        add_special_tokens=True,
        truncation=True,
        max_length=CONTEXT_LENGTH,
        padding=True)


def prepare_dataset(file_path):
    """
    tokenize the dataset
    :param file_path: the location to the file
    :type file_path: str
    """
    dt = pd.read_csv(file_path)
    # preprocessing omitted here
    dt = dt[["file", "tran", "label"]]
    dt = dt.groupby(["file", "label"])["tran"].apply(". ".join).reset_index()
    dt["tran"] = dt["tran"].str.lower()
    dt = Dataset.from_pandas(dt)
    tokenized_dt = dt.map(tokenize_function, batched=True)
    return tokenized_dt

def compute_metrics(eval_pred):
    """
    compute accuracy for the fine-tuned BERT model

    :param eval_pred: _description_
    :type eval_pred: _type_
    :return: _description_
    :rtype: _type_
    """
    metric = load_metric("accuracy")
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return metric.compute(predictions=predictions, references=labels)

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
tokenized_train = prepare_dataset(
    "training_set.csv")
tokenized_test = prepare_dataset(
    "test.csv")
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
print(tokenized_train[4].keys())
model = AutoModelForSequenceClassification.from_pretrained(
    "bert-base-uncased", num_labels=2)
training_args = TrainingArguments(
    output_dir="../outputs/",
    num_train_epochs=EPOCHS,
    warmup_steps=500,
    fp16=True,
    learning_rate=1e-4,
    per_device_train_batch_size=BATCH_SIZE,
    per_device_eval_batch_size=BATCH_SIZE,
    evaluation_strategy='epoch',
    save_strategy="epoch",
    logging_strategy="epoch",
    # prediction_loss_only=True,
    do_train=True,
    do_eval=True,
    max_grad_norm=1.0,
    seed=RANDOM_SEED,
    data_seed=RANDOM_SEED,
    save_total_limit=1,
    load_best_model_at_end=True,
    report_to="none"
)
if training_args.do_train:
    trainer = Trainer(
        model=model,
        args=training_args,
        data_collator=data_collator,
        tokenizer=tokenizer,
        train_dataset=tokenized_train,
        eval_dataset=tokenized_test,
        compute_metrics=compute_metrics
    )
    trainer.train()
```

The output of `print(tokenized_train[4]` is:

```python
dict_keys(['tran', 'label', 'input_ids', 'token_type_ids', 'attention_mask'])
```

### Expected behavior

The script is basically what the blog has. As expected, it should begin the fine-tuning process. Instead, I got this error: `ValueError: Target size (torch.Size([8])) must be the same as input size (torch.Size([8, 2]))`.

I checked some online resource, it suggested something like `torch.unsqueeze()`, but I wonder if how to make it happen inside of the `trainer`.  Also, I'm a little bit confused - did I missed something from the blog?

Thanks in advance!


Comments:
Please use the [forums](https://discuss.huggingface.co/) to debug your code as we keep issues for feature requests and bugs (in the library) only. It's hard to know what went wrong just from your code since we don't have access to the files you use, but my guess would be that your labels are floats instead of ints, so by default the model thinks you have one-hot encoded labels instead of numbers.

Thanks!

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.