-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathtutorial10-save-model.py
59 lines (46 loc) · 1.85 KB
/
tutorial10-save-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
# To Avoid GPU errors
physical_devices = tf.config.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(physical_devices[0], True)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28 * 28).astype("float32") / 255.0
x_test = x_test.reshape(-1, 28 * 28).astype("float32") / 255.0
# Alright, so here have some code which should feel familiar from previous tutorials,
# here is what we want to cover
# 1. How to save and load model weights
# 2. Save and loading entire model (Serializing model)
# - Saves weights
# - Model architecture
# - Training Configuration (model.compile())
# - Optimizer and states
model1 = keras.Sequential([layers.Dense(64, activation="relu"), layers.Dense(10)])
inputs = keras.Input(784)
x = layers.Dense(64, activation="relu")(inputs)
outputs = layers.Dense(10)(x)
model2 = keras.Model(inputs=inputs, outputs=outputs)
class MyModel(keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = layers.Dense(64, activation="relu")
self.dense2 = layers.Dense(10)
def call(self, input_tensor):
x = tf.nn.relu(self.dense1(input_tensor))
return self.dense2(x)
# SavedModel format or HDF5 format
model3 = MyModel()
# model = keras.models.load_model('saved_model/')
# model.load_weights('checkpoint_folder/')
model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.Adam(),
metrics=["accuracy"],
)
model.fit(x_train, y_train, batch_size=32, epochs=2, verbose=2)
model.evaluate(x_test, y_test, batch_size=32, verbose=2)
# model.save_weights('checkpoint_folder/')
model.save("saved_model/")