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

Doesn't anyone think the author's center loss is too complicated? #20

Open
crj1998 opened this issue May 9, 2021 · 3 comments
Open

Comments

@crj1998
Copy link

crj1998 commented May 9, 2021

A concise and easy to understand version

class CenterLoss(nn.Module):
    def __init__(self, num_class=10, num_feature=2):
        super(CenterLoss, self).__init__()
        self.num_class = num_class
        self.num_feature = num_feature
        self.centers = nn.Parameter(torch.randn(self.num_class, self.num_feature))

    def forward(self, x, labels):
        center = self.centers[labels]
        dist = (x-center).pow(2).sum(dim=-1)
        loss = torch.clamp(dist, min=1e-12, max=1e+12).mean(dim=-1)

        return loss
@ajndkr
Copy link

ajndkr commented May 26, 2021

@crj1998 I agree. The above solution is more efficient. I suggest you open a PR for the author.

@RaduFilip16
Copy link

Can you please give an example of how to use this?

@Anson-He
Copy link

Does anyone know if that works?

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

No branches or pull requests

4 participants