-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunsupdro.py
135 lines (105 loc) · 4.66 KB
/
unsupdro.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Fully-Supervised model."""
import torch
import torch.nn.functional as F
from model.fullyconn import FullyConnected
from util.train import BaseTrain
from util.utils import HParams, DEFAULT_MISSING_CONST as DF_M
from util.metrics_store import MetricsEval
from pytorch_model_summary import summary
from util.utils import HParams
import numpy as np
import pdb
class UnsupDRO(BaseTrain):
"""Fully Unsupervised DRO measure.
https://arxiv.org/pdf/1806.08010.pdf
"""
def __init__(self, hparams):
super(UnsupDRO, self).__init__(hparams)
print('UnsupDRO!')
def get_ckpt_path(self):
super(UnsupDRO, self).get_ckpt_path()
new_params = ['_unsupdro_eta', self.hp.unsupdro_eta]
self.params_str += '_'.join([str(x) for x in new_params])
def get_config(self):
super(UnsupDRO, self).get_config()
# Additional hyperparameters.
self.unsupdro_eta = self.hp.unsupdro_eta
self.relu = torch.nn.ReLU()
def train_step(self, batch):
"""Trains a model for one step."""
# Prepare data.
x = batch[0].float()
y = batch[1].long()
c = batch[2].long()
if self.hp.flag_usegpu and torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
c = c.cuda()
# Compute loss
y_logit = self.model(x)
y_pred = torch.argmax(y_logit, 1)
# Calculating unsupervised dro
loss = F.cross_entropy(y_logit, y, reduction='none')
loss_unsupdro = self.relu(loss - self.unsupdro_eta).mean()
# Compute gradient.
self.optimizer.zero_grad()
loss_unsupdro.backward()
self.optimizer.step()
# Update metrics.
# Maintains running average over all the metrics
# TODO: eliminate the need for for loops
prefix = 'train'
for cid in range(-1, self.dset.n_controls):
select = c != DF_M if cid == -1 else c == cid
size = sum(select)
self.metrics_dict[f'{prefix}.loss.{cid}'].update(
val=MetricsEval().cross_entropy(y_logit[select], y[select]),
num=size)
self.metrics_dict[f'{prefix}.acc.{cid}'].update(
val=MetricsEval().accuracy(y_pred[select], y[select]),
num=size)
self.metrics_dict[f'{prefix}.y_score.{cid}'] = \
np.concatenate((self.metrics_dict[f'{prefix}.y_score.{cid}'],
MetricsEval().logit2prob(y_logit[select]).cpu().numpy()))
self.metrics_dict[f'{prefix}.y_true.{cid}'] = \
np.concatenate((self.metrics_dict[f'{prefix}.y_true.{cid}'],
y[select].cpu().numpy()))
def eval_step(self, batch, prefix='test'):
"""Trains a model for one step."""
# Prepare data.
x = batch[0].float()
y = batch[1].long()
c = batch[2].long()
if self.hp.flag_usegpu and torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
c = c.cuda()
# Compute loss
with torch.no_grad():
y_logit = self.model(x)
y_pred = torch.argmax(y_logit, 1)
for cid in range(-1, self.dset.n_controls):
select = c != DF_M if cid == -1 else c == cid
size = sum(select)
self.metrics_dict[f'{prefix}.loss.{cid}'].update(
val=MetricsEval().cross_entropy(y_logit[select], y[select]),
num=size)
self.metrics_dict[f'{prefix}.acc.{cid}'].update(
val=MetricsEval().accuracy(y_pred[select], y[select]),
num=size)
self.metrics_dict[f'{prefix}.y_score.{cid}'] = \
np.concatenate((self.metrics_dict[f'{prefix}.y_score.{cid}'],
MetricsEval().logit2prob(y_logit[select]).cpu().numpy()))
self.metrics_dict[f'{prefix}.y_true.{cid}'] = \
np.concatenate((self.metrics_dict[f'{prefix}.y_true.{cid}'],
y[select].cpu().numpy()))
if __name__ == '__main__':
trainer = UnsupDRO(hparams=HParams({'dataset': 'Adult',
'batch_size': 64,
'model_type': 'fullyconn',
'learning_rate': 0.0001,
'weight_decay': 0.00001,
'num_epoch': 100,
}))
trainer.get_config()
trainer.train()