-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSTM.py
62 lines (51 loc) · 1.75 KB
/
LSTM.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from utils import *
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras import metrics
from keras.models import load_model
# Load datasets
periods = read_period_file("calendar.txt")
train_x, train_y, test_x, test_y, last_known_period = make_train_test_sets(periods)
train = True
n_epochs = 4000
if train:
# Configuration
n_steps = 3
n_features = train_x.shape[2]
# LSTM model
model = Sequential()
model.add(LSTM(100, activation="relu",
return_sequences=True,
input_shape=(n_steps, n_features)))
model.add(LSTM(100, activation="relu"))
model.add(Dense(n_features))
model.compile(optimizer="adam",
loss="mse",
metrics=[metrics.mae, metrics.mape])
# fit model
model.fit(train_x, train_y,
epochs=n_epochs,
verbose=2,
#validation_data=(test_x, test_y)
)
model.evaluate(test_x, test_y)
# Serialize model to HDF5
model.save("lstm_" + str(n_epochs) + ".h5")
print("Saved model to disk")
else:
# Load model if saved
model = load_model("lstm_" + str(n_epochs) + ".h5")
model.summary()
# Make predictions
y_preds = model.predict(test_x, verbose=0)
predictions = [[int(round(i[0])), int(round(i[1]))] for i in y_preds]
accuracies = evaluate_predictions(test_y, predictions)
print("Accuracy of menstrual cycle length prediction: ", round(accuracies[0], 4))
print("Accuracy of menstruation length prediction: ", round(accuracies[1], 4))
# print("Next periods: ")
# next_periods = print_predictions(last_known_period, predictions)
print(len(train_y))
print(len(test_y))