Numpy 恢复的TensorFlow模型每次恢复时都会意外更改权重

Numpy 恢复的TensorFlow模型每次恢复时都会意外更改权重,numpy,tensorflow,Numpy,Tensorflow,我的TensorFlow模型每次恢复时都会返回不同的权重,即使我不再训练该模型,并且选中的模型文件也不会更改。这是有问题的,因为我用相同的硬编码输入得到随机和不可靠的预测 下面是我的代码的简化版本,它只返回所有权重的总和。每次运行脚本时,总和都会更改,这表明权重正在更改 import tensorflow as tf import numpy as np def weight_variable(shape): initial = tf.truncated_normal(shape, s

我的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))

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

您正在运行
tf.initialize_all_variables()
之后tf.initialize\u all\u variables()应该可以解决问题