-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmnist_baseline_model.py
39 lines (29 loc) · 1.16 KB
/
mnist_baseline_model.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
import tensorflow as tf
mnist = tf.keras.datasets.mnist
# loading data
(x_train, y_train),(x_test, y_test) = mnist.load_data()
# converting from 0-255 to 0-1
x_train, x_test = x_train / 255.0, x_test / 255.0
# building a model
model = tf.keras.models.Sequential()
# flatten first layer of neurons
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
# addind first and second hidden layers of neurons
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
# adding output layer of neurons
model.add(tf.keras.layers.Dense(10, activation = tf.nn.softmax))
# compiling model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# training model
model.fit(x_train, y_train, epochs=5)
# model results
model.evaluate(x_test, y_test)
# saving model
saved_model_path = tf.contrib.saved_model.save_keras_model(model, "./saved_other_model")
# converting to tf lite
converter = tf.lite.TFLiteConverter.from_saved_model("./saved_other_model")
tflite_model = converter.convert()
open("converted_other_model.tflite", "wb").write(tflite_model)