TensorFlow2-数学运算(9)

数学运算

运算符

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

Operation type 操作类型

  • element-wise 元素方面
    • ±*/
  • matrix-wise 矩阵式
    • @, matmul
  • dim-wise 维度操作
    • reduce_mean/max/min/sum
import tensorflow as tf
import numpy as np

±*/%

b=tf.fill([2,2],value=2.)
a=tf.ones([2,2])
a+b,a-b,a*b,a/b
(<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[3., 3.],
        [3., 3.]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[-1., -1.],
        [-1., -1.]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[2., 2.],
        [2., 2.]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[0.5, 0.5],
        [0.5, 0.5]], dtype=float32)>)
b//a, b%a
(<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[2., 2.],
        [2., 2.]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[0., 0.],
        [0., 0.]], dtype=float32)>)

tf.math.log tf.e

a=tf.ones([2,2])
a
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>

log ⁡ e 1 = 0 \log_{e}1 = 0 loge1=0

log ⁡ e e 0 = 0 \log_{e}e^0 = 0 logee0=0

e 0 = 1 e^0=1 e0=1

y = log ⁡ e x y = \log_e x y=logex

# 计算a元素的自然对数。
tf.math.log(a)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
       [0., 0.]], dtype=float32)>

y = e x y = e^x y=ex

# 计算a元素的指数
tf.exp(a)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2.7182817, 2.7182817],
       [2.7182817, 2.7182817]], dtype=float32)>

log2, log10?

log ⁡ a b / log ⁡ a c = log ⁡ c b \log_{a}b / \log_{a}c = \log_{c}b logab/logac=logcb

tf.math.log(8.)/tf.math.log(2.)
<tf.Tensor: shape=(), dtype=float32, numpy=3.0>
tf.math.log(100.)/tf.math.log(10.)
<tf.Tensor: shape=(), dtype=float32, numpy=2.0>

pow, sqrt

b=tf.fill([2,2],value=2.)
b
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],
       [2., 2.]], dtype=float32)>
# 平方
tf.pow(b,3)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],
       [8., 8.]], dtype=float32)>
b**3
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],
       [8., 8.]], dtype=float32)>
# 开方
tf.sqrt(b)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.4142135, 1.4142135],
       [1.4142135, 1.4142135]], dtype=float32)>

@ matm

同数据形状

a=tf.ones([2,2])
b=tf.fill([2,2],value=2.)
a,b
(<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[1., 1.],
        [1., 1.]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[2., 2.],
        [2., 2.]], dtype=float32)>)
a@b
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],
       [4., 4.]], dtype=float32)>
tf.matmul(a,b)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],
       [4., 4.]], dtype=float32)>

不同数据形状

a=tf.ones([4,2,3])
b=tf.fill([4,3,5],value=2.)
# [4,2,3]*[4,3,5] -> [2,3]*[3,5] -> [2,5]
# [4,2,5]
a@b
<tf.Tensor: 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: 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=tf.ones([4,2,3])
b=tf.fill([3,5],value=2.)
a.shape, b.shape
(TensorShape([4, 2, 3]), TensorShape([3, 5]))
# [3,5] -> [1,3,5] -> [4,3,5]
bb=tf.broadcast_to(b,[4,3,5])
# [4,2,3]*[4,3,5]
a@bb
<tf.Tensor: 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)>

Recap 扼要重述

  • 矩阵相乘最重要的方法是一般矩阵乘积。它只有在第一个矩阵的列数(column)和第二个矩阵的行数(row)相同时才有意义。一般单指矩阵乘积时,指的便是一般矩阵乘积。一个m×n的矩阵就是m×n个数排成m行n列的一个数阵。
  • y = w * x + b
  • Y = X @ W + b
  • $
    \begin{bmatrix}
    x_{0}^{0}, x_{0}^{1} \
    x_{1}^{0}, x_{1}^{1}
    \end{bmatrix}
    \begin{bmatrix}
    w_{0}^{0}, w_{0}^{1}, w_{0}^{2} \
    w_{1}^{0}, w_{1}^{1}, w_{1}^{2}
    \end{bmatrix} +
    [b_{0},b_{1},b_{2}]
    \rightarrow
    \begin{bmatrix}
    y_{0}^{0}, y_{0}^{1}, y_{0}^{2} \
    y_{1}^{0}, y_{1}^{1}, y_{1}^{2}
    \end{bmatrix}
    $
    • y 0 0 = x 0 0 ∗ w 0 0 + x 0 1 ∗ w 1 0 + b 0 y_{0}^{0}=x_{0}^{0}*w_{0}^{0} + x_{0}^{1}*w_{1}^{0} + b_{0} y00=x00w00+x01w10+b0
    • y 0 1 = x 0 0 ∗ w 0 1 + x 0 1 ∗ w 1 1 + b 0 y_{0}^{1}=x_{0}^{0}*w_{0}^{1} + x_{0}^{1}*w_{1}^{1} + b_{0} y01=x00w01+x01w11+b0
    • y 0 2 = x 0 0 ∗ w 0 2 + x 0 1 ∗ w 1 2 + b 0 y_{0}^{2}=x_{0}^{0}*w_{0}^{2} + x_{0}^{1}*w_{1}^{2} + b_{0} y02=x00w02+x01w12+b0
  • [b,2] -> [b,3]
    • X:[2,2] W:[3,2]
    • [3,2].T -> [2,3]
    • [2,2]@[2,3]
    • [2,3]

Y = X @ W + b

x=tf.ones([4,2])
w=tf.ones([2,1])
b=tf.constant(0.1)
# [4,2]@[2,1] + [1]
# [4,1] + [1] -> [4,1] + [1,1] -> [4,1] + [4,1]
# [4,1]
x@w+b
<tf.Tensor: shape=(4, 1), dtype=float32, numpy=
array([[2.1],
       [2.1],
       [2.1],
       [2.1]], dtype=float32)>

out = relu(X @ W + b)

x=tf.ones([4,2])
w=tf.ones([2,1])
b=tf.constant(0.1)
out = x@w+b
out
<tf.Tensor: shape=(4, 1), dtype=float32, numpy=
array([[2.1],
       [2.1],
       [2.1],
       [2.1]], dtype=float32)>
# 计算校正的线性,只保留正数
out = tf.nn.relu(out)
out
<tf.Tensor: shape=(4, 1), dtype=float32, numpy=
array([[2.1],
       [2.1],
       [2.1],
       [2.1]], dtype=float32)>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值