tensorflow中tensor的理解

import tensorflow as tf
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)
张量表示的意思:Tensor(name名字,shape维度,type类型)
其中,名字的表示形式为:node:src_output。node为节点的名称,src_output表示当前张量来自节点的第几个输出。add:0就表示,result这个张量是计算节点“add”输出的第一个结果(编号从0开始)。
shape=(2,)表示张量result是一个一维数组,数组长度为2.
dtype=float32表示类型。类型不匹配时会报错。

如果想要得到具体的结果,必须要使用Session

with tf.Session() as sess:
    print(sess.run(result))

结果:[ 3. 5.]

猜你喜欢

转载自blog.csdn.net/qq_22522663/article/details/70209060