lonePatient / Bert-Multi-Label-Text-Classification

This repo contains a PyTorch implementation of a pretrained BERT model for multi-label text classification.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ValueError: Target size (torch.Size([8, 5])) must be the same as input size (torch.Size([8, 6]))

Bamboomini opened this issue · comments

Hi
I run zhe code in my servers following the steps. And I met the error as follows:

4/01/2020 01:25:10 - INFO - root - ***** Running training *****
04/01/2020 01:25:10 - INFO - root - Num examples = 122532
04/01/2020 01:25:10 - INFO - root - Num Epochs = 6
04/01/2020 01:25:10 - INFO - root - Total train batch size (w. parallel, distributed & accumulation) = 8
04/01/2020 01:25:10 - INFO - root - Gradient Accumulation steps = 1
04/01/2020 01:25:10 - INFO - root - Total optimization steps = 91902
04/01/2020 01:25:22 - INFO - root - Epoch 1/6
Traceback (most recent call last):
File "run_bert.py", line 228, in
main()
File "run_bert.py", line 221, in main
run_train(args)
File "run_bert.py", line 129, in run_train
trainer.train(train_data=train_dataloader, valid_data=valid_dataloader)
File "/wangyu/bert-multi-laber/pybert/train/trainer.py", line 153, in train
train_log = self.train_epoch(train_data)
File "/wangyu/bert-multi-laber/pybert/train/trainer.py", line 98, in train_epoch
loss = self.criterion(output=logits,target=label_ids)
File "/wangyu/bert-multi-laber/pybert/train/losses.py", line 22, in call
loss = self.loss_fn(input = output,target = target)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in call
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py", line 601, in forward
reduction=self.reduction)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py", line 2112, in binary_cross_entropy_with_logits
raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
ValueError: Target size (torch.Size([8, 5])) must be the same as input size (torch.Size([8, 6]))

I think it was wrong that I do something. But I do not know why? Hope to get your help!

Thank you very much!

Same error for me but with [11, 5]... it looks like it cant handle if you dont have exactly 6 labels?

commented

Please check your train.csv. Number of labels should exactly be same as specified in xlnet_processor.py and bert_processor.py. Also remove all cached files and .pkl files in dataset directory. Re-run bert or xlnet and your issue should be resolved.