tensorflow - Change constant in tensoflow session while looping -
how can change tensorflow constant inside session for loop.
learner , wondering how update in loop
import tensorflow tf import numpy np loopercount = 10 data = np.random.randint(2, size=loopercount) x = tf.constant(data, name='x') y = tf.variable((5 * (x * x)) - (3 * x) + 15, name="y") model = tf.initialize_all_variables() tf.session() sess: in range(loopercount): sess.run(model) data = np.random.randint(2, size=loopercount) x = tf.constant(data, name='x') avg = np.average(sess.run(y)) print "avg - {}, sess - {}".format(avg, sess.run(y))
updated working code
import tensorflow tf import numpy np loopercount = 10 x = tf.placeholder("float", loopercount) y = (5 * (x * x)) - (3 * x) + 15 tf.session() sess: in range(loopercount): data = np.random.randint(10, size=loopercount) result_y = sess.run(y, feed_dict={x: data}) avg = np.average(result_y) print "avg - {:10} valy - {:10}".format("{:.2f}".format(avg), result_y)
in tensorflow, "constant" means that: once set it, can't change it. change value tensorflow program uses in loop, have 2 main choices: (1) using tf.placeholder()
feed in value, or (2) using tf.variable
store value between steps, , tf.variable.assign()
update it.
option 1 easier. here's example of how use implement program using placeholder:
import tensorflow tf import numpy np loopercount = 10 data = np.random.randint(2, size=loopercount) x = tf.placeholder(tf.float64, shape=[2], name="x") y = tf.variable((5 * (x * x)) - (3 * x) + 15, name="y") init_op = tf.initialize_all_variables() tf.session() sess: sess.run(init_op) in range(loopercount): data = np.random.randint(2, size=loopercount) avg = np.average(sess.run(y, feed_dict={x: data})) print "avg - {}, sess - {}".format(avg, sess.run(y, feed_dict={x: data}))
Comments
Post a Comment