-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
59 lines (48 loc) · 1.92 KB
/
train.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
import numpy as np
import pandas as pd
from keras.layers import Dense, Conv1D, MaxPooling1D, BatchNormalization, Dropout, Activation, MaxPooling2D, Flatten
from keras.models import Sequential, load_model
from keras.losses import categorical_crossentropy, binary_crossentropy
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
data = pd.read_csv('data/dataset.csv')
train_y = data['3600']
train_X = data.drop('3600', axis=1)
test = data.sample(frac=0.2)
test_y = test['3600']
test_X = test.drop('3600', axis=1)
train_y = to_categorical(train_y, 17)
test_y = to_categorical(test_y, 17)
train_X = np.asarray(train_X).reshape(-1,3600,1)
train_y = np.asarray(train_y)
test_X = np.asarray(test_X).reshape(-1,3600,1)
test_y = np.asarray(test_y)
def cnn_model():
model = Sequential()
model.add(Conv1D(128,50,activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=(2)))
model.add(Conv1D(32,7,activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=(2)))
model.add(Conv1D(32,10,activation='relu'))
model.add(BatchNormalization())
model.add(Conv1D(128,5,activation='relu'))
model.add(MaxPooling1D(pool_size=(2)))
model.add(Conv1D(256,15,activation='relu'))
model.add(MaxPooling1D(pool_size=(2)))
model.add(Conv1D(512,5,activation='relu'))
model.add(Conv1D(128,3,activation='relu'))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dense(17,activation='softmax'))
return model
def model_fit(model):
model = cnn_model()
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
model.fit(x=train_X, y=train_y, epochs=10, batch_size=16, verbose = 2, validation_data=(test_X, test_y))
model.save('models/cnn_model_1.h5')
model = cnn_model()
model_fit(model)