-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathsave-load.py
67 lines (51 loc) · 1.64 KB
/
save-load.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
import tensorflow as tf
from tensorflow import keras
import numpy as np
import os
data_x = np.random.normal(size=[1000, 1])
noise = np.random.normal(size=[1000, 1]) * 0.2
data_y = data_x * 3. + 2. + noise
train_x, train_y = data_x[:900], data_y[:900]
test_x, test_y = data_x[900:], data_y[900:]
def create_model():
return keras.models.Sequential([
keras.layers.Dense(10, activation=keras.activations.relu, input_shape=(1, )),
keras.layers.Dense(1),
])
model = create_model()
model.compile(
optimizer=keras.optimizers.SGD(0.01),
loss=keras.losses.MeanSquaredError(),
)
checkpoint_path = "training/cp-{epoch:04d}.ckpt"
ckpt_dir = os.path.dirname(checkpoint_path)
os.makedirs(ckpt_dir, exist_ok=True)
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,
save_weights_only=True,
verbose=1,
period=5
)
# save ckpt
model.save_weights(checkpoint_path.format(epoch=0))
history = model.fit(
train_x, train_y, batch_size=32, epochs=10, validation_data=(test_x, test_y), shuffle=True,
callbacks=[cp_callback, ] # save when callback
)
# restore ckpt
latest_model = tf.train.latest_checkpoint(ckpt_dir)
model2 = create_model()
model2.load_weights(latest_model)
model2.compile(
optimizer=keras.optimizers.SGD(0.01),
loss=keras.losses.MeanSquaredError(),
)
loss = model2.evaluate(test_x, test_y, verbose=2)
print("Restored ckpt model, loss: {:.2f}".format(loss))
# save pb
final_path = "training/final_model"
model.save(final_path)
# restore
model3 = keras.models.load_model(final_path)
loss = model3.evaluate(test_x, test_y, verbose=2)
print("Restored pb model, loss: {:.2f}".format(loss))