When tensorflow's session is runned, i need to get the same value of y. How can i get y with same value, not rerun this graph?
import tensorflow as tf
import numpy as np
x = tf.Variable(0.0)
tf.set_random_seed(10)
x_plus1 = x+tf.random_normal([1], mean=0.0, stddev=0.01,dtype=tf.float32)
y = tf.Variable([1.0])
y += x_plus1
z = y + tf.random_normal([1], mean=0.0, stddev=0.01,dtype=tf.float32)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(z.eval())
for i in range(5):
print(y.eval())
Here, i want to get y that contributes to z.
You can evaluate y and z simultaneously with sess.run(), that runs the needed parts of the graph only once, hence the value for y will be the one used for z.
with tf.Session() as sess:
sess.run(init)
z_value, y_value = sess.run([z, y])
print(z_value)
print(y_value)
Modify the with
block as following, this way you only evaluate the graph once, before the for
loop, then you can print it as many times as you like:
with tf.Session() as sess:
sess.run(init)
print(z.eval())
yy = y.eval()
for i in range(5):
print(yy)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.