ValueError: Target size (torch.Size([32])) must be the same as input size (torch.Size([32, 1]))

label_cols = ["toxic", "severe_toxic"]

databunch = TextDataBunch.from_df(".", train, val, test,

              collate_fn=partial(pad_collate, pad_first=False, pad_idx=0),
from pytorch_pretrained_bert.modeling import BertConfig, BertForSequenceClassification

bert_model = BertForSequenceClassification.from_pretrained(config.bert_model_name, num_labels=2)

Code Works fine when I have two labels columns as my target. But when I have:
label_cols = [“toxic”]
bert_model = BertForSequenceClassification.from_pretrained(config.bert_model_name, num_labels=1)

Gives me error
ValueError: Target size (torch.Size([32])) must be the same as input size (torch.Size([32, 1]))

Hi, i suggest to prvoide the code if you really want people to help you.
However, I guess the problem is the label_cols that its size is torch.Size([32]), and you should make it to be (torch.Size([32, 1])). Thus, I suggest this.

label_cols = [“toxic”]
torch.unsqueeze(label_cols, 1)
print(label_cols.shape)          //It should be (torch.Size([32, 1]))

My suggestion may be wrong because I haven’t used plain python for a while.

The code is same as is this post:

Except I only want to train on label_cols = [“toxic”] .
Code works fine with more than one label columns