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

Major BiC fix/reimplementation #1550

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions avalanche/models/bic_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Iterable, SupportsInt
import torch


Expand All @@ -10,25 +11,28 @@ class BiasLayer(torch.nn.Module):
Recognition. 2019"
"""

def __init__(self, device, clss):
def __init__(self, clss: Iterable[SupportsInt]):
"""
:param device: device used by the main model. 'cpu' or 'cuda'
:param clss: list of classes of the current layer. This are use
to identify the columns which are multiplied by the Bias
correction Layer.
"""
super().__init__()
self.alpha = torch.nn.Parameter(torch.ones(1, device=device))
self.beta = torch.nn.Parameter(torch.zeros(1, device=device))
self.alpha = torch.nn.Parameter(torch.ones(1))
self.beta = torch.nn.Parameter(torch.zeros(1))

self.clss = torch.Tensor(list(clss)).long().to(device)
self.not_clss = None
unique_classes = list(sorted(set(int(x) for x in clss)))

self.register_buffer("clss", torch.tensor(unique_classes, dtype=torch.long))

def forward(self, x):
alpha = torch.ones_like(x)
beta = torch.ones_like(x)
beta = torch.zeros_like(x)

alpha[:, self.clss] = self.alpha
beta[:, self.clss] = self.beta

return alpha * x + beta


__all__ = ["BiasLayer"]
Loading
Loading