Tensorflow里面,基本的数据单元是张量,即多维数组。数据与数据之间用有向边连接,构成了计算图。
来看如下代码:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a=tf.constant([1.0,2.0])
b=tf.constant([3.0,4.0])
c=a+b
print(c)
输出结果如下
Tensor("add:0", shape=(2,), dtype=float32)
这个输出结果记录了节点c在计算图中的信息。add:0是节点名,其中0代表第0个输出,2代表张量的位数,最后一项是数据类型。
进一步,考虑构造一个线性的神经元并计算结果:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
x=tf.constant([[1.0,2.0]])
w=tf.constant([[3.0],[4.0]])
y=tf.matmul(x,w)
print(y)
with tf.Session() as sess:
print(sess.run(y))
输出结果如下
Tensor("MatMul:0", shape=(1, 1), dtype=float32)
[[11.]]