为了让训练好模型可以复用,需要将神经网络结果持久化。TensorFlow提供了一个简单的API来保存和恢复神经网络模型。这个API是tf.train.Saver.
import tensorflow as tf
v1 = tf. Variable( tf. constant( 1.0 , shape= [ 1 ] ) , name = 'v1' )
v2 = tf. Variable( tf. constant( 2.0 , shape= [ 1 ] ) , name = 'v2' )
result = v1 + v2
init_op = tf. global_variables_initializer( )
saver = tf. train. Saver( )
with tf. Session( ) as sess:
sess. run( init_op)
saver. save( sess, "./mymodel/model.ckpt" )
import tensorflow as tf
v1 = tf. Variable( tf. constant( 1.0 , shape= [ 1 ] ) , name = 'v1' )
v2 = tf. Variable( tf. constant( 2.0 , shape= [ 1 ] ) , name = 'v2' )
result = v1 + v2
saver = tf. train. Saver( )
with tf. Session( ) as sess:
saver. restore( sess, "./mymodel/model.ckpt" )
print ( sess. run( result) )
INFO:tensorflow:Restoring parameters from ./mymodel/model.ckpt
[ 3.]
savers = tf. train. import_meta_graph( "./mymodel/model.ckpt.meta" )
with tf. Session( ) as sess:
saver. restore( sess, "./mymodel/model.ckpt" )
print ( sess. run( tf. get_default_graph( ) . get_tensor_by_name( "add:0" ) ) )
INFO:tensorflow:Restoring parameters from ./mymodel/model.ckpt
[ 3.]
v1 = tf. Variable( tf. constant( 1.0 , shape= [ 1 ] ) , name = "other-v1" )
v2 = tf. Variable( tf. constant( 2.0 , shape= [ 1 ] ) , name = "other-v2" )
saver = tf. train. Saver( { 'v1' : v1, 'v2' : v2} )
import tensorflow as tf
v = tf. Variable( 0 , dtype = tf. float32, name = 'v' )
for variables in tf. global_variables( ) :
print ( variables. name)
v:0
ema = tf. train. ExponentialMovingAverage( 0.99 )
maintain_averages_op = ema. apply ( tf. global_variables( ) )
for variables in tf. global_variables( ) :
print ( variables. name)
v:0
v/ExponentialMovingAverage:0
saver = tf. train. Saver( )
with tf. Session( ) as sess:
init_op = tf. global_variables_initializer( )
sess. run( init_op)
sess. run( tf. assign( v, 10 ) )
sess. run( maintain_averages_op)
saver. save( sess, "./emamodel/model.ckpt" )
print ( sess. run( [ v, ema. average( v) ] ) )
[10.0, 0.099999905]
saver = tf. train. Saver( { "v/ExponentialMovingAverage" : v} )
with tf. Session( ) as sess:
saver. restore( sess, './emamodel/model.ckpt' )
print ( sess. run( v) )
INFO:tensorflow:Restoring parameters from ./emamodel/model.ckpt
0.0999999
import tensorflow as tf
v = tf. Variable( 0 , dtype = tf. float32, name = 'v' )
ema = tf. train. ExponentialMovingAverage( 0.99 )
print ( ema. variables_to_restore( ) )
saver = tf. train. Saver( ema. variables_to_restore( ) )
with tf. Session( ) as sess:
saver. restore( sess, './emamodel/model.ckpt' )
print ( sess. run( v) )
{'v/ExponentialMovingAverage': <tf.Variable 'v:0' shape=() dtype=float32_ref>}
INFO:tensorflow:Restoring parameters from ./emamodel/model.ckpt
0.0999999
import tensorflow as tf
from tensorflow. python. framework import graph_util
v1 = tf. Variable( tf. constant( 1.0 , shape = [ 1 ] ) , name = "v1" )
v2 = tf. Variable( tf. constant( 2.0 , shape = [ 1 ] ) , name = "v2" )
result = v1 + v2
init_op = tf. global_variables_initializer( )
with tf. Session( ) as sess:
sess. run( init_op)
graph_def = tf. get_default_graph( ) . as_graph_def( )
output_graph_def = graph_util. convert_variables_to_constants( sess, graph_def, [ 'add' ] )
with tf. gfile. GFile( "combined_model.pb" , "wb" ) as f:
f. write( output_graph_def. SerializeToString( ) )
INFO:tensorflow:Froze 2 variables.
Converted 2 variables to const ops.
from tensorflow. python. platform import gfile
with tf. Session( ) as sess:
model_filename = "combined_model.pb"
with gfile. FastGFile( model_filename, 'rb' ) as f:
graph_def = tf. GraphDef( )
graph_def. ParseFromString( f. read( ) )
result = tf. import_graph_def( graph_def, return_elements= [ 'add:0' ] )
print ( sess. run( result) )
[array([ 3.], dtype=float32)]