获取 TensorFlow 变量或 Tensor 的值
有时我们需要获取并打印 TensorFlow 变量的值以保证我们的程序是正确的。
例如,如果我们有以下程序:
import tensorflow as tf
import numpy as np
a = tf.Variable(tf.random_normal([2,3])) # declare a tensorflow variable
b = tf.random_normal([2,2]) #declare a tensorflow tensor
init = tf.initialize_all_variables()
如果我们想获得 a 或 b 的值,可以使用以下过程:
with tf.Session() as sess:
sess.run(init)
a_value = sess.run(a)
b_value = sess.run(b)
print a_value
print b_value
要么
with tf.Session() as sess:
sess.run(init)
a_value = a.eval()
b_value = b.eval()
print a_value
print b_value