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

Update loss.py #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update loss.py #2

wants to merge 1 commit into from

Conversation

Mahiro2211
Copy link

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!🙏

Add tow lines of codes to avoid Nan Loss
@Mahiro2211
Copy link
Author

Last Night I try to delete the lable_scale and I found it works also well , but the loss is big , so I think if turning zero vectro into One vetcor is ok
image

@Mahiro2211
Copy link
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()

@Mahiro2211
Copy link
Author

I will continue to add the report later . I want to make a comparison between these two changes

@Mahiro2211
Copy link
Author

I found that It has a better performance than just delete label_scale .

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

Successfully merging this pull request may close these issues.

1 participant