TensorFlow 学习(二) 张量和基本运算

tensor: 张量

operation:  专门运算的操作节点

graph:  整个程序的结构, 图

TensorBoard:  可视化学习

run() :  运算程序的图、会话

张量的阶:

  •     0 阶: 标量(也叫纯量) 只有大小,没有方向。例子:s = 12
  •     1 阶: 向量 有大小和方向。例子: v = [1,2,3] 
  •     2 阶: 矩阵。 例子:  m = [[1,2],[2,3],[4,6]]
  •     3 阶: 数据立体。 例子:   t = [[[1],[3]], [[3],[4]]]
  •     n 阶: 自己想象。
  1.  查看 TensorFlow 版本
    import tensorflow as tf
    tf.__version__
  2.  加、减、乘、除 四则运算
    import tensorflow as tf
    
    # 创建常数张量
    a = tf.constant(3)
    b = tf.constant(4)
    
    # 加法
    add = tf.add(a, b)
    
    # 减法
    sub = tf.subtract(a, b)
    
    # 乘法
    mul = tf.multiply(a, b)
    
    # 除法
    div = tf.divide(a, b)
    
    # 运行会话
    with tf.Session() as sess:
        print(sess.run([add, sub, mul, div]))
  3.  矩阵运算

    import tensorflow as tf
    
    # 初始化 2 阶张量 矩阵 a
    #  两行三列
    # [[1, 3, 5],
    #  [7, 9, 11]]
    a = tf.constant([1,3,5,7,9,11], shape = [2, 3])
    
    # 初始化 2 阶张量 矩阵 b
    #  两行三列
    # [[ 7,  8],
    #  [ 9, 10],
    #  [11, 12]]
    b = tf.constant([7,8,9,10,11,12], shape = [3, 2])
    
    # 矩阵a * 矩阵b
    # 得到 两行两列 矩阵 c
    # [[ 89  98]
    #  [251 278]]
    c = tf.matmul(a, b)
    
    # 运行会话
    with tf.Session() as sess:
        print(sess.run(c))

猜你喜欢

转载自blog.csdn.net/qq_35200479/article/details/83098319