【问题标题】:TensorFlow: Not all of my variables are being restored - PythonTensorFlow:并非我的所有变量都在恢复 - Python
【发布时间】:2017-07-22 21:41:38
【问题描述】:

我有另一个 TensorFlow 查询:

我正在训练一个回归模型,保存权重和偏差,然后恢复它们以在不同的数据集上重新运行模型。至少,这就是我想要做的。并不是我所有的体重都在恢复。这是用于保存我的变量的代码:

# Add ops to save and restore all the variables.
saver = tf.train.Saver({**weights, **biases})

# Save the variables to disk.
save_path = saver.save(sess, "Saved_Vars.ckpt")

这是我恢复和运行模型的全部代码:

# Network Parameters
n_hidden_1 = 9
n_hidden_2 = 56
n_hidden_3 = 8
n_input = 9
n_classes = 1

# TensorFlow Graph Input
x = tf.placeholder("float", [None, n_input])

# Create Multilayer Model
def multilayer_perceptron(x, weights, biases):
    # First hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)

    # Second hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)

    # Second hidden layer with RELU activation
    layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
    layer_3 = tf.nn.relu(layer_3)

    # Last output layer with linear activation
    out_layer = tf.matmul(layer_3, weights['out']) + biases['out']
    return out_layer

# weights and biases
weights = {
        'h1': tf.Variable(tf.zeros([n_input, n_hidden_1])),
        'h2': tf.Variable(tf.zeros([n_hidden_1, n_hidden_2])),
        'h3': tf.Variable(tf.zeros([n_hidden_2, n_hidden_3])),
        'out': tf.Variable(tf.zeros([n_hidden_3, n_classes]))
}

biases = {
        'b1' : tf.Variable(tf.zeros([n_hidden_1])),
        'b2': tf.Variable(tf.zeros([n_hidden_2])),
        'b3': tf.Variable(tf.zeros([n_hidden_3])),
        'out': tf.Variable(tf.zeros([n_classes]))
}

# Construct Model
pred = multilayer_perceptron(x, weights, biases)
pred = tf.transpose(pred)

# Initialize variables
init = tf.global_variables_initializer()

# RUNNING THE SESSION

# launch the session
sess = tf.InteractiveSession()


# Initialize all the variables
sess.run(init)

# Add ops to save and restore all the variables.
saver = tf.train.Saver({**weights, **biases})

# Restore variables from disk.
saver.restore(sess, "Saved_Vars.ckpt")

# Use the restored model to predict the target values
prediction = sess.run(pred, feed_dict={x:dataVar_scaled}) #pred.eval(feed_dict={x:X})

现在,这就是我感到困惑/沮丧/烦恼的地方。从权重我可以恢复“h1”、“h2”和“h3”,但不能恢复“out”。为什么不“出去”?有什么我做错了吗?请您花几分钟时间来帮助我吗?

非常感谢

我在 Windows 10 上直接运行 Python 3.5 和 TensorFlow 0.12,并且我正在使用 Spyder IDE。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    看起来您正在使用此字典构造函数覆盖“out”键之一:

    {**weights, **biases}
    

    例如:

    weights = {'h1':1, 'h2':2, 'out':3}
    biases = {'b1':4, 'b2':5, 'out':6}
    print({**weights, **biases})
    
    {'h2': 2, 'out': 6, 'b2': 5, 'b1': 4, 'h1': 1}
    

    【讨论】:

    • 非常感谢,更改变量名很管用! :) 请你能解释一下{**weights, **biases} 正在做什么,因为我显然没有很好的把握吗?再次感谢
    • 这个SO thread 不错。此外,Saver 真的想要一个列表,而不是字典。我通常不会将任何值传递给 Saver,然后它默认保存任何可保存的内容。
    猜你喜欢
    • 2021-12-11
    • 2023-03-22
    • 1970-01-01
    • 2011-01-11
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    相关资源
    最近更新 更多