-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[Concept Entry] PyTorch: Built-in Loss Functions #6085
base: main
Are you sure you want to change the base?
[Concept Entry] PyTorch: Built-in Loss Functions #6085
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @PragatiVerma18, I've reviewed the entry and left some comments. Please make the changes, thank you!!
- `size_average` (bool, optional): Deprecated (use `reduction` instead). If `True`, the loss is averaged over observations for each mini-batch. Default is `True`. | ||
- `ignore_index` (int, optional): Specifies a target value that is ignored and does not contribute to the loss calculation. | ||
- `reduce` (bool, optional): Deprecated (use `reduction` instead). If `True`, it sums the losses across the batch. Default is `True`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these are deprecated, then I think we don't need to include them.
predictions = torch.tensor([2.0, 3.0, 4.0]) | ||
targets = torch.tensor([2.5, 3.5, 4.5]) | ||
loss = loss_fn(predictions, targets) | ||
print(loss) # Output: tensor(0.0833) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recheck the output of this code here. I'm getting this output - tensor(0.2500)
logits = torch.tensor([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]) | ||
labels = torch.tensor([2, 0]) | ||
loss = loss_fn(logits, labels) | ||
print(loss) # Output: tensor(0.4076) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting this output for the above code - tensor(1.4076)
logits = torch.tensor([0.5, -1.5, 2.0]) | ||
labels = torch.tensor([1.0, 0.0, 1.0]) | ||
loss = loss_fn(logits, labels) | ||
print(loss) # Output: tensor(0.4891) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recheck the output.
import torch | ||
import torch.nn as nn | ||
|
||
# Example of CosineEmbeddingLoss | ||
loss_fn = nn.CosineEmbeddingLoss() | ||
input1 = torch.tensor([1.0, 0.0]) | ||
input2 = torch.tensor([0.0, 1.0]) | ||
target = torch.tensor([1]) # 1 means the inputs should be similar | ||
loss = loss_fn(input1, input2, target) | ||
print(loss) # Output: tensor(2.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's giving a runtime error. Please correct the code.
import torch | ||
import torch.nn as nn | ||
|
||
# Example of KLDivLoss | ||
loss_fn = nn.KLDivLoss(reduction='batchmean') | ||
input = torch.tensor([[0.0, 0.1, 0.2], [0.1, 0.0, 0.3]]) | ||
target = torch.tensor([[0.0, 0.1, 0.3], [0.1, 0.2, 0.2]]) | ||
loss = loss_fn(input.log(), target) | ||
print(loss) # Output: tensor(0.0237) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something wrong with the code snippet. It's giving tensor(nan)
output.
|
||
## Choosing the Right Loss Function | ||
|
||
When selecting a loss function for your model, it is essential to consider the task you're working on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use first and second-person pronouns.
Description
Add a new entry on Built-in Loss Functions concept under PyTorch.
Issue Solved
Closes #5861
Type of Change
Checklist
main
branch.Issues Solved
section.