-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_lstm.py
58 lines (46 loc) · 1.64 KB
/
train_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
import numpy as np
import pandas as pd
from keras.layers import LSTM, Dense,Dropout
from keras.models import Sequential
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
# Đọc dữ liệu
run_df = pd.read_csv('RUN_TEST.txt')
stand_df = pd.read_csv('STAND_TEST.txt')
yoga_df = pd.read_csv('YOGA_TEST.txt')
X = []
y = []
no_of_timesteps = 10
dataset = run_df.iloc[:,1:].values
n_sample = len(dataset)
for i in range(no_of_timesteps, n_sample):
X.append(dataset[i-no_of_timesteps:i,:])
y.append(0)
dataset = yoga_df.iloc[:,1:].values
n_sample = len(dataset)
for i in range(no_of_timesteps, n_sample):
X.append(dataset[i-no_of_timesteps:i,:])
y.append(1)
dataset = stand_df.iloc[:,1:].values
n_sample = len(dataset)
for i in range(no_of_timesteps, n_sample):
X.append(dataset[i-no_of_timesteps:i,:])
y.append(2)
X, y = np.array(X), np.array(y)
print(X.shape, y.shape)
# Mã hóa one-hot cho nhãn
y = to_categorical(y, num_classes=3)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = Sequential()
model.add(LSTM(units = 50, return_sequences = True, input_shape = (X.shape[1], X.shape[2])))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
model.add(Dense(units = 3, activation="softmax"))
model.compile(optimizer="adam", metrics = ['accuracy'], loss = "categorical_crossentropy")
model.fit(X_train, y_train, epochs=16, batch_size=32,validation_data=(X_test, y_test))
model.save("model.h5")