zoukankan      html  css  js  c++  java
  • 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:	f_jenkinsworkspace
    el-winMwindowsPY35	ensorflowcoreplatformcpu_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 
  • 相关阅读:
    centos搭建window下写的flask程序
    【MTK】iwpriv命令说明
    vs2019专业版离线安装方法
    Python文件编译成exe
    python3升级与安装
    谷歌浏览器https和flash禁用的解决方法
    SQL注入常见函数
    nmap简介与运用
    WPF 万维网对战象棋 客户端+服务端(全套可执行代码在文章末尾)
    《软件工程实践》2020春季学期教学回顾--线上教学,化弊为利
  • 原文地址:https://www.cnblogs.com/weizhen/p/8451508.html
Copyright © 2011-2022 走看看