-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnn.py
80 lines (62 loc) · 2.24 KB
/
rnn.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import collections
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split
from DatasetLoader import DatasetLoader as Loader
batch_size_fit = 64
# Each input sequence will be of size (28, 28) (height is treated like time).
units = 128
output_size = 3 # labels are from 0 to 3
epochs = 50
# Build the RNN model
def build_model(allow_cudnn_kernel=True):
# CuDNN is only available at the layer level, and not at the cell level.
# This means `LSTM(units)` will use the CuDNN kernel,
# while RNN(LSTMCell(units)) will run on non-CuDNN kernel.
if allow_cudnn_kernel:
# The LSTM layer with default options uses CuDNN.
gru_layer = tf.keras.layers.GRU(units, input_shape=(None, input_dim))
else:
# Wrapping a LSTMCell in a RNN layer will not use CuDNN.
gru_layer = tf.keras.layers.GRU(
tf.keras.layers.LSTMCell(units), input_shape=(None, input_dim)
)
model = tf.keras.models.Sequential(
[
gru_layer,
#tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(output_size,activation='softmax'),
]
)
return model
loader = Loader()
x, y, lens, lenMax = loader.load(datasetPath="out-dataset/dataset-variable-trace-110.npz")
#x, y, lens, lenMax = loader().loadDefault()
input_dim = lenMax
model = build_model(allow_cudnn_kernel=True)
opt = tf.keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(
loss='categorical_crossentropy',
optimizer=opt,
metrics=["accuracy"],
)
x = x.reshape(x.__len__(), 1, lenMax)
print("x.shape",x.shape)
# print(y.shape)
# print(x[0])
# print(y[0])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
model.summary()
model.fit(
x_train, y_train, batch_size=batch_size_fit, epochs=epochs, validation_split=0.2
)
print('\n# Evaluate')
result = model.evaluate(x_test,y_test)
dict(zip(model.metrics_names, result))
model.save('rnn-trace/', save_format="tf")
#prepared for predictions
# Xnew = np.array([[0.89337759, 0.65864154]])
# ynew = model.predict_classes(Xnew)
# print("X=%s, Predicted=%s" % (Xnew[0], ynew[0]))