Tensorflow:在变量初始化后获得预期的全局变量列表的两倍

时间:2017-07-20 14:24:04

标签: python tensorflow

我正在运行神经网络如下:

xt = tf.placeholder(tf.float32)
x_t = tf.placeholder(tf.float32)
tht = tf.placeholder(tf.float32)
th_t = tf.placeholder(tf.float32)


rlu_1t = tf.contrib.layers.fully_connected(inputs=tf.reshape([xt,x_t,tht,th_t],[1,4]),num_outputs=10)

# 4 state features: x, x_, th, th_
rlu_1t.weights_initializer = tf.random_uniform(shape=[4],minval=-1,maxval=1) # is this 4 or 10?
rlu_1t.biases_initializer = tf.random_uniform(shape=[1],minval=-1,maxval=1)
rlu_2t = tf.contrib.layers.fully_connected(inputs=rlu_1t,num_outputs=10) # hope that makes a copy
rlu_2t.weights_initializer = tf.random_uniform(shape=[10],minval=-1,maxval=1)
rlu_2t.biases_initializer = tf.random_uniform(shape=[1],minval=-1,maxval=1)
Qvalst = tf.contrib.layers.fully_connected(inputs=rlu_2t,num_outputs=2)
Qvalst.weights_initializer = tf.random_uniform(shape=[10],minval=-1,maxval=1)
Qvalst.biases_initializer = tf.random_uniform(shape=[1],minval=-1,maxval=1)
Qvalst.activation_fn = tf.identity

当我没有初始化神经网络中的权重和偏差时,则调用print(tf.global_variables())输出:

[<tf.Variable 'fully_connected/weights:0' shape=(4, 10) dtype=float32_ref>, 
<tf.Variable 'fully_connected/biases:0' shape=(10,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_1/weights:0' shape=(10, 10) dtype=float32_ref>, 
<tf.Variable 'fully_connected_1/biases:0' shape=(10,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_2/weights:0' shape=(10, 2) dtype=float32_ref>, 
<tf.Variable 'fully_connected_2/biases:0' shape=(2,) dtype=float32_ref>]

当我在神经网络中初始化权重和偏差时,如下所示:

with tf.Session() as sess:

    init = tf.global_variables_initializer()
    sess.run(init)

然后调用print(tf.global_variables())返回之前变量列表的两倍(我不需要):

[<tf.Variable 'fully_connected/weights:0' shape=(4, 10) dtype=float32_ref>, 
<tf.Variable 'fully_connected/biases:0' shape=(10,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_1/weights:0' shape=(10, 10) dtype=float32_ref>, 
<tf.Variable 'fully_connected_1/biases:0' shape=(10,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_2/weights:0' shape=(10, 2) dtype=float32_ref>, 
<tf.Variable 'fully_connected_2/biases:0' shape=(2,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_3/weights:0' shape=(4, 10) dtype=float32_ref>, 
<tf.Variable 'fully_connected_3/biases:0' shape=(10,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_4/weights:0' shape=(10, 10) dtype=float32_ref>, 
<tf.Variable 'fully_connected_4/biases:0' shape=(10,) dtype=float32_ref>, 
<tf.Variable 'fully_connected_5/weights:0' shape=(10, 2) dtype=float32_ref>, 
<tf.Variable 'fully_connected_5/biases:0' shape=(2,) dtype=float32_ref>]

为什么会这样?

1 个答案:

答案 0 :(得分:0)

修正了 - 结果我需要在神经网络之前添加tf.reset_default_graph()以清除变量列表。