2017-07-28 28 views
0

我正在使用Tensorflow的scatter_update在簡單測試中設置數組的值。我期望以下代碼將數組中的所有值設置爲1.0。Tensorflow增量分配與scatter_update不一致結果

pt = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 
numOne = tf.constant(1) 
xi = tf.Variable(0) 
out = tf.Variable(pt) 
tf.global_variables_initializer().run() 

xi_ = xi + numOne 
out_ = tf.scatter_update(out, [xi], [tf.cast(numOne, tf.float32)]) 

step = tf.group(
    xi.assign(xi_), 
    out.assign(out_) 
    ) 
for i in range(15): step.run() 
print(out.eval()) 

相反,我得到這樣的結果不一致:

[ 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1.] 

是否有某種類型的鎖,我很想念機制呢?

+0

除了16,並且你期望填充15個值,我認爲隨着操作執行的隨機順序發生了一些事情。 – Uvar

回答

0

tf.group內部的操作以隨機順序執行。如果你想要特定的順序,你可以使用tf.control_dependencies或分裂成兩個.run通話,即

step1 = xi.assign(xi_) 
step2 = out.assign(out_) 
for i in range(15): 
    print(out.eval()) 
    sess.run(step1) 
    sess.run(step2) 
0

此外,使用control_dependencies它會是這樣的:

xi_ = xi + numOne 
with tf.control_dependencies[xi_]: 
    out_ = tf.scatter_update(out, [xi], [tf.cast(numOne, tf.float32)]) 
    with tf.control_dependencies[out_]: 
     step = tf.group(
      xi.assign(xi_), 
      out.assign(out_) 
     ) 
     for i in range(15): step.run() 
print(out.eval()) 
的事實,您的PT是大小