TensorFlow:并非所有变量都被恢复 - Python

时间:2017-03-02 23:07:29

标签: python tensorflow

我有另一个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'。为什么不' out'?我有什么不对的吗?请你花几分钟时间来帮助我?

非常感谢

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

1 个答案:

答案 0 :(得分:0)

It looks like you are over-writing one of the 'out' keys with this dictionary constructor:

{**weights, **biases}

For example:

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}