tensorflow中变量的保存和加载
来源:互联网 发布:听歌软件排行 编辑:程序博客网 时间:2024/05/16 06:55
TensorFlow中我们可以保存全部变量也可以保存部分变量
下面是保存全部变量的代码(保存变量的时候要全部初始化)
import tensorflow as tf# Create some variables.v1 = tf.Variable('v1', name="v1")v2 = tf.Variable('v2', name="v2")# Add an op to initialize the variables.init_op = tf.initialize_all_variables()# Add ops to save and restore all the variables.saver = tf.train.Saver()# Later, launch the model, initialize the variables, do some work, save the# variables to disk.with tf.Session() as sess: sess.run(init_op) # Save the variables to disk. save_path = saver.save(sess, "/tmp/model.ckpt") print("Model saved in file: ", save_path)
加载保存的全部变量
import tensorflow as tf# Create some variables.v1 = tf.Variable("v2", name="v1")v2 = tf.Variable("v1", name="v2")# Add ops to save and restore all the variables.saver = tf.train.Saver()# init=tf.global_variables_initializer()# Later, launch the model, use the saver to restore variables from disk, and# do some work with the model.with tf.Session() as sess: # sess.run(init) # Restore variables from disk. saver.restore(sess, "/tmp/model.ckpt") print("Model restored.") print(sess.run(v2)) # Do some work with the model
实验结果:
C:\python35\python.exe C:/Users/User/PycharmProjects/nlpdemo/tensorflowlearn/use_save_variables.py2017-10-24 13:11:20.610000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.2017-10-24 13:11:20.610000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.Model restored.b'v2'Process finished with exit code 0
保存一部分代码:
import tensorflow as tf# Create some variables.v1 = tf.Variable("v2", name="v1")v2 = tf.Variable("v2", name="v2")# Add ops to save and restore only 'v2' using the name "my_v2"init_op = tf.initialize_all_variables()saver = tf.train.Saver({"my_v2": v2})# Use the saver object normally after that.with tf.Session() as sess: # Restore variables from disk. sess.run(init_op) # Save the variables to disk. save_path = saver.save(sess, "/tmp/model_one.ckpt") print("Model saved in file: ", save_path)
加载一部分变量(注意加载变量之前先保存变量,记住key的对应)
import tensorflow as tf# Create some variables.v1 = tf.Variable("v2", name="v1")my_v2 = tf.Variable("v1", name="my_v2")# Add ops to save and restore all the variables.saver = tf.train.Saver({"my_v2": my_v2})# init=tf.global_variables_initializer()# Later, launch the model, use the saver to restore variables from disk, and# do some work with the model.with tf.Session() as sess: # sess.run(init) # Restore variables from disk. saver.restore(sess, "/tmp/model_one.ckpt") print("Model restored.") print(sess.run(my_v2)) # Do some work with the model
实验结果:
C:\python35\python.exe C:/Users/User/PycharmProjects/nlpdemo/tensorflowlearn/use_some_save_variables.py2017-10-24 13:15:06.539000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.2017-10-24 13:15:06.539000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.Model restored.b'v2'Process finished with exit code 0
阅读全文
0 0
- tensorflow中变量的保存和加载
- TensorFlow入门(三)--变量:创建、初始化、保存和加载
- tensorflow variable的保存和加载
- tensorflow保存 和 加载模型
- Tensorflow 保存和加载模型
- tensorflow保存和加载模型
- tensorflow中模型的保存和恢复
- Tensorflow语法学习笔记(一):变量:创建、初始化、保存和加载
- Tensorflow中变量保存与恢复
- Tensorflow学习: 保存变量和网络
- tensorflow 保存和加载模型 -2
- tensorflow模型参数保存和加载问题
- TensorFlow保存和加载训练模型
- tensorflow-模型保存和加载(二)
- TensorFlow保存和加载训练模型
- 使用tensorflow保存、加载和使用模型
- TensorFlow保存和加载训练模型
- tensorflow基础(1)变量的创建、初始化、保存与加载
- 14.(2)Struts2_值栈
- 数组、对象数组转化成JSON对象的情况
- 解决锁死数据库的sql
- angularjs添加排序查询
- android 屏幕高度、状态栏高度、titlebar高度详解
- tensorflow中变量的保存和加载
- 113. Path Sum II
- 超链接向后台传值含有日期
- 表格 -隔行换色
- bzoj2330: [SCOI2011]糖果
- SparkCore 知识点 (一)
- shell 随记
- RESTful API
- 图片二次采样