-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRU_Model.py
More file actions
59 lines (52 loc) · 1.91 KB
/
GRU_Model.py
File metadata and controls
59 lines (52 loc) · 1.91 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
from sklearn.model_selection import train_test_split
from tensorflow import keras as keras
from keras.layers import GRU
from keras.layers import BatchNormalization
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Activation
import os
import matplotlib.pyplot as plt
class GRU_Model:
def __init__(self,inputs,targets):
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
self.INPUT = inputs
self.OUTPUT = targets
self.OUTPUT_UNITS = 88
self.LOSS_FUNCTION = 'sparse_categorical_crossentropy'
self.LEARNING_RATE = 0.001
self.BATCH_SIZE = 128
def build_model(self):
model = keras.models.Sequential()
model.add(GRU(
512,
input_shape = (self.INPUT.shape[1],self.INPUT.shape[2]),
recurrent_dropout=0,
return_sequences=True
))
model.add(GRU(512,return_sequences=True,recurrent_dropout=0))
model.add(GRU(512))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Dense(88))
model.add(Activation('softmax'))
model.compile(loss=self.LOSS_FUNCTION,
optimizer=keras.optimizers.Adam(learning_rate=self.LEARNING_RATE),
metrics=['accuracy'])
model.summary()
return model
def train(self):
model = self.build_model()
history = model.fit(self.INPUT,self.OUTPUT,epochs=15,batch_size=self.BATCH_SIZE)
plt.plot(history.history['accuracy'])
plt.plot(history.history['loss'])
plt.title('Accuracy vs Loss')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Accuracy','Loss'],loc='upper left')
plt.show()
model.save('model_gru.h5')