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 
  • 相关阅读:
    HTML实体符号代码速查表
    在vue中使用css预编辑器
    多个SVG图形集成到一个SVG图形上
    CSS3那些不为人知的高级属性
    如何搭建一个vue项目(完整步骤)
    Vue.js——vue-resource全攻略
    this.$router.push、replace、go的区别
    Vue界面中关于APP端回调方法问题
    Vue、webpack中默认的config.js、index.js 配置详情
    vue mint ui 手册文档
  • 原文地址:https://www.cnblogs.com/weizhen/p/8451508.html
Copyright © 2011-2022 走看看