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))

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值