-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathscript_params.py
75 lines (59 loc) · 2.29 KB
/
script_params.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
import yaml
import numpy as np
import torch
from study.vanilla import vanilla_rnn
# Will be setting the random seed before every run
rand_seed = 2019
# The starting configuration is whatever is in the file below
start_file = './study/vanilla/laptop_rnn.yml'
# Define various parameter values to be explored
params = {
'window_size': ['', '1000']
}
# We'll print results to file so that we can access later
out_file = './study/vanilla/params_3vowels_nocv.txt'
# Temp file we'll use do define configurations
temp_file = './study/vanilla/temp.yml'
if __name__ == '__main__':
start_conf = open(start_file, 'r')
str_cnf = start_conf.read()
print_out = open(out_file, 'w')
# Print out starting configuration
print('Starting configuration: \n', file=print_out)
with open(start_file, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
print(yaml.dump(cfg, default_flow_style=False), file=print_out)
# Run the starting configuration
torch.manual_seed(rand_seed)
np.random.seed(rand_seed)
# (acc_train_st, acc_test_st) = vanilla_rnn.main(['--config', start_file])
# print('Starting conf. train accuracy: %1.2f; test accuracy: %1.2f. \n' % (np.float16(acc_train_st), np.float16(acc_test_st)), file=print_out)
# Iterate over all parameters that are to be changed
for key, values in params.items():
# Get the starting value from the file
ind_param = str_cnf.find(key + ': ')
ind_val = ind_param + len(key + ': ')
vc = 0
val = ''
c = str_cnf[ind_val]
while c != '\n':
val += c
vc += 1
c = str_cnf[ind_val + vc]
# Iterate over all values of each parameter
acc_train_temp, acc_test_temp = [], []
for iv, value in enumerate(values):
str_temp = str_cnf[:ind_val] + str(value) + str_cnf[ind_val+vc:]
temp_cnf = open(temp_file, 'w')
temp_cnf.write(str_temp)
temp_cnf.close()
# Run the temporary configuration
torch.manual_seed(rand_seed)
np.random.seed(rand_seed)
(acc_train, acc_test) = vanilla_rnn.main(['--config', temp_file])
acc_train_temp.append(acc_train)
acc_test_temp.append(acc_test)
print('For parameter ' + key + ' with values ' + str(values) + ' accuracies are, respectively:', file=print_out)
print('Training: ' + str(np.float16(acc_train_temp)), file=print_out)
print('Testing: ' + str(np.float16(acc_test_temp)) + '\n', file=print_out)
print_out.close()