1、张量中并没有真正保存数据,它保存的是如何得到这些数字的计算过程。
2、一个张量中主要保存了三个属性:名字(name)、维度(shape)和类型(type)。
(1)名字不仅是一个张量的唯一标识符,同样也给出了这个张量是如何计算出来的。张量和计算图上节点所代表的计算结果是对应的,张量的命名可以通过“node:src_output”的形式来给出。其中node为节点的名称,src_output表示当前张量来自节点的第几个输出,例:
with tf.Session( ) as sess:
a = tf.constant([1.0, 2.0], name = "a")
b = tf.constant([2.0, 3.0], name = "b")
result = tf.add(a, b, name = "add")
print(result)
//运行结果:
Tensor("add:0", shape=(2,), dtype=float32)
//"add:0"说明result这个张量是计算节点“add”输出的第一个结果(编号从0开始)
(2)类型 每个张量都会有一个唯一的类型。TensorFlow会对参与运算的所有张量进行类型的检查,当发现类型不匹配时会报错,例:
with tf.Session( ) as sess:
a = tf.constant([1, 2], name = "a")
b = tf.constant([2.0, 3.0], name = "b")
result = tf.add(a, b, name = "add")
print(sess.run(result))
//运算结果:
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("b:0", shape=(2,), dtype=float32)'
若将a改为a = tf.constant([1, 2], name = "a")就不会报错了。