forked from Hear-Me-Out/Hear-Me-Out
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
65 lines (59 loc) · 2.21 KB
/
training.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
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Activation
from keras import optimizers
from keras.preprocessing import sequence
from keras.models import model_from_json
#load Training and Validation data
train_data = np.load('Preprocessed_Data/train_data.npy')
val_data = np.load('Preprocessed_Data/val_data.npy')
test_data = np.load('Preprocessed_Data/test_data.npy')
#load Training and Validation labels
train_label = np.load('Preprocessed_Data/train_label.npy')
val_label = np.load('Preprocessed_Data/val_label.npy')
test_label = np.load('Preprocessed_Data/test_label.npy')
labels =np.load('Preprocessed_Data/labels_list.npy')
model = Sequential([
LSTM(50, input_shape=(50, 11)),
Dense(100),
Dropout(0.4),
Dense(len(labels), activation='softmax'),
])
model.summary()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
hist = model.fit(train_data, train_label, validation_data = (val_data, val_label),
batch_size=64, epochs=30)
loss, accuracy = model.evaluate(test_data, test_label)
print(loss, accuracy)
#save model
model_json = model.to_json()
with open("Model/model.json", "w") as json_file:
json_file.write(model_json)
model.save_weights("Model/model.h5")
print("Saved model to disk")
#Plot Loss and Accuracy Curves
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
plt.subplot(211)
x = np.arange(1,len(hist.history['acc'])+1)
plt.scatter(x, hist.history['acc'], color='blue', s=10)
plt.plot(x,hist.history['acc'], 'b')
plt.scatter(x, hist.history['val_acc'], color='green', s=10)
plt.plot(x, hist.history['val_acc'], 'g')
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.legend(['Training', 'Validation'], loc='lower right')
plt.subplot(212)
plt.scatter(x, hist.history['loss'], color='blue', s=10)
plt.plot(x,hist.history['loss'], 'b')
plt.scatter(x, hist.history['val_loss'], color='green', s=10)
plt.plot(x, hist.history['val_loss'], 'g')
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='upper right')
plt.savefig('plots/loss_acc_curves.png',bbox_inches='tight')