Tensorflow保存和恢复变量不一样

时间:2017-09-01 18:31:32

标签: tensorflow

来自Udacity深度学习基础课程。它似乎对他们有用。但它在我的电脑中不起作用。请看一看。感谢你的帮助!

讲座和我的电脑的tensorflow版本都是1.0.0。

import tensorflow as tf

# The file path to save the data
save_file = './model.ckpt'

# Two Tensor Variables: weights and bias
weights = tf.Variable(tf.truncated_normal([2, 3]))
bias = tf.Variable(tf.truncated_normal([3]))

# Class used to save and/or restore Tensor Variables
saver = tf.train.Saver()

with tf.Session() as sess:
    # Initialize all the Variables
    sess.run(tf.global_variables_initializer())

    # Show the values of weights and bias
    print('Weights:')
    print(sess.run(weights))
    print('Bias:')
    print(sess.run(bias))

    # Save the model
    saver.save(sess, save_file)

# Remove the previous weights and bias
tf.reset_default_graph()

# Two Variables: weights and bias
weights = tf.Variable(tf.truncated_normal([2, 3]))
bias = tf.Variable(tf.truncated_normal([3]))

# Class used to save and/or restore Tensor Variables
saver = tf.train.Saver()

with tf.Session() as sess:
    # Load the weights and bias
    saver.restore(sess, save_file)

    # Show the values of weights and bias
    print('Weight:')
    print(sess.run(weights))
    print('Bias:')
    print(sess.run(bias))

2 个答案:

答案 0 :(得分:0)

导入tensorflow后插入tf.reset_default_graph()

答案 1 :(得分:0)

我在1.1.0中运行了你的代码,结果是一样的......