tensorflow教程:变量创建初始化保存和加载

Posted 我花开后百花残

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow教程:变量创建初始化保存和加载相关的知识,希望对你有一定的参考价值。

变量保存到文件

import tensorflow as tf
import numpy as np
# Create two variables.
x_data = np.float32([1,2,3,4,5,6,7,8,9,0])
weights = tf.Variable(tf.random_normal([10, 1], stddev=0.35), name="weights")
biases = tf.Variable(tf.zeros([1]), name="biases")
y = tf.matmul(x_data.reshape((1,-1)), weights)+biases
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
# Later, when launching the model
with tf.Session() as sess:
    # Run the init operation.
    sess.run(init_op)
    y_ = sess.run(y)
    print(y_)
    save_path = saver.save(sess, "./tmp/model.ckpt")
    print("Model saved in file: ", save_path)

从文件加载变量

import tensorflow as tf
import numpy as np
# Create two variables.
x_data = np.float32([1,2,3,4,5,6,7,8,9,0])
weights = tf.Variable(tf.random_normal([10, 1], stddev=0.35), name="weights")
biases = tf.Variable(tf.zeros([1]), name="biases")
y = tf.matmul(x_data.reshape((1,-1)), weights)+biases
saver = tf.train.Saver()
# Later, when launching the model
with tf.Session() as sess:
    saver.restore(sess, ./tmp/model.ckpt)
    y_ = sess.run(y)
    print(y_)

参考链接

http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variables.html

以上是关于tensorflow教程:变量创建初始化保存和加载的主要内容,如果未能解决你的问题,请参考以下文章

Notetensorflow freeze model

在 Tensorflow 中重新初始化变量

TF Boys (TensorFlow Boys ) 养成记

tensorflow中的Supervisor

Tensorflow学习教程------变量

Tensorflow加载预训练模型和保存模型