Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target size (torch.Size([10])) must be the same as input size (torch.Size([10, 6])) #20

Open
basti97 opened this issue Jun 11, 2021 · 2 comments

Comments

@basti97
Copy link

basti97 commented Jun 11, 2021

Another error occurs while running DocumentBertLSTM:

_ValueError Traceback (most recent call last)
in ()
7
8 model = BertForDocumentClassification(args=args)
----> 9 model.fit((train_documents, train_labels), (dev_documents,dev_labels))

3 frames
/usr/local/lib/python3.7/dist-packages/bert_document_classification/document_bert.py in fit(self, train, dev)
187
188 batch_correct_output = correct_output[i:i + self.args['batch_size']].to(device=self.args['device'])
--> 189 loss = self.loss_function(batch_predictions, batch_correct_output)
190 epoch_loss += float(loss.item())
191 #self.log.info(batch_predictions)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
530 result = self._slow_forward(*input, **kwargs)
531 else:
--> 532 result = self.forward(*input, **kwargs)
533 for hook in self._forward_hooks.values():
534 hook_result = hook(self, input, result)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
599 self.weight,
600 pos_weight=self.pos_weight,
--> 601 reduction=self.reduction)
602
603

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
2122
2123 if not (target.size() == input.size()):
-> 2124 raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
2125
2126 return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

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

For my understanding it has to do something with the args.label-argument.
Can you tell me how I set the labels for this argument right?

I have 6 Labels in my dataset (0,1,2,3,4,5).
I tried running while setting the args-labels-argument as a list ([0,1,2,3,4,5]).
I also tried a string ("0,1,2,3,4,5"). In that case the input size was in shape of (torch.Size([10, 1])

Thank you for response.

@zeinramadan
Copy link

Have you managed to find a solution? Having similar trouble

@zeinramadan
Copy link

Check your config.ini file and make sure each label is entered properly and is spaced out with commas, essentially make sure the labels are in the exact same way it's written in the config.ini file available in the examples folder in the repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants