-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_results.py
241 lines (194 loc) · 9.63 KB
/
query_results.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
This script contains functions which can assist in querying the directory containing the trained models. There is a lot of overlap with the utilities here and those in plotting.py
"""
import os
import json
import numpy as np
import torch
from src.neural_nets.metrics import MaskedBCE, compute_loss
from src.neural_nets.load_data import get_loader
from src.neural_nets.models import get_model
# a list of configs whose results we want to plot
useful_configs = [
#{'architecture': "TANH", 'init': "zero", 'do_hpsearch': False},
{'architecture': "LDS", 'init': "zero", 'do_hpsearch': False},
#{'architecture': "TANH", 'init': "identity", 'scale': 0.01, 'do_hpsearch': False},
#{'architecture': "LDS", 'init': "identity", 'scale': 0.01, 'do_hpsearch': False},
#{'architecture': "TANH", 'init': "identity", 'scale': 1, 'do_hpsearch': False},
#{'architecture': "LDS", 'init': "identity", 'scale': 1, 'do_hpsearch': False},
#{'architecture': "TANH", 'init': "blockortho", 'scale': 0.01, 'do_hpsearch': False},
{'architecture': "LDS", 'init': "blockortho", 'scale': 0.01, 'do_hpsearch': False},
#{'architecture': "TANH", 'init': "blockortho", 'scale': 1, 'do_hpsearch': False},
#{'architecture': "LDS", 'init': "blockortho", 'scale': 1, 'do_hpsearch': False},
#{'architecture': "TANH", 'init': "normal", 'scale': 0.01, 'do_hpsearch': False},
#{'architecture': "LDS", 'init': "normal", 'scale': 0.01, 'do_hpsearch': False},
#{'architecture': "LDS", 'init': 'critical', 'scale': 1, 'do_hpsearch': False},
#{'architecture': "TANH", 'init': 'critical', 'scale': 1, 'do_hpsearch': False},
#{'architecture': "LDS", 'init': 'critical', 'scale': 0.1, 'do_hpsearch': False},
#{'architecture': "TANH", 'init': 'critical', 'scale': 0.1, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'init': "regression", 'do_hpsearch': False, 'lag': 1},
#{'architecture': "REGRESSION", 'init': "default", 'do_hpsearch': False, 'lag': 7},
#{'architecture': "REGRESSION_WIDE", 'init': "default", 'window': 7,'do_hpsearch': False}
]
reg_configs = [
{'architecture': "REGRESSION", 'lag': 1, 'window': 1, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'lag': 1, 'window': 2, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'lag': 1, 'window': 3, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'lag': 1, 'window': 4, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'lag': 1, 'window': 5, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'lag': 1, 'window': 6, 'do_hpsearch': False},
{'architecture': "REGRESSION", 'lag': 1, 'window': 7, 'do_hpsearch': False}
]
reg_labels = [
'Width 1',
'Width 2',
'Width 3',
'Width 4',
'Width 5',
'Width 6',
'Width 7'
]
# labels corresponding
labels = [
#'RNN:\nzeros',
'LDS:\nzeros',
#'RNN:\ndiag',
#'LDS:\ndiag',
#'RNN:\nidentity',
#'LDS:\nidentity',
#'RNN:\n sbrot',
'LDS:\n sbrot',
#'RNN:\nbrot',
#'LDS:\nbrot',
#'RNN:\nnormal',
#'LDS:\nnormal',
#'LDS:\ncritical',
#'LDS:\nscritical',
#'RNN:\ncritical',
#'RNN:\nscritical',
'Regression',
#'Reg:\nlag 7',
#'Reg:\nwidth 7'
]
# find the directories with these configurations
config_dict = {
'architecture': "TANH",
#'lag': 1,
#'window': 1,
'dataset': "Nottingham",
#'init': "default",
#'parity': "rotate",
#'scale': 0.01,
#'lag': 8,
#'init': "blockortho",
'do_hpsearch': False
}
# success argument checks if there are NaNs in the loss records
def find_results(configs, success=False):
good_dirs = []
dirs = os.listdir('models')
for dir in dirs:
if dir != "_sources":
config_file = open('models/' + dir + '/config.json')
config_contents = config_file.read()
config_file.close()
file_configs = json.loads(config_contents)
agree = True
for key, value in configs.items():
try:
if file_configs[key] != value:
agree = False
break
except:
agree = False
break
if success:
try:
metric_file = open('models/' + dir + '/metrics.json')
metric_contents = metric_file.read()
metric_file.close()
metrics = json.loads(metric_contents)
trainLoss = metrics['trainLoss']['values']
testLoss = metrics['testLoss']['values']
validLoss = metrics['validLoss']['values']
nan = float("NaN")
if nan in trainLoss or nan in testLoss or nan in validLoss:
agree = False
except:
agree = False
if agree:
good_dirs.append(dir)
return good_dirs
def find_recent_metrics(config_dicts, eval_loss=False, initial=False):
"""
:param config_dicts: list of dictionaries whose metrics we want to find
:param eval_loss: whether or not we re-compute the loss (regularized loss may've been computed during training)
:param initial: whether or not to use the initial state dictionaries
:return: a pair: a triple of losses and a triple of accuracies. Each entry is train, test, loss.
"""
train_losses = []
test_losses = []
val_losses = []
train_accs = []
test_accs = []
val_accs = []
for cdict in config_dicts:
# find the most recent experiment with the given configs
good_dirs = find_results(cdict, success=True)
recent_dir = 'results/' + str(np.sort([int(dir) for dir in good_dirs])[-1])
metric_handle = open(recent_dir + '/metrics.json')
recent_metrics = json.loads(metric_handle.read())
metric_handle.close()
# take note of all the most recent accuracies
train_accs.append(recent_metrics['trainAccuracy']['values'][-1])
test_accs.append(recent_metrics['testAccuracy']['values'][-1])
val_accs.append(recent_metrics['validAccuracy']['values'][-1])
# compute the unregularized loss for the model over each dataset, just in case
if eval_loss and cdict['architecture'] != "REGRESSION":
# we must read some information about the model to properly construct the data loaders
config_handle = open(recent_dir + '/config.json')
recent_configs = json.loads(config_handle.read())
config_handle.close()
dataset = recent_configs['dataset']
batch_size = recent_configs['batch_size']
architecture = recent_configs['architecture']
# determine if there is a certain part of the sequences we need to cover up
init_mask = 0
if architecture == "REGRESSION":
init_mask = recent_configs['lag']
elif architecture == "REGRESSION_WIDE":
init_mask = recent_configs['window']
train_loader, test_loader, val_loader = get_loader(dataset, batch_size, init_mask)
# loss function without regularization
loss_fcn = MaskedBCE(0)
# get the configuration for the model and construct it
# initializer is not required
model_dict = {'architecture': architecture,
'readout': recent_configs['readout'],
'gradient_clipping': recent_configs['gradient_clipping'],
'jit': recent_configs['jit'],
'lag': recent_configs['lag'],
'window': recent_configs['window'],
'input_size': recent_configs['input_size'],
'hidden_size': recent_configs['hidden_size'],
'num_layers': recent_configs['num_layers'],
'output_size': recent_configs['output_size']
}
dict_name = '/final_state_dict.pt'
if initial:
dict_name = '/initial_state_dict.pt'
state_dict = torch.load(str(recent_dir) + dict_name, map_location='cpu')
model = get_model(model_dict, {'init': "default"}, False)
model.load_state_dict(state_dict)
# compute and record the losses
train_losses.append(compute_loss(loss_fcn, model, train_loader))
test_losses.append(compute_loss(loss_fcn, model, test_loader))
val_losses.append(compute_loss(loss_fcn, model, val_loader))
else:
# take note of all the most recent accuracies
train_losses.append(recent_metrics['trainLoss']['values'][-1])
test_losses.append(recent_metrics['testLoss']['values'][-1])
val_losses.append(recent_metrics['validLoss']['values'][-1])
return ((train_losses, test_losses, val_losses), (train_accs, test_accs, val_accs))
if __name__ == "__main__":
print(find_results(config_dict, success=True))