Wrong dimensions of input to loss function, multilabel classification
Author: simhallq
Pull request: no
Labels: name: wontfix

# 🐛 Bug

## Information

**Model I am using (Bert, XLNet ...):**
bert-base-uncased

**Language I am using the model on (English, Chinese ...):**
English

**The problem arises when using:**
* [x] the official example scripts: (give details below)
[](url)
Problem originally arose when running my own scripts but I was able to reproduce with example code from [https://huggingface.co/transformers/model_doc/bert.html#bertforsequenceclassification](https://huggingface.co/transformers/model_doc/bert.html#bertforsequenceclassification)


**The tasks I am working on is:**
Multi-label classification

## To reproduce
Add `.from_pretrained("bert-base-uncased",num_labels=3)` and and 2 additional labels (`torch.tensor([1,0,0])`) to example code from docs, like so: 


```
from transformers import BertTokenizer, BertForSequenceClassification
import torch

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained("bert-base-uncased",num_labels=3)

input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(0)  # Batch size 1
labels = torch.tensor([1,0,0]).unsqueeze(0)  # Batch size 1
outputs = model(input_ids, labels=labels)

loss, logits = outputs[:2]
```


> Traceback (most recent call last):
  File "/Users/Simpan/agge/agust/playground/sve/test/loss_bug.py", line 8, in <module>
    outputs = model(input_ids, labels=labels)
  File "/Users/Simpan/agge/agust/playground/sve/env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/Simpan/agge/agust/playground/sve/env/lib/python3.7/site-packages/transformers/modeling_bert.py", line 1193, in forward
    loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
  File "/Users/Simpan/agge/agust/playground/sve/env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/Simpan/agge/agust/playground/sve/env/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/Users/Simpan/agge/agust/playground/sve/env/lib/python3.7/site-packages/torch/nn/functional.py", line 2009, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/Users/Simpan/agge/agust/playground/sve/env/lib/python3.7/site-packages/torch/nn/functional.py", line 1836, in nll_loss
    .format(input.size(0), target.size(0)))
ValueError: Expected input batch_size (1) to match target batch_size (3).



## Expected behavior
Was expecting to get loss for output nodes vs. labels (3 in this case). 

Furthermore, removing labels as input to model returns logits with right dimensions:
```
#outputs = model(input_ids, labels=labels)
outputs = model(input_ids)
print(outputs)
```
> (tensor([[ 0.1703, -0.0389,  0.0923]], grad_fn=<AddmmBackward>),)

## Environment info
     
- `transformers` version: 2.9.0
- Platform: Darwin-18.5.0-x86_64-i386-64bit
- Python version: 3.7.7
- PyTorch version (GPU?): 1.3.1 (False)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: No.
- Using distributed or parallel set-up in script?: No.



Comments:
If you are doing anything custom, then you should write your own model class, right? Plus your own loss function suiting your needs if default ones raises errors?

[This](https://discuss.pytorch.org/t/what-kind-of-loss-is-better-to-use-in-multilabel-classification/32203/) might help;

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.