Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tf Variable does not change value? [duplicate]

Tags:

tensorflow

i just want to change the value of a tf.Variable with .assign(), but it always prints "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)

Edit: this works

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)
like image 243
flobotics robotics Avatar asked Feb 09 '26 23:02

flobotics robotics


1 Answers

assign only creates the operation to change the variable, you have to run it:

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)
like image 118
Vincent Renkens Avatar answered Feb 12 '26 14:02

Vincent Renkens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!