-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherm.py
More file actions
125 lines (95 loc) · 4.18 KB
/
Copy patherm.py
File metadata and controls
125 lines (95 loc) · 4.18 KB
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
"""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
import numpy as np
import pdb
class ERM(BaseTrain):
"""Fully-supervised model trainer."""
def __init__(self, hparams):
super(ERM, self).__init__(hparams)
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()
# Check for missing values
if DF_M in c:
raise ValueError('Missing values not supported')
# Compute loss
y_logit = self.model(x)
y_pred = torch.argmax(y_logit, 1)
loss = F.cross_entropy(y_logit, y)
# Compute gradient.
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# Update metrics.
# Maintains running average over all the metrics
prefix = 'train'
for cid in range(-1, self.dset.n_controls):
select = c >= 0 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()
# Check for missing values
if DF_M in c:
raise ValueError('Missing values not supported')
# 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 >= 0 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 = ERM(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()