在Tensorflow中获取Tensor的原始值
Get original value of Tensor in Tensorflow
我现在正在学习 tensorflow,我养成了打印变量的编程习惯,以便在编码时查看正在使用的内容。我不知道如何在 tensorflow
中打印 constant
和 Variable
对象的值
如何取回分配给实例的值?
import tensorflow as tf
C_int = tf.constant(134)
#Tensor("Const_11:0", shape=TensorShape([]), dtype=int32)
V_mat = tf.Variable(tf.zeros([2,3]))
#<tensorflow.python.ops.variables.Variable object at 0x10672fa10>
C_str = tf.constant("ABC")
#Tensor("Const_16:0", shape=TensorShape([]), dtype=string)
C_int
#134
V_mat
#array([[ 0., 0., 0.],
# [ 0., 0., 0.]])
C_str
#ABC
查看张量值的最简单方法是创建一个 tf.Session
and use Session.run
来计算张量。因此,您的代码如下所示:
import tensorflow as tf
C_int = tf.constant(134)
V_mat = tf.Variable(tf.zeros([2,3]))
C_str = tf.constant("ABC")
sess = tf.Session()
sess.run(C_int)
#134
sess.run(V_mat)
#array([[ 0., 0., 0.],
# [ 0., 0., 0.]])
sess.run(C_str)
#ABC
sess.close()
我现在正在学习 tensorflow,我养成了打印变量的编程习惯,以便在编码时查看正在使用的内容。我不知道如何在 tensorflow
constant
和 Variable
对象的值
如何取回分配给实例的值?
import tensorflow as tf
C_int = tf.constant(134)
#Tensor("Const_11:0", shape=TensorShape([]), dtype=int32)
V_mat = tf.Variable(tf.zeros([2,3]))
#<tensorflow.python.ops.variables.Variable object at 0x10672fa10>
C_str = tf.constant("ABC")
#Tensor("Const_16:0", shape=TensorShape([]), dtype=string)
C_int
#134
V_mat
#array([[ 0., 0., 0.],
# [ 0., 0., 0.]])
C_str
#ABC
查看张量值的最简单方法是创建一个 tf.Session
and use Session.run
来计算张量。因此,您的代码如下所示:
import tensorflow as tf
C_int = tf.constant(134)
V_mat = tf.Variable(tf.zeros([2,3]))
C_str = tf.constant("ABC")
sess = tf.Session()
sess.run(C_int)
#134
sess.run(V_mat)
#array([[ 0., 0., 0.],
# [ 0., 0., 0.]])
sess.run(C_str)
#ABC
sess.close()