Open
Conversation
Add tow lines of codes to avoid Nan Loss
Author
Author
def forward(self, logits, z, labels):
if self.multiclass:
if not self.onehot:
labels = F.one_hot(labels, logits.size(1))
labels = labels.float()
margin_logits = self.compute_margin_logits(logits, labels)
# label smoothing
log_logits = F.log_softmax(margin_logits, dim=1)
# if there are some Zero Vector in Matrix
A = ((labels==0).sum(dim=1) == labels.shape[1])
labels[A==True] = 1
labels_scaled = labels / labels.sum(dim=1, keepdim=True)
loss = - (labels_scaled * log_logits).sum(dim=1)
loss = loss.mean() |
Author
|
I will continue to add the report later . I want to make a comparison between these two changes |
Author
|
I found that It has a better performance than just delete label_scale . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

I wonder if it is possible to convert the zero vectors in the matrix to one vectors? To avoid NaN loss, add the following two lines of code. I'm testing this modified model to see if it works. As a newbie to deep learning, please forgive me if my question is naive. thank you very much for your help!🙏