-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
|
||
class NeuralNetworkOptimizer: | ||
def __init__(self, model: nn.Module, criterion: nn.Module, optimizer: optim.Optimizer): | ||
self.model = model | ||
self.criterion = criterion | ||
self.optimizer = optimizer | ||
|
||
def train(self, inputs: torch.Tensor, targets: torch.Tensor): | ||
self.optimizer.zero_grad() | ||
outputs = self.model(inputs) | ||
loss = self.criterion(outputs, targets) | ||
loss.backward() | ||
self.optimizer.step() | ||
|
||
def prune(self, amount: float): | ||
# Prune the model by removing the smallest weights | ||
for module in self.model.modules(): | ||
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear): | ||
weights = module.weight.data | ||
threshold = torch.abs(weights).mean() * amount | ||
mask = torch.abs(weights) > threshold | ||
module.weight.data *= mask | ||
|
||
def quantize(self, bits: int): | ||
# Quantize the model's weights and activations | ||
for module in self.model.modules(): | ||
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear): | ||
weights = module.weight.data | ||
min_val = weights.min() | ||
max_val = weights.max() | ||
scale = (max_val - min_val) / (2 ** bits - 1) | ||
module.weight.data = torch.round((weights - min_val) / scale) * scale + min_val | ||
|
||
def knowledge_distillation(self, teacher_model: nn.Module): | ||
# Perform knowledge distillation from the teacher model | ||
for module in self.model.modules(): | ||
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear): | ||
teacher_weights = teacher_model.state_dict()[module.weight.name] | ||
module.weight.data = teacher_weights | ||
|
||
class NeuralNetworkPruningScheduler: | ||
def __init__(self, optimizer: NeuralNetworkOptimizer, prune_amount: float, prune_frequency: int): | ||
self.optimizer = optimizer | ||
self.prune_amount = prune_amount | ||
self.prune_frequency = prune_frequency | ||
self.epoch = 0 | ||
|
||
def step(self): | ||
if self.epoch % self.prune_frequency == 0: | ||
self.optimizer.prune(self.prune_amount) | ||
self.epoch += 1 |