-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_trish.py
107 lines (95 loc) · 3.23 KB
/
main_trish.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
import json
import argparse
import matplotlib.pyplot as plt
from trish import TRish
from cifar10 import Cifar10
from training import train
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--batch', '-b', default=32, type=int,
help='batch size')
parser.add_argument('--epochs', '-e', default=25, type=int,
help='number of epochs')
parser.add_argument('--alpha', '-a', default=0.1,
type=float, help='alpha')
parser.add_argument('--gamma-1', '-g1', default=1,
type=float, help='gamma 1')
parser.add_argument('--gamma-2', '-g2', default=0.001,
type=float, help='gamma 2')
parser.add_argument('--plot', '-p', action='store_true',
help='use this flag to plot the training history')
parser.add_argument('--save', '-s', default=None,
help='Specify a path to save training history as a JSON file')
args = parser.parse_args()
return args
def main(batch_size, epochs, alpha, gamma_1, gamma_2, plot):
(x_train, y_train), (x_test, y_test) = Cifar10.load_data()
model = Cifar10.load_model()
optimizer = TRish(
alpha=alpha,
gamma_1=gamma_1,
gamma_2=gamma_2
)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
history = train(
model=model,
train_set=(x_train, y_train),
test_set=(x_test, y_test),
batch_size=batch_size,
epochs=epochs
)
# summarize history for accuracy
if plot:
plt.plot(history['training']['accuracy'])
plt.plot(history['test']['accuracy'])
plt.xlabel('epoch')
plt.legend(['training', 'test'], loc='upper left')
plt.show()
return history
if __name__ == '__main__':
args = parse_arguments()
history = main(
batch_size=args.batch,
epochs=args.epochs,
alpha=args.alpha,
gamma_1=args.gamma_1,
gamma_2=args.gamma_2,
plot=args.plot
)
if args.save:
with open(args.save, 'w') as fobj:
json.dump(history, fobj)
"""
alphas = [0.01, 0.1, 1]
gamma_1s = [0.2, 1, 5]
gamma_2s = [0.001, 0.01, 0.1]
batches = [32, 64, 128]
epochs = 25
results = []
for batch in batches:
for alpha in alphas:
for gamma_1 in gamma_1s:
for gamma_2 in gamma_2s:
history = main(
batch_size=batch,
epochs=epochs,
alpha=alpha,
gamma_1=gamma_1,
gamma_2=gamma_2,
plot=False
)
result = {
'params': {
'batch': batch,
'alpha': alpha,
'gamma_1': gamma_1,
'gamma_2': gamma_2
},
'history': history
}
results.append(result)
import json
json.dump(results, open('results/trish.json', 'w'))
"""