2017-09-01 118 views
1

它来自Udacity深度学习基础课程。它似乎为他们工作。但它在我的电脑中不起作用。请看一看。感谢您的帮助!Tensorflow保存和恢复变量不一样

讲座和我的电脑的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)) 
+0

你的代码适用于我的最新TensorFlow。你能否更新到最新的TensorFlow版本并重试?如果它仍然不起作用,会出现什么问题?你有错误还是错误的输出? –

回答

0

输入张量流后插入tf.reset_default_graph()

0

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