【问题标题】:Restored TensorFlow model unexpectedly changes weights each time it is restored每次恢复时,恢复的 TensorFlow 模型都会意外更改权重
【发布时间】:2016-09-12 16:45:05
【问题描述】:

我的 TensorFlow 模型每次恢复时都会返回不同的权重,即使我不再训练模型并且检查点模型文件没有改变。这是有问题的,因为我使用相同的硬编码输入得到随机且不可靠的预测。

下面是我的代码的简化版本,它只返回所有权重的总和。每次我运行脚本时总和都会改变,表明权重在改变。

import tensorflow as tf
import numpy as np

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

x = tf.placeholder(tf.float32, shape=[None, 240, 320, 3])
y_ = tf.placeholder(tf.float32, shape=[None, 3])
x_shaped = tf.reshape(x, [-1, 240 * 320 * 3])

W1 = weight_variable([240 * 320 * 3, 32])
b1 = bias_variable([32])
h1 = tf.sigmoid(tf.matmul(x_shaped, W1) + b1)

W2 = weight_variable([32, 3])
b2 = bias_variable([3])
y=tf.nn.softmax(tf.matmul(h1, W2) + b2)

saver = tf.train.Saver()
sess = tf.InteractiveSession(config=tf.ConfigProto())
saver.restore(sess, "/some/path/model.ckpt")
sess.run(tf.initialize_all_variables())
weights = W1.eval(session=sess)
print(np.sum(weights))

【问题讨论】:

    标签: numpy tensorflow


    【解决方案1】:

    你正在运行tf.initialize_all_variables() 之后 saver.restore()。这意味着您从检查点恢复的值将被每个变量的新初始值覆盖。删除行 tf.initialize_all_variables() 应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 2016-05-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多