131、TensorFlow保存模型

# tf.train.Saver类提供了保存和恢复模型的方法
# tf.train.Saver的构造函数 提供了save和恢复的参数选项
# Saver对象提供了方法来运行这些计算节点,制定了写和读的路径
# Saver会恢复所有在你模型当中已经定义的变量
# 如果你加载一个模型没有通知如果构建该模型的计算图
# TensorFlow 在二进制检查点文件中保存变量, 粗略地说, 将变量名映射到张量值
# Saving variable
# 创建一个Saver使用tf.train.Saver()来管理所有的变量在模型中
# 列如下面的代码块解释了如何调用tf.train.Saver.save方法来保存变量到checkpoint文件中
import tensorflow as tf
# 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)

保存的结果如下:

2018-02-17 11:21:22.621424: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Model saved in path : tmp/model.ckpt 

 

转载于:https://www.cnblogs.com/weizhen/p/8451508.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值