Skip to content

Commit

Permalink
Update trainer.mdx class_weights example (huggingface#23787)
Browse files Browse the repository at this point in the history
class_weights tensor should follow model's device
  • Loading branch information
amitportnoy authored and gojiteji committed Jun 5, 2023
1 parent 3ff3bc2 commit 77ddc24
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion docs/source/en/main_classes/trainer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CustomTrainer(Trainer):
outputs = model(**inputs)
logits = outputs.get("logits")
# compute custom loss (suppose one has 3 labels with different weights)
loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 2.0, 3.0]))
loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 2.0, 3.0], device=model.device))
loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
return (loss, outputs) if return_outputs else loss
```
Expand Down

0 comments on commit 77ddc24

Please sign in to comment.