我只想用.assign()更改tf.Variable的值,但它总是打印“0”?
import tensorflow as tf
session = tf.Session()
var = tf.Variable(0, "myVar")
session.run(tf.initialize_all_variables())
var.assign(var + 1)
print session.run(var)
var.assign(var + 1)
print session.run(var)
编辑:这有效
import tensorflow as tf
session = tf.Session()
var = tf.Variable(0, "myVar")
session.run(tf.initialize_all_variables())
add = var.assign(var + 1)
print session.run(add)
print session.run(add)
答案 0 :(得分:2)
assign仅创建更改变量的操作,您必须运行它:
import tensorflow as tf
session = tf.Session()
var = tf.Variable(0, "myVar")
session.run(tf.initialize_all_variables())
session.run(var.assign(var + 1))
print session.run(var)
session.run(var.assign(var + 1))
print session.run(var)