1
所以我嘗試以下Tensorflow相當於x的= X + 1
x = tf.Variable(0.10, tf.float32)
tf.assign(x, tf.add(x,1))
和
x = tf.Variable(0.10, tf.float32)
x = x + 1
但他們沒有工作。任何想法我們如何在TensorFlow中做這樣的功能?爲了完整
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable(0.10, tf.float32)
y = tf.constant(1.00, tf.float32)
x.assign(1.0)
sess.run(tf.initialize_all_variables())
x = sess.run(x)
print(x)
整個代碼更新:解決方案 的完整性整個代碼。這只是確保你有x = x.assign(1.0)
而不是簡單的x.assign(1.0)
。
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable(0.10, tf.float32)
y = tf.constant(1.00, tf.float32)
x = x.assign(1.0)
sess.run(tf.initialize_all_variables())
x = sess.run(x)
print(x)
我試過服用,它返回0.1仍然(原值)。任何想法?我編輯了整個代碼的文章(簡單),告訴你它是如何工作的。 – Ritchie
似乎需要運行分配操作。你有沒有檢查一下[this](http://stackoverflow.com/questions/39671956/how-to-increment-a-variable-in-tensorflow)。 – 108
謝謝。是的,我玩這個想法,我們必須在該鏈接中使用相同的'var'名稱而不是新的。用更改更新了代碼。 – Ritchie