-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_classifier.py
84 lines (69 loc) · 2.49 KB
/
simple_classifier.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
from tensorflow import keras
# import tensorflow.compat.v1 as tf
import tensorflow as tf
import numpy as np
import DatasetLoader as loader
from sklearn.model_selection import train_test_split
# tf.disable_v2_behavior()
tf.compat.v1.disable_v2_behavior()
modelOutName = "simple-demo-classifier.h5"
class Classifier:
def __init__(self, stateVariableLengths, stateLengths):
# define model
model = keras.Sequential(
[
keras.layers.Dense(8, input_shape=(stateVariableLengths, stateLengths)),
keras.layers.Dense(8, activation="relu"),
keras.layers.Dense(4),
]
)
model.compile(
optimizer="adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"],
)
self.model = model
def fit(self, x, y):
"fit & save & try predict"
model = self.model
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
x_train, x_val, y_train, y_val = train_test_split(
x_train, y_train, test_size=0.2
)
model.fit(x_train, y_train, epochs=100)
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print("\nTest accuracy:", test_acc)
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict(x_test)
val_prediction = probability_model.predict(x_val)
model.save(modelOutName)
# Recreate the exact same model, including its weights and the optimizer
the_model = tf.keras.models.load_model(modelOutName)
# Show the model architecture
the_model.summary()
print(
"Predition:",
"Result 1",
predictions[0],
np.argmax(predictions[0]),
y_test[0],
"Validation",
val_prediction[0],
"Result 2",
predictions[1],
np.argmax(predictions[1]),
y_test[1],
"Validation",
val_prediction[0],
"Result 3",
predictions[2],
np.argmax(predictions[2]),
y_test[2],
"Validation",
val_prediction[0],
sep="\n",
)
traces, labels, lengths, lengthMax = loader.loadVariableTrace()
print(traces, labels, lengthMax)
classifier = Classifier(stateVariableLengths=labels, stateLengths=lengthMax)
classifier.fit(traces, labels)