tf变量不会改变价值?

时间:2016-08-05 13:08:53

标签: tensorflow

我只想用.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)

1 个答案:

答案 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)