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

fix: allow to pass other loss function to training strategies #101

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions whittle/loss/kd_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def forward(self, outputs, labels, outputs_teacher):
torch.Tensor: The combined loss.
"""
soft_target_loss = 0
outputs_teacher = outputs_teacher.detach()
if outputs_teacher is not None and self.distillation_weight > 0:
soft_target_loss = self.kldiv(
F.log_softmax(outputs / self.temperature, dim=1),
Expand Down
2 changes: 1 addition & 1 deletion whittle/training_strategies/ats.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __call__(self, model, inputs, outputs, **kwargs):
super-network.
"""
total_loss = 0
y_supernet = model(inputs).detach()
y_supernet = model(inputs)
if self.current_step % 2 == 0:
# update random sub-networks
for i in range(self.random_samples):
Expand Down
4 changes: 2 additions & 2 deletions whittle/training_strategies/base_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
self,
sampler: RandomSampler,
loss_function: Callable,
kd_loss: DistillLoss | None = None,
kd_loss: Callable | None = None,
device: str = "cuda",
**kwargs,
):
Expand All @@ -35,7 +35,7 @@ def __init__(
self.loss_function = loss_function
self.device = device
self.kd_loss = kd_loss
if self.kd_loss is not None:
if isinstance(self.kd_loss, DistillLoss):
if not isinstance(loss_function, torch.nn.CrossEntropyLoss):
raise TypeError(
"KD Loss not yet supported: Expected torch.nn.CrossEntropyLoss"
Expand Down
4 changes: 2 additions & 2 deletions whittle/training_strategies/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RandomStrategy(BaseTrainingStrategy):
Randomly samples and updates `random_samples` sub-networks in each step.
"""

def __init__(self, random_samples=1, **kwargs):
def __init__(self, random_samples: int = 1, **kwargs):
"""
Initialises a `RandomStrategy`

Expand All @@ -24,7 +24,7 @@ def __init__(self, random_samples=1, **kwargs):
def __call__(self, model, inputs, outputs, **kwargs):
"""Updates randomly sampled sub-networks in each step."""
total_loss = 0
y_supernet = model(inputs).detach()
y_supernet = model(inputs)
for i in range(self.random_samples):
config = self.sampler.sample()
model.select_sub_network(config)
Expand Down
2 changes: 1 addition & 1 deletion whittle/training_strategies/random_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, total_number_of_steps: int, random_samples: int = 1, **kwargs

def __call__(self, model, inputs, outputs, **kwargs):
total_loss = 0
y_supernet = model(inputs).detach()
y_supernet = model(inputs)
if np.random.rand() <= self.rate[self.current_step]:
# update random sub-networks
for i in range(self.random_samples):
Expand Down
6 changes: 3 additions & 3 deletions whittle/training_strategies/sandwich.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SandwichStrategy(BaseTrainingStrategy):
https://arxiv.org/abs/1903.05134
"""

def __init__(self, random_samples=2, **kwargs):
def __init__(self, random_samples: int = 2, **kwargs):
"""
Initialises a `SandwichStrategy`

Expand All @@ -42,7 +42,7 @@ def __call__(self, model, inputs, outputs, **kwargs):
model.select_sub_network(config)
y_hat = model(inputs)
if self.kd_loss is not None:
loss = self.kd_loss(y_hat, outputs, y_supernet.detach())
loss = self.kd_loss(y_hat, outputs, y_supernet)
else:
loss = self.loss_function(y_hat, outputs)
loss.backward()
Expand All @@ -54,7 +54,7 @@ def __call__(self, model, inputs, outputs, **kwargs):
model.select_sub_network(config)
y_hat = model(inputs)
if self.kd_loss is not None:
loss = self.kd_loss(y_hat, outputs, y_supernet.detach())
loss = self.kd_loss(y_hat, outputs, y_supernet)
else:
loss = self.loss_function(y_hat, outputs)
loss.backward()
Expand Down