tensrflow基础学习

tensorflow基础学习

保存与恢复模型及变量

保存

#Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)
inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1)
 Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
#Add ops to save and restore all the variables.
saver = tf.train.Saver()
 #Later, launch the model, initialize the variables, do some work, and save the
 variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
 #Do some work with the model.
  inc_v1.op.run()
  dec_v2.op.run()
  #Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print("Model saved in path: %s" % save_path)

恢复

tf.reset_default_graph()
#Create some variables.
v1 = tf.get_variable("v1", shape=[3])
v2 = tf.get_variable("v2", shape=[5])

#Add ops to save and restore all the variables.
saver = tf.train.Saver()

#Later, launch the model, use the saver to restore variables from disk, and
#do some work with the model.
with tf.Session() as sess:
  #Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")
  print("Model restored.")
  #Check the values of the variables
  print("v1 : %s" % v1.eval())
  print("v2 : %s" % v2.eval())

检查某个检查点中的变量:

将模型中的tensor打印出来

from tensorflow.python.tools import inspect_checkpoint as chkp
chkp.print_tensors_in_checkpoint_file("/tmp/model.ckpt", tensor_name='', all_tensors=True)
chkp.print_tensors_in_checkpoint_file("/tmp/model.ckpt", tensor_name='v1', all_tensors=False)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值