-
Notifications
You must be signed in to change notification settings - Fork 44
/
expr.py
148 lines (133 loc) · 4.77 KB
/
expr.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
import sys
import json
import pickle
import argparse
from lop.nets.ffnn import FFNN
from lop.nets.linear import MyLinear
from lop.algos.bp import Backprop
from lop.algos.cbp import ContinualBackprop
from lop.utils.miscellaneous import *
def expr(params: {}):
agent_type = params['agent']
env_file = params['env_file']
num_data_points = int(params['num_data_points'])
to_log = False
to_log_grad = False
to_log_activation = False
beta_1 = 0.9
beta_2 = 0.999
weight_decay = 0.0
accumulate = False
perturb_scale = 0
if 'to_log' in params.keys():
to_log = params['to_log']
if 'to_log_grad' in params.keys():
to_log_grad = params['to_log_grad']
if 'to_log_activation' in params.keys():
to_log_activation = params['to_log_activation']
if 'beta_1' in params.keys():
beta_1 = params['beta_1']
if 'beta_2' in params.keys():
beta_2 = params['beta_2']
if 'weight_decay' in params.keys():
weight_decay = params['weight_decay']
if 'accumulate' in params.keys():
accumulate = params['accumulate']
if 'perturb_scale' in params.keys():
perturb_scale = params['perturb_scale']
num_inputs = params['num_inputs']
num_features = params['num_features']
hidden_activation = params['hidden_activation']
step_size = params['step_size']
opt = params['opt']
replacement_rate = params["replacement_rate"]
decay_rate = params["decay_rate"]
mt = 10
util_type='adaptable_contribution'
init = 'kaiming'
if "mt" in params.keys():
mt = params["mt"]
if "util_type" in params.keys():
util_type = params["util_type"]
if "init" in params.keys():
init = params["init"]
if agent_type == 'linear':
net = MyLinear(
input_size=num_inputs,
)
else:
net = FFNN(
input_size=num_inputs,
num_features=num_features,
hidden_activation=hidden_activation,
)
if agent_type == 'bp' or agent_type == 'linear' or agent_type == 'l2':
learner = Backprop(
net=net,
step_size=step_size,
opt=opt,
beta_1=beta_1,
beta_2=beta_2,
weight_decay=weight_decay,
to_perturb=(perturb_scale > 0),
perturb_scale=perturb_scale,
)
elif agent_type == 'cbp':
learner = ContinualBackprop(
net=net,
step_size=step_size,
opt=opt,
replacement_rate=replacement_rate,
decay_rate=decay_rate,
device='cpu',
maturity_threshold=mt,
util_type=util_type,
init=init,
accumulate=accumulate,
)
with open(env_file, 'rb+') as f:
inputs, outputs, _ = pickle.load(f)
errs = torch.zeros((num_data_points), dtype=torch.float)
if to_log: weight_mag = torch.zeros((num_data_points, 2), dtype=torch.float)
if to_log_grad: grad_mag = torch.zeros((num_data_points, 2), dtype=torch.float)
if to_log_activation: activation = torch.zeros((num_data_points, ), dtype=torch.float)
for i in tqdm(range(num_data_points)):
x, y = inputs[i: i+1], outputs[i: i+1]
err = learner.learn(x=x, target=y)
if to_log:
weight_mag[i][0] = learner.net.layers[0].weight.data.abs().mean()
weight_mag[i][1] = learner.net.layers[-1].weight.data.abs().mean()
if to_log_grad:
grad_mag[i][0] = learner.net.layers[0].weight.grad.data.abs().mean()
grad_mag[i][1] = learner.net.layers[-1].weight.grad.data.abs().mean()
if to_log_activation:
if hidden_activation == 'relu':
activation[i] = (learner.previous_features[0] == 0).float().mean()
if hidden_activation == 'tanh':
activation[i] = (learner.previous_features[0].abs() > 0.9).float().mean()
errs[i] = err
data_to_save = {
'errs': errs.numpy()
}
if to_log:
data_to_save['weight_mag'] = weight_mag.numpy()
if to_log_grad:
data_to_save['grad_mag'] = grad_mag.numpy()
if to_log_activation:
data_to_save['activation'] = activation.numpy()
return data_to_save
def main(arguments):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-c', help="Path to the file containing the parameters for the experiment",
type=str, default='temp_cfg/0.json')
args = parser.parse_args(arguments)
cfg_file = args.c
with open(cfg_file, 'r') as f:
params = json.load(f)
data = expr(params)
with open(params['data_file'], 'wb+') as f:
pickle.dump(data, f)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))