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

For OCGNN models, update hypersphere radius, and filter training data with active_mask #95

Merged
merged 10 commits into from
Jan 3, 2024
10 changes: 6 additions & 4 deletions pygod/nn/ocgnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,19 @@ def loss_func(self, emb):
score : torch.Tensor
Outlier scores of shape :math:`N` with gradients.
"""
if self.warmup > 0:
with torch.no_grad():
self.warmup -= 1
self.c = torch.mean(emb, 0)
self.c[(abs(self.c) < self.eps) & (self.c < 0)] = -self.eps
self.c[(abs(self.c) < self.eps) & (self.c > 0)] = self.eps

dist = torch.sum(torch.pow(emb - self.c, 2), 1)
score = dist - self.r ** 2
loss = self.r ** 2 + 1 / self.beta * torch.mean(torch.relu(score))

if self.warmup > 0:
with torch.no_grad():
self.warmup -= 1
self.r = torch.quantile(torch.sqrt(dist), 1 - self.beta)
self.c = torch.mean(emb, 0)
self.c[(abs(self.c) < self.eps) & (self.c < 0)] = -self.eps
self.c[(abs(self.c) < self.eps) & (self.c > 0)] = self.eps

return loss, score