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

Question about amsoftmax. #10

Open
tfygg opened this issue Nov 18, 2020 · 1 comment
Open

Question about amsoftmax. #10

tfygg opened this issue Nov 18, 2020 · 1 comment

Comments

@tfygg
Copy link

tfygg commented Nov 18, 2020

`class ArcMarginProduct(nn.Module):
def init(self, in_feature=128, out_feature=10575, s=32.0, m=0.50, easy_margin=False):
super(ArcMarginProduct, self).init()
self.in_feature = in_feature
self.out_feature = out_feature
self.s = s
self.m = m
self.weight = Parameter(torch.Tensor(out_feature, in_feature))
nn.init.xavier_uniform_(self.weight)

    self.easy_margin = easy_margin
    self.cos_m = math.cos(m)
    self.sin_m = math.sin(m)

    # make the function cos(theta+m) monotonic decreasing while theta in [0°,180°]
    self.th = math.cos(math.pi - m)
    self.mm = math.sin(math.pi - m) * m

def forward(self, x, label):
    # cos(theta)
    cosine = F.linear(F.normalize(x), F.normalize(self.weight))
    # cos(theta + m)
    sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
    phi = cosine * self.cos_m - sine * self.sin_m

    if self.easy_margin:
        phi = torch.where(cosine > 0, phi, cosine)
    else:
        phi = torch.where((cosine - self.th) > 0, phi, cosine - self.mm)

    #one_hot = torch.zeros(cosine.size(), device='cuda' if torch.cuda.is_available() else 'cpu')
    one_hot = torch.zeros_like(cosine)
    one_hot.scatter_(1, label.view(-1, 1), 1)
    output = (one_hot * phi) + ((1.0 - one_hot) * cosine)
    output = output * self.s

    return output`

This is the implementation of amsoftmax, but in your code, cos_theta is equal to output without normalization.

@kprokofi
Copy link
Owner

This function takes cosine from the last layer of the model. See class AngleSimpleLinear.

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

2 participants