Mnist
以Mnist为例,在定义好模型的结构以后,模型训练的代码:
init=tf.global_variables_initializer()
saver=tf.train.Saver()
#execute phase
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("tmp/data")
n_epochs=400
batch_size=50
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
for iteration in range(mnist.train.num_examples//batch_size):
x_batch,y_batch=mnist.train.next_batch(batch_size)
sess.run(training_op,feed_dict={x:x_batch,y:y_batch})
acc_train=accuracy.eval(feed_dict={x:x_batch,y:y_batch})
acc_test=accuracy.eval(feed_dict={x:mnist.test.images,y:mnist.test.labels})
print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)
save_path = saver.save( sess, "./my_model_final.ckpt" )
tf.train.write_graph(sess.graph_def, '', 'my_model_final.pb')
生成模型文件
checkpoint
my_model_final.ckpt.data-00000-of-00001
my_model_final.ckpt.index
my_model_final.ckpt.meta
my_model_final.pb
- checkpoint: 记录模型文件的路径信息表
>>cat checkpoint
model_checkpoint_path: "my_model_final.ckpt"
all_model_checkpoint_paths: "my_model_final.ckpt"
其余几个文件,从stackoverflow的解答:
@ajfbiw.s
.meta stores the graph structure,
.data stores the values of each variable in the graph,
.index identifies the checkpiont.
So in the example above: import_meta_graph uses the .meta, and saver.restore uses the .data and .index – T.K. Bartel May 4 '17 at 20:06