-
Notifications
You must be signed in to change notification settings - Fork 0
/
troll_train.py
136 lines (118 loc) · 4.84 KB
/
troll_train.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
from dataset import Dataset
from Protodeep.model.model import Model
# from layers.Dense import Dense
# from layers.Dense import Dense
from Protodeep.layers.Dense import Dense
from Protodeep.layers.Conv2D import Conv2D
from Protodeep.layers.Flatten import Flatten
from Protodeep.layers.MaxPool2D import MaxPool2D
from Protodeep.regularizers.L1L2 import L1L2
import sys
import Protodeep
# import numpy as np
import matplotlib.pyplot as plt
from Protodeep.callbacks.EarlyStopping import EarlyStopping
import numpy as np
def parse_option_value(opt, dflt):
if opt in sys.argv:
if sys.argv.index(opt) + 1 != len(sys.argv):
return sys.argv[sys.argv.index(opt) + 1]
return dflt
def usage():
print("usage : blabla")
quit()
def check_option(options):
return True
def parse_options():
options = {
'optimizer': parse_option_value('-o', dflt=None),
'epoch': parse_option_value('-e', dflt='100'),
'csv_name': parse_option_value('-n', dflt='data.csv')
}
if check_option(options) is False:
usage()
return options
def get_model_regularizers():
model = Model()
model.add(Protodeep.layers.Dense(64, activation=Protodeep.activations.Relu(), kernel_regularizer=Protodeep.regularizers.L2()))
# model.add(Protodeep.layers.Dense(64, activation=Protodeep.activations.Relu(), kernel_regularizer=''))
model.add(Protodeep.layers.Dense(32, activation=Protodeep.activations.Relu(), kernel_regularizer=Protodeep.regularizers.L1()))
model.add(Protodeep.layers.Dense(2, activation=Protodeep.activations.Softmax()))
model.compile(30, metrics=[Protodeep.metrics.CategoricalAccuracy()], optimizer=Protodeep.optimizers.Adam())
model.summary()
return model
def get_basic_model():
model = Model()
model.add(Protodeep.layers.Dense(64, activation=Protodeep.activations.Relu()))
model.add(Protodeep.layers.Dense(32, activation=Protodeep.activations.Relu()))
model.add(Protodeep.layers.Dense(2, activation=Protodeep.activations.Sigmoid()))
model.compile(30, metrics=[Protodeep.metrics.CategoricalAccuracy()], optimizer=Protodeep.optimizers.Adam())
model.summary()
return model
def get_troll_model_for_bonuses():
model = Model()
model.add(Protodeep.layers.Conv2D(filters=30, kernel_size=(2, 2), activation=Protodeep.activations.Relu()))
# model.add(Protodeep.layers.MaxPool2D(pool_size=(2, 2)))
model.add(Protodeep.layers.Flatten())
model.add(Protodeep.layers.Dense(32, activation=Protodeep.activations.Relu()))
# model.add(Protodeep.layers.Dense(32, activation="linear"))
model.add(Protodeep.layers.Dense(2, activation=Protodeep.activations.Softmax()))
# model.add(Protodeep.layers.64, activation=Protodeep.activations.Relu())
# model.add(Protodeep.layers.32, activation=Protodeep.activations.Relu())
# model.add(Protodeep.layers.2, activation=Protodeep.activations.Softmax())
model.compile((5, 6, 1), metrics=[Protodeep.metrics.CategoricalAccuracy()], optimizer=Protodeep.optimizers.Adam())
model.summary()
return model
if __name__ == "__main__":
options = parse_options()
dataset = Dataset(options['csv_name'], 0.2)
# model = get_basic_mo֧del()
model = get_model_regularizers()
# model = get_troll_model_for_bonuses()
# print(dataset.features.shape)
# print(dataset.test_features.shape)
# history = model.fit(
# features=dataset.features.reshape((455, 5, 6, 1)),
# targets=dataset.targets,
# epochs=500,
# batch_size=32,
# validation_data=(dataset.test_features.reshape((114, 5, 6, 1)), dataset.test_targets),
# callbacks=[EarlyStopping(monitor="val_loss", patience=10)]
# )
# model.evaluate(
# validation_data=(dataset.test_features.reshape((114, 5, 6, 1)), dataset.test_targets)
# )
history = model.fit(
features=dataset.features,
targets=dataset.targets,
epochs=500,
batch_size=32,
validation_data=(dataset.test_features, dataset.test_targets),
callbacks=[EarlyStopping(monitor="val_loss", patience=500)]
)
model.evaluate(
validation_data=(dataset.test_features, dataset.test_targets)
)
# model.save_weights()
plt.plot(history['categorical_accuracy'])
plt.plot(history['val_categorical_accuracy'])
# plt.plot(history['accuracy'])
# plt.plot(history['val_accuracy'])
plt.ylabel('categorical_accuracy')
plt.xlabel('epoch')
plt.legend([
'categorical_accuracy',
'val_categorical_accuracy',
'accuracy',
'val_accuracy'], loc='lower right')
plt.show()
plt.plot(history['loss'])
plt.plot(history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()
# print("val_loss", val_loss)
# e = np.array([1, 2, 3])
# test(e)
# print(e)