注:本文主要通过莫烦的python学习视频记录的内容,如果喜欢请支持莫烦python。谢谢
目前tf的模型保存其实只是参数保存,所以保存文件时你特别要主要以下几点:
1、一定要设定好参数的数据类型!
2、设定参数的名称,并且一一对应!
3、读取参数时,需要设定好模型图!
下面做一个简单的demo,供各位参考:
保存模型:
import tensorflow as tf
import numpy as np
## Save to file
# remember to define the same dtype and shape when restore
W = tf.Variable([[2,2,3],[3,4,5]], dtype=tf.float32, name='weights')
b = tf.Variable([[2,2,3]], dtype=tf.float32, name='biases')
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
save_path = saver.sa