11.1 Saving and Restoring TensorFlow Models
import os
import numpy as np
np.random.seed(123)
print("NumPy:{}".format(np.__version__))
import tensorflow as tf
tf.set_random_seed(123)
print("TensorFlow:{}".format(tf.__version__))
NumPy:1.13.3
Pandas:0.21.0
Matplotlib:2.1.0
TensorFlow:1.4.0
Using TensorFlow backend.
Keras:2.0.9
DATASETSLIB_HOME = os.path.expanduser('~/dl-ts/datasetslib')
import sys
if not DATASETSLIB_HOME in sys.path:
sys.path.append(DATASETSLIB_HOME)
%reload_ext autoreload
%autoreload 2
import datasetslib
from datasetslib import util as dsu
datasetslib.datasets_root = os.path.join(os.path.expanduser('~'),'datasets')
models_root = os.path.join(os.path.expanduser('~'),'models')
Saving / Restoring Model in TensorFlow
Saving all variables in a graph
tf.reset_default_graph()
w = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
y = w * x + b
output = 0
saver = tf.train.Saver()
with tf.Session() as tfs:
tfs.run(tf.global_variables_initializer())
output = tfs.run(y,{x:[1,2,3,4]})
saved_model_file = saver.save(tfs,'saved-models/full-graph-save-example.ckpt')
print('Model saved in {}'.format(saved_model_file))
print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))
print('output={}'.format(output))
Model saved in saved-models/full-graph-save-example.ckpt
Values of variables w,b: [ 0.30000001][-0.30000001]
output=[ 0. 0.30000001 0.60000002 0.90000004]
Restoring all variables from a graph
tf.reset_default_graph()
w = tf.Variable([0], dtype=tf.float32)
b = tf.Variable([0], dtype=tf.float32)
x = tf.placeholder(dtype=tf.float32)
y = w * x + b
output = 0
saver = tf.train.Saver()
with tf.Session() as tfs:
saved_model_file = saver.restore(tfs,'saved-models/full-graph-save-example.ckpt')
print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))
output = tfs.run(y,{x:[1,2,3,4]})
print('output={}'.format(output))
INFO:tensorflow:Restoring parameters from saved-models/full-graph-save-example.ckpt
Values of variables w,b: [ 0.30000001][-0.30000001]
output=[ 0. 0.30000001 0.60000002 0.90000004]
Saving selected variables in a graph
tf.reset_default_graph()
w = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
y = w * x + b
output = 0
saver = tf.train.Saver({'weights': w})
with tf.Session() as tfs:
tfs.run(tf.global_variables_initializer())
output = tfs.run(y,{x:[1,2,3,4]})
saved_model_file = saver.save(tfs,'saved-models/weights-save-example.ckpt')
print('Model saved in {}'.format(saved_model_file))
print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))
print('output={}'.format(output))
Model saved in saved-models/weights-save-example.ckpt
Values of variables w,b: [ 0.30000001][-0.30000001]
output=[ 0. 0.30000001 0.60000002 0.90000004]
Restoring selected variables in a graph
tf.reset_default_graph()
w = tf.Variable([0], dtype=tf.float32)
b = tf.Variable([0], dtype=tf.float32)
x = tf.placeholder(dtype=tf.float32)
y = w * x + b
output = 0
saver = tf.train.Saver({'weights': w})
with tf.Session() as tfs:
b.initializer.run()
saved_model_file = saver.restore(tfs,'saved-models/weights-save-example.ckpt')
print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))
output = tfs.run(y,{x:[1,2,3,4]})
print('output={}'.format(output))
INFO:tensorflow:Restoring parameters from saved-models/weights-save-example.ckpt
Values of variables w,b: [ 0.30000001][ 0.]
output=[ 0.30000001 0.60000002 0.90000004 1.20000005]