zoukankan      html  css  js  c++  java
  • 数学运算

    Outline

    • +-*/
    • **,pow,square
    • sqrt
    • //,%
    • exp,log
    • @,matmul
    • linear layer

    Operation type

    • element-wise
      • +-*/
    • matrix-wise
      • @,matmul
      • [b,3,4]@[b,4,5] = b([3,4][4,5])=[b,3,5]
    • dim-wise
      • reduce_mean/max/min/sum

    +-*/%//

    import tensorflow as tf
    
    b = tf.fill([2, 2], 2.)
    a = tf.ones([2, 2])
    
    a+b
    
    <tf.Tensor: id=6, shape=(2, 2), dtype=float32, numpy=
    array([[3., 3.],
           [3., 3.]], dtype=float32)>
    
    a-b
    
    <tf.Tensor: id=8, shape=(2, 2), dtype=float32, numpy=
    array([[-1., -1.],
           [-1., -1.]], dtype=float32)>
    
    a*b
    
    <tf.Tensor: id=10, shape=(2, 2), dtype=float32, numpy=
    array([[2., 2.],
           [2., 2.]], dtype=float32)>
    
    a/b
    
    <tf.Tensor: id=12, shape=(2, 2), dtype=float32, numpy=
    array([[0.5, 0.5],
           [0.5, 0.5]], dtype=float32)>
    
    b // a
    
    <tf.Tensor: id=14, shape=(2, 2), dtype=float32, numpy=
    array([[2., 2.],
           [2., 2.]], dtype=float32)>
    
    b % a
    
    <tf.Tensor: id=16, shape=(2, 2), dtype=float32, numpy=
    array([[0., 0.],
           [0., 0.]], dtype=float32)>
    

    tf.math.log, tf.exp

    a
    
    <tf.Tensor: id=5, shape=(2, 2), dtype=float32, numpy=
    array([[1., 1.],
           [1., 1.]], dtype=float32)>
    
    tf.math.log(a)  # 只有以e为底的log
    
    <tf.Tensor: id=23, shape=(2, 2), dtype=float32, numpy=
    array([[0., 0.],
           [0., 0.]], dtype=float32)>
    
    tf.exp(a)
    
    <tf.Tensor: id=21, shape=(2, 2), dtype=float32, numpy=
    array([[2.7182817, 2.7182817],
           [2.7182817, 2.7182817]], dtype=float32)>
    
    tf.math.log(8.)/tf.math.log(2.)  # 以2为底
    
    <tf.Tensor: id=43, shape=(), dtype=float32, numpy=3.0>
    
    tf.math.log(100.)/tf.math.log(10.)  # 以10为底
    
    <tf.Tensor: id=49, shape=(), dtype=float32, numpy=2.0>
    

    pow, sqrt

    b
    
    <tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy=
    array([[2., 2.],
           [2., 2.]], dtype=float32)>
    
    tf.pow(b, 3)
    
    <tf.Tensor: id=53, shape=(2, 2), dtype=float32, numpy=
    array([[8., 8.],
           [8., 8.]], dtype=float32)>
    
    b**3
    
    <tf.Tensor: id=56, shape=(2, 2), dtype=float32, numpy=
    array([[8., 8.],
           [8., 8.]], dtype=float32)>
    
    tf.sqrt(b)
    
    <tf.Tensor: id=58, shape=(2, 2), dtype=float32, numpy=
    array([[1.4142135, 1.4142135],
           [1.4142135, 1.4142135]], dtype=float32)>
    

    @, matmul

    a, b
    
    (<tf.Tensor: id=5, shape=(2, 2), dtype=float32, numpy=
     array([[1., 1.],
            [1., 1.]], dtype=float32)>,
     <tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy=
     array([[2., 2.],
            [2., 2.]], dtype=float32)>)
    
    a@b
    
    <tf.Tensor: id=62, shape=(2, 2), dtype=float32, numpy=
    array([[4., 4.],
           [4., 4.]], dtype=float32)>
    
    tf.matmul(a, b)
    
    <tf.Tensor: id=64, shape=(2, 2), dtype=float32, numpy=
    array([[4., 4.],
           [4., 4.]], dtype=float32)>
    
    a = tf.ones([4, 2, 3])  # 4作为batch处理
    b = tf.fill([4, 3, 5], 2.)  # 4作为batch处理
    
    a@b
    
    <tf.Tensor: id=72, shape=(4, 2, 5), dtype=float32, numpy=
    array([[[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]]], dtype=float32)>
    
    tf.matmul(a, b)
    
    <tf.Tensor: id=74, shape=(4, 2, 5), dtype=float32, numpy=
    array([[[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]]], dtype=float32)>
    

    With broadcasting

    a.shape
    
    TensorShape([4, 2, 3])
    
    b.shape
    
    TensorShape([4, 3, 5])
    
    bb = tf.broadcast_to(b, [4, 3, 5])
    
    a@bb
    
    <tf.Tensor: id=78, shape=(4, 2, 5), dtype=float32, numpy=
    array([[[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]],
    
           [[6., 6., 6., 6., 6.],
            [6., 6., 6., 6., 6.]]], dtype=float32)>
    

    Y = X@W +b

    x = tf.ones([4, 2])
    W = tf.ones([2, 1])
    b = tf.constant(0.1)  # 自动broadcast为[4,1]
    
    x@W + b
    
    <tf.Tensor: id=88, shape=(4, 1), dtype=float32, numpy=
    array([[2.1],
           [2.1],
           [2.1],
           [2.1]], dtype=float32)>
    
    out = x@W + b
    tf.nn.relu(out)
    
    <tf.Tensor: id=95, shape=(4, 1), dtype=float32, numpy=
    array([[2.1],
           [2.1],
           [2.1],
           [2.1]], dtype=float32)>
  • 相关阅读:
    leetcode-242-Valid Anagram
    leetcode-232-Implement Queue using Stacks
    机器学习(3)——梯度下降法
    Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
    Codeforces Round #304 (Div. 2)(CF546D) Soldier and Number Game(线性筛)
    Codeforces Round #304 (Div. 2)(CF546E) Soldier and Traveling(最大流)
    机器学习(2)——线性回归法
    Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
    机器学习(1)——K近邻算法
    简易解说拉格朗日对偶(Lagrange duality)(转载)
  • 原文地址:https://www.cnblogs.com/nickchen121/p/10849481.html
Copyright © 2011-2022 走看看