-
Notifications
You must be signed in to change notification settings - Fork 1
/
ERM_dpsgdf.py
47 lines (44 loc) · 1.63 KB
/
ERM_dpsgdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import torch
from algorithms.single_model_algorithm import SingleModelAlgorithm
from models.initializer import initialize_model
from dpsgdf import FairDPOptimizer
from torch.nn.utils import clip_grad_norm_
class ERMDPSGDf(SingleModelAlgorithm):
def __init__(self, config, d_out, grouper, loss,
metric, n_train_steps):
model = initialize_model(config, d_out).to(config.device)
# initialize module
super().__init__(
config=config,
model=model,
grouper=grouper,
loss=loss,
metric=metric,
n_train_steps=n_train_steps,
)
def objective(self, results):
return self.loss.compute(results['y_pred'], results['y_true'], return_dict=False)
def _update(self, results):
"""
Computes the objective and updates the model.
Also updates the results dictionary yielded by process_batch().
Should be overridden to change algorithm update beyond modifying the objective.
"""
# compute objective
objective = self.objective(results)
results['objective'] = objective.item()
# update
self.model.zero_grad()
if isinstance(self.optimizer, FairDPOptimizer):
self.optimizer.zero_grad()
self.optimizer.sample_groups = results['g']
else:
raise
objective.backward()
if self.max_grad_norm:
clip_grad_norm_(self.model.parameters(), self.max_grad_norm)
self.optimizer.step()
self.step_schedulers(
is_epoch=False,
metrics=results,
log_access=False)