-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_LSTM.py
More file actions
72 lines (55 loc) · 2.09 KB
/
main_LSTM.py
File metadata and controls
72 lines (55 loc) · 2.09 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
63
64
65
66
67
68
69
70
71
72
import numpy as np
import pandas as pd
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, LSTM
from keras.callbacks import EarlyStopping, ModelCheckpoint
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
def preprocess(mtx):
# arr_of_lists = []
# for i in range(0, mtx.shape[0] - 150, 50):
# list = []
# slice = mtx[i:i + 150, 0:3]
# for arr in slice:
# list.append(arr)#np.asarray(arr))
# arr_of_lists.append(list)
# return arr_of_lists
done = np.empty((0, 150, 3))
for i in range(0, mtx.shape[0] - 150, 50):
slice = mtx[i:i + 150, 0:3]
done = np.append(done, np.expand_dims(slice, axis=0), axis=0)
return done
# importing, preprocessing data
# data0
mtx0 = pd.read_csv("data_gabor.csv").as_matrix()
print("data0 loaded")
# data1
mtx1 = pd.read_csv("data_dani.csv").as_matrix()
print("data1 loaded")
# creating sequences
sequence0 = preprocess(mtx0)
print('sequence0 created')
sequence1 = preprocess(mtx1)
print('sequence1 created')
sequence = np.append(sequence0, sequence1, axis=0)
# creating labels
label0 = np.zeros([sequence0.shape[0]])
label1 = np.ones([sequence1.shape[0]])
labels = np.append(label0, label1)
# permutating
permutation_array = np.arange(sequence.shape[0]) # ezzel keverjük össze az adatokat
permutation_array = np.random.permutation(permutation_array) # összekeverjük
sequence = sequence[permutation_array] # indexeljük
labels = labels[permutation_array]
# train-validation data split
inputs_train, inputs_val, labels_train, labels_val = train_test_split(sequence, labels, test_size=0.33)
#creating model
model = Sequential()
model.add(LSTM(input_shape=(None, 3), units=50, return_sequences=False))
model.add(Dropout(0.2))
# model.add(Dense(
# output_dim=1, activation='linear'))
model.add(Dense(units=1, activation="linear"))
model.compile(loss='mse', optimizer='rmsprop', metrics=['acc'])
model.fit(inputs_train, labels_train, validation_data=(inputs_val, labels_val))