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

EEGClassifier classifier not supporting criterion=torch.nn.CrossEntropyLoss() #311

Open
bokey007 opened this issue Jul 25, 2021 · 3 comments
Labels
question Further information is requested

Comments

@bokey007
Copy link

Hi, I want to pass custom criterion to the EEGClassifier.

but its seems to support only torch.nn.NLLLoss.

I even tried criterion=torch.nn.CrossEntropyLoss() but it does not work.

Is their any way to pass our custom loss instead of NLLLoss.

looking forward to your answer,
Bokey

@gemeinl
Copy link
Collaborator

gemeinl commented Jul 25, 2021

If you attempt to do trialwise decoding, criterion=CrossEntropyLoss will work.
If you attempt to do cropped decoding, set criterion=CroppedLoss and criterion__loss_function=torch.nn.functional.cross_entropy).

@bokey007
Copy link
Author

bokey007 commented Jul 25, 2021

Hi thanka for the reply. I am trying to create custom criterion as follows :


class Cosine(nn.modules.loss._WeightedLoss):
    def __init__(self, s=64.0, m=0.40):
        super(CosFace, self).__init__()
        self.s = s
        self.m = m


    def forward(self, cosine: torch.Tensor, label: torch.Tensor):
        index = torch.where(label != -1)[0]
        m_hot = torch.zeros(index.size()[0], cosine.size()[1], device=cosine.device)
        m_hot.scatter_(1, label[index, None], self.m)
        cosine[index] -= m_hot
        ret = cosine * self.s
        return ret

But am getting following error 👍

RuntimeError: grad can be implicitly created only for scalar outputs

Can u please help me understand ?

@robintibor
Copy link
Contributor

Hi,

as the error indicates, your loss function needs to return a single value (hence the "scalar outputs" error). May be your loss function creates a value per example? then you still need to call torch.mean on it before returning, e.g., maybe
return torch.mean(ret) would work?

@bruAristimunha bruAristimunha added the question Further information is requested label Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants