-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
174 lines (146 loc) · 5.57 KB
/
main.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
import os
import time
import argparse
import numpy as np
import multiprocessing
import torch
from torch import nn
import models
from train import fit
from evaluations import examine_lc
from utils import load_config, str2bool, load_new_stimuli
torch.autograd.set_detect_anomaly(True)
import wandb
"""
Main execution script.
"""
def train_model(config_version):
torch.set_num_threads(1)
config = load_config(config_version)
print(f'[Check] config: {config_version}')
random_seed=config['random_seed']
num_blocks=config['num_blocks']
num_runs=config['num_runs']
results_path = f'results/{config_version}'
if not os.path.exists(results_path):
os.makedirs(results_path)
np.random.seed(random_seed)
lc_fast = np.zeros(num_blocks)
lc_slow = np.zeros(num_blocks)
lc_total = np.zeros(num_blocks)
loss_fast = np.zeros(num_blocks)
loss_slow = np.zeros(num_blocks)
loss_total = np.zeros(num_blocks)
ct = 0
for run in range(num_runs):
model = models.FastSlow(config=config)
for epoch in range(num_blocks):
print(f'[Check] run {run}, epoch {epoch}')
dataset = load_new_stimuli()
shuffled_indices = np.random.permutation(len(dataset))
shuffled_dataset = dataset[shuffled_indices]
for i in range(len(shuffled_dataset)):
dp = shuffled_dataset[i]
x = torch.Tensor(dp[0])
y_true = torch.Tensor(dp[1])
signature = dp[2]
model, \
item_proberror_fast, item_proberror_slow, item_proberror_total, \
loss_value_fast, loss_value_slow, loss_value = \
fit(
model=model,
x=x,
y_true=y_true,
signature=signature,
epoch=epoch,
i=i,
)
lc_fast[epoch] += item_proberror_fast
lc_slow[epoch] += item_proberror_slow
lc_total[epoch] += item_proberror_total
loss_fast[epoch] += loss_value_fast
loss_slow[epoch] += loss_value_slow
loss_total[epoch] += loss_value
ct += 1
# save run-level model task
ckpt_data = {}
ckpt_data['state_dict'] = model.state_dict()
torch.save(
ckpt_data,
os.path.join(results_path,
f'run{run}.pth.tar')
)
del model
assert num_runs * num_blocks * len(dataset) == ct, f'got incorrect ct = {ct}'
lc_fast = lc_fast / (num_runs * len(dataset))
lc_slow = lc_slow / (num_runs * len(dataset))
lc_total = lc_total / (num_runs * len(dataset))
loss_fast = loss_fast / (num_runs * len(dataset))
loss_slow = loss_slow / (num_runs * len(dataset))
loss_total = loss_total / (num_runs * len(dataset))
np.save(os.path.join(results_path, f'lc_fast.npy'), lc_fast)
np.save(os.path.join(results_path, f'lc_slow.npy'), lc_slow)
np.save(os.path.join(results_path, f'lc_total.npy'), lc_total)
np.save(os.path.join(results_path, f'loss_fast.npy'), loss_fast)
np.save(os.path.join(results_path, f'loss_slow.npy'), loss_slow)
np.save(os.path.join(results_path, f'loss_total.npy'), loss_total)
def log_results(config_version):
print(f'[Check] logging results..')
# will save locally.
plt = examine_lc(config_version)
# log figures to wandb
wandb.log({"lc": plt})
print(f'[Check] done logging results.')
def train_model_multiproc(config_version):
config = load_config(config_version)
if args.logging:
run = wandb.init(
project="thinking_fast_and_slow",
entity="robandken",
config=config,
reinit=True,
)
wandb.run.name = f'{config_version}'
train_model(config_version)
# log results to wandb
log_results(config_version)
run.finish()
if __name__ == '__main__':
start_time = time.time()
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--logging', dest='logging', default=True, type=str2bool)
parser.add_argument('-c', '--config', dest='config', type=str, default=None)
parser.add_argument('-b', '--begin', dest='begin', type=int, default=None)
parser.add_argument('-e', '--end', dest='end', type=int, default=None)
args = parser.parse_args()
# just run a single config
if args.config:
config_version = args.config
config = load_config(config_version)
if args.logging:
wandb.init(
project="thinking_fast_and_slow",
entity="robandken",
config=config,
reinit=True,
)
wandb.run.name = f'{config_version}'
train_model(config_version)
# log results to wandb
if args.logging:
log_results(config_version)
# run a range of configs (hparams sweep)
elif args.begin and args.end:
# one process is one config
config_versions = [f'config_dlMU_dnn_{i}' for i in range(args.begin, args.end+1)]
num_processes = 40
with multiprocessing.Pool(num_processes) as pool:
for config_version in config_versions:
results = pool.apply_async(
train_model_multiproc,
args=[config_version]
)
pool.close()
pool.join()
duration = time.time() - start_time
print(f'duration = {duration}s')