Tensorflow - running total -
how can add number 5 after every iteration of loop?
i want this:
weight = 0.225 in range(10): weight += 5 print (weight)
here how trying in tensorflow never updates weight
import tensorflow tf def dummy(x): weights['h0'] = tf.add(weights['h0'], 5) res = tf.add(weights['h0'], x) return res # build computational graph = tf.placeholder('float', none) d = dummy(a) weights = { 'h0': tf.variable(tf.random_normal([1])) } # initialize variables init = tf.global_variables_initializer() # create session , run graph tf.session() sess: sess.run(init) in range(10): print (sess.run(d, feed_dict={a: [2]})) # close session sess.close()
there's operation explicitly created adding value , assigning result input node: tf.assign_add
you should use instead of tf.assing
+ tf.add
.
also, it's more important understand why previous code won't work.
weights['h0'] = tf.add(weights['h0'], 5) res = tf.add(weights['h0'], x)
at fist line, you're defining node add, inputs weights['h0']
, 5
and you're assigning node python variable weights['h0']
.
now, thus, weights['h0']
python variable holding tensorflow node.
in next line, you're defining add node, between previous node , x
, , return node.
when graph evaluated, evaluate node pointed res
, force evaluation of previous node (because res function of node holded weights['h0']
).
the problem assignment @ line 1
python assignment , not tensorflow assignment. assign operation executed in python environment has no defined assign node tensorflow graph.
p.s: when use with
you're defining context manager handles closing operations you. can remove sess.close()
because executed automatically when exit context
Comments
Post a Comment