·· 维 数 ···· 阶 ········· 名 字 ········· 例 子 ············
·· 0-D ······ 0 ····· 标量 scalar ···· s=1 2 3
·· 1-D ······ 0 ····· 向量 vector ···· s=[1,2,3]
·· 2-D ······ 0 ····· 矩阵 matrix ···· s=[ [1,2,3], [4,5,6],[7,8,9] ]
·· n-D ······ 0 ····· 标量 tensor ···· s=[[[[[....n个html
# 两个张量的加法 import tensorflow as tf a = tf.constant([1.0, 2.0]) b = tf.constant([3.0, 4.0]) result = a+b print(result)
该计算图表示:y = X1W1 + X2W2
(不能理解就记住,该计算图表示上面的这种含义)git
# 两个张量的加法 import tensorflow as tf # x 是一个一行两列的张量 x = tf.constant([[1.0, 2.0]]) # x 是一个两行一列的张量 w = tf.constant([[3.0], [4.0]]) ''' 构建计算图,但不运算 y = XW = x1*w1 + x2*w2 ''' # 矩阵相乘 y = tf.matmul(x, w) print(y)
Tensor("MatMul:0", shape=(1, 1), dtype=float32)github
咱们用 with 结构实现,语法以下:数组
with tf.Session() as sess :
print(sess.run(y))网络
意思是:将 tf.Session 记为 sess,调用 tf.Session 下的 run 方法执行 y,y 也就是上面的计算图,也就是那个表达式优化
# 两个张量的加法 import tensorflow as tf # x 是一个一行两列的张量 x = tf.constant([[1.0, 2.0]]) # x 是一个两行一列的张量 w = tf.constant([[3.0], [4.0]]) ''' 构建计算图,但不运算 y = XW = x1*w1 + x2*w2 ''' # 矩阵相乘 y = tf.matmul(x, w) print(y) # 会话:执行节点运算 with tf.Session() as sess: print(sess.run(y))