-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
62 lines (50 loc) · 2 KB
/
Copy pathtrain.py
File metadata and controls
62 lines (50 loc) · 2 KB
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
import os
from keras.callbacks import ModelCheckpoint
from keras.layers import LSTM, Dropout, Dense
from keras.models import Sequential
from data import load, split_data_into_steps
from properties import get_time_steps, get_categories, get_hidden_size, get_features, get_step, get_random_seed, \
get_path_data
import matplotlib.pyplot as plt
print("loading data...")
file_path_data = get_path_data()
data = load(file_path_data)
time_steps = get_time_steps()
features = get_features()
random_seed = get_random_seed()
step = get_step()
categories = get_categories()
hidden_size = get_hidden_size()
#data['activity'].value_counts().plot(kind='bar', title='Activity types')
#plt.show()
x_train, x_test, y_train, y_test, activities = split_data_into_steps(data, time_steps, features, step, random_seed)
model = Sequential()
model.add(LSTM(hidden_size, return_sequences=True, input_shape=(time_steps, features)))
model.add(LSTM(hidden_size))
model.add(Dropout(0.1))
model.add(Dense(categories, activation='sigmoid'))
# model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['mean_squared_error', 'accuracy'])
file_path_best = "models/epoch-loss-acc {epoch:02d}-{loss:.5f}-{acc:.5f}.hdf5"
checkpoint = ModelCheckpoint(file_path_best, monitor='val_loss', verbose=0, save_best_only=False, mode='auto')
callbacks_list = [checkpoint]
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
history = model.fit(x_train, y_train, epochs=100, validation_data=(x_test, y_test), verbose=1, callbacks=callbacks_list)
# Plot training & validation accuracy values
plt.figure(0)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
#plt.show()
# Plot training & validation loss values
plt.figure(1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()