-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_and_train.py
174 lines (152 loc) · 6.91 KB
/
prepare_and_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
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
# %%
from keras.optimizer_v2.adam import Adam
from ml_utils import load_data, plot_results, load_model, model_names
from sklearn.utils import class_weight
import numpy as np
import matplotlib.pyplot as plt
# %%
logs_path = './exported_logs/'
# %% Load data once
(train_x, train_y), (test_x, test_y) = load_data(split=0.35,
logs_path=logs_path,
stride=50,
bin_size=150,
transpose=True,
simplify_classes=False)
# %% Train for data static parameters
model = load_model('LSTM_A', input_shape=(train_x.shape[1:]), output_dim=train_y.shape[1])
opt = Adam(learning_rate=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
y_ints = [d.argmax() for d in train_y]
class_weights = class_weight.compute_class_weight(
class_weight = "balanced",
classes = np.unique(y_ints),
y = y_ints
)
class_weights = dict(enumerate(class_weights))
history = model.fit(train_x, train_y, epochs=100, validation_data=(test_x, test_y), verbose=0, class_weight=class_weights)
plot_results(history, title=f"model: LSTM_A epochs: 100")
# %% loop over split, bin_size and stride.
ep = 60
name = 'LSTM_A'
fig, axs = plt.subplots(2, 2, figsize=(16, 10))
fig.suptitle(f"{name} over different parameters, epochs:{ep}")
for x in axs:
for y in x:
y.set_xlabel("epochs")
axs[0][0].set_ylabel('training loss')
axs[0][1].set_ylabel('training accuracy')
axs[0][1].set_ylim(bottom=0, top=1)
axs[1][0].set_ylabel('val loss')
axs[1][1].set_ylabel('val accuracy')
axs[1][1].set_ylim(bottom=0, top=1)
for split in [0.25, 0.35]:
for bin_size in [150, 200]:
for stride in [50, 100]:
(train_x, train_y), (test_x, test_y) = load_data(split=split,
logs_path=logs_path,
stride=stride,
bin_size=bin_size,
transpose=True,
simplify_classes=True)
model = load_model(name, input_shape=(train_x.shape[1:]), output_dim=train_y.shape[1])
opt = Adam(learning_rate=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
y_ints = [d.argmax() for d in train_y]
class_weights = class_weight.compute_class_weight(
class_weight = "balanced",
classes = np.unique(y_ints),
y = y_ints
)
class_weights = dict(enumerate(class_weights))
history = model.fit(train_x, train_y, epochs=ep, validation_data=(test_x, test_y), verbose=0, class_weight=class_weights)
axs[0][0].plot(history.history['loss'], label=f"splt{split}_bin{bin_size}_stride{stride}")
axs[0][1].plot(history.history['accuracy'], label=f"splt{split}_bin{bin_size}_stride{stride}")
axs[1][0].plot(history.history['val_loss'], label=f"splt{split}_bin{bin_size}_stride{stride}")
axs[1][1].plot(history.history['val_accuracy'], label=f"splt{split}_bin{bin_size}_stride{stride}")
for x in axs:
for y in x:
y.legend()
y.grid()
plt.tight_layout()
plt.savefig(f'./multi_dim_test/{name}_parameter_comparsion_{ep}epochs.jpg')
plt.show()
# %% Test all models over different epochs
for ep in [10, 30, 100]:
fig, axs = plt.subplots(2, 2, figsize=(16, 10))
fig.suptitle(f"Model comparison, epochs:{ep}")
for x in axs:
for y in x:
y.set_xlabel("epochs")
axs[0][1].set_ylim(bottom=0, top=1)
axs[1][1].set_ylim(bottom=0, top=1)
axs[0][0].set_ylabel('training loss')
axs[0][1].set_ylabel('training accuracy')
axs[1][0].set_ylabel('val loss')
axs[1][1].set_ylabel('val accuracy')
for name in model_names:
model = load_model(name, input_shape=(train_x.shape[1:]), output_dim=train_y.shape[1])
opt = Adam(learning_rate=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
y_ints = [d.argmax() for d in train_y]
class_weights = class_weight.compute_class_weight(
class_weight = "balanced",
classes = np.unique(y_ints),
y = y_ints
)
class_weights = dict(enumerate(class_weights))
history = model.fit(train_x, train_y, epochs=ep, validation_data=(test_x, test_y), verbose=0, class_weight=class_weights)
axs[0][0].plot(history.history['loss'], label=name)
axs[0][1].plot(history.history['accuracy'], label=name)
axs[1][0].plot(history.history['val_loss'], label=name)
axs[1][1].plot(history.history['val_accuracy'], label=name)
for x in axs:
for y in x:
y.legend()
y.grid()
plt.tight_layout()
plt.savefig(f'./test_results/model_comparison_{ep}epochs.jpg')
plt.show()
# %% Test all models over different epochs using all classes
(train_x, train_y), (test_x, test_y) = load_data(split=0.35,
logs_path=logs_path,
stride=50,
bin_size=150,
transpose=True,
simplify_classes=False)
ep = 60
fig, axs = plt.subplots(2, 2, figsize=(16, 10))
fig.suptitle(f"Model comparison, epochs:{ep}")
for x in axs:
for y in x:
y.set_xlabel("epochs")
axs[0][1].set_ylim(bottom=0, top=1)
axs[1][1].set_ylim(bottom=0, top=1)
axs[0][0].set_ylabel('training loss')
axs[0][1].set_ylabel('training accuracy')
axs[1][0].set_ylabel('val loss')
axs[1][1].set_ylabel('val accuracy')
for name in model_names:
model = load_model(name, input_shape=(train_x.shape[1:]), output_dim=train_y.shape[1])
opt = Adam(learning_rate=1e-3, decay=1e-5)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
y_ints = [d.argmax() for d in train_y]
class_weights = class_weight.compute_class_weight(
class_weight = "balanced",
classes = np.unique(y_ints),
y = y_ints
)
class_weights = dict(enumerate(class_weights))
history = model.fit(train_x, train_y, epochs=ep, validation_data=(test_x, test_y), verbose=0, class_weight=class_weights)
axs[0][0].plot(history.history['loss'], label=name)
axs[0][1].plot(history.history['accuracy'], label=name)
axs[1][0].plot(history.history['val_loss'], label=name)
axs[1][1].plot(history.history['val_accuracy'], label=name)
for x in axs:
for y in x:
y.legend()
y.grid()
plt.tight_layout()
plt.savefig(f'./test_results/model_comparison_{ep}epochs_all_classes.jpg')
plt.show()
# %%