-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnn-model3b.py
87 lines (71 loc) · 2.85 KB
/
rnn-model3b.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
81
82
83
84
85
86
87
import collections
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Concatenate, Dense, LSTM, Input, concatenate
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
import DatasetLoader as Loader
# config
batch_size_fit = 100
units = 50
output_size = 5 # labels are from 0 to 3
epochs = 650
# dataset
(x1, x2), y, lengths, lengthsMax, exeNames, roleInStates = Loader.stateTrace.r5.load(model='3')
features = roleInStates.max()
####################################model########################################
first_input = Input(shape=(300, 1))
first_GRU = tf.keras.layers.GRU(units)(first_input)
first_dense = tf.keras.layers.Dense(output_size, activation="sigmoid")(first_GRU)
second_input = Input(shape=(300, features - 1))
second_GRU = tf.keras.layers.GRU(units)(second_input)
second_dense = tf.keras.layers.Dense(output_size, activation="sigmoid")(second_GRU)
merged = concatenate([first_dense, second_dense])
out = tf.keras.layers.Dense(output_size, activation="softmax")(merged)
model = tf.keras.models.Model(inputs=[first_input, second_input], outputs=out)
#################################################################################
#model
#adam may be too aggresive
opt = tf.keras.optimizers.Adam(
lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False
)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
#training data and validation data
x1_train, x1_validation, x2_train, x2_validation, y_train, y_validation = train_test_split(x1, x2, y, test_size=0.2)
x1_train, x1_test, x2_train, x2_test, y_train, y_test = train_test_split(x1_train, x2_train, y_train, test_size=0.2)
model.summary()
# callback = tf.keras.callbacks.EarlyStopping(
# monitor="val_loss", min_delta=0, patience=5, verbose=0, mode="auto"
# )
checkpoint = tf.keras.callbacks.ModelCheckpoint(
"rnn-stateTrace/model3b_checkpoint/",
monitor="val_accuracy",
verbose=1,
save_best_only=True,
mode="max",
)
history = model.fit(
[x1_train, x2_train], y_train, batch_size=batch_size_fit, epochs=epochs, validation_data=([x1_test, x2_test],y_test), callbacks = [checkpoint]
)
print("\n# Evaluate")
result = model.evaluate([x1_validation, x2_validation], y_validation)
dict(zip(model.metrics_names, result))
model.save("rnn-stateTrace/model3b/", save_format="tf")
# draw loss
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
plt.title("Model loss")
plt.ylabel("Loss")
plt.xlabel("Epoch")
plt.legend(["Train", "Test"], loc="upper left")
plt.show()
# draw acc
plt.plot(history.history["accuracy"])
plt.plot(history.history["val_accuracy"])
plt.title("Model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Train", "Test"], loc="upper left")
plt.show()