Loading

4-2张量的数学运算——eat_tensorflow2_in_30_days

4-2张量的数学运算

张量的操作主要包括张量的结构操作和张量的数学运算

张量的结构操作诸如:张量创建,索引切片,维度变换,合并分割

张量的数学运算主要有:标量运算,向量运算,矩阵运算。另外这里会介绍张量运算的广播机制

标量运算

  • 张量的数学运算符可以分为标量运算符、向量运算符、以及矩阵运算符。
    • 加减乘除乘方,以及三角函数,指数,对数等常见函数,逻辑比较运算符等都是标量运算符
    • 标量运算符的特点是对张量实施逐元素运算
    • 有些标量运算符对常用的数学运算符进行了重载。并且支持类似numpy的广播特性
    • 许多标量运算符都在 tf.math模块下
import tensorflow as tf 
import numpy as np 
a = tf.constant([[1.0, 2], [-3, 4.0]])
b = tf.constant([[5.0, 6], [7.0, 8.0]])
a + b  # 运算符重载

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 6.,  8.],
       [ 4., 12.]], dtype=float32)>
"""
a - b

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ -4.,  -4.],
       [-10.,  -4.]], dtype=float32)>
"""
a * b

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[  5.,  12.],
       [-21.,  32.]], dtype=float32)>
"""
a / b

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 0.2       ,  0.33333334],
       [-0.42857143,  0.5       ]], dtype=float32)>
"""
a ** 2

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 1.,  4.],
       [ 9., 16.]], dtype=float32)>
"""
a ** 0.5

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.       , 1.4142135],
       [      nan, 2.       ]], dtype=float32)>
"""
a % 3  # mod的运算符重载,等价于m = tf.math.mod(a,3)

"""
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 1.,  2.],
       [-0.,  1.]], dtype=float32)>
"""
a // 3

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

"""
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[False,  True],
       [False,  True]])>
"""
(a >= 2) & (a <= 3)

"""
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[False,  True],
       [False, False]])>
"""
(a >= 2) | (a <= 3)

"""
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[ True,  True],
       [ True,  True]])>
"""
a == 5

"""
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[False, False],
       [False, False]])>
"""
tf.equal(a, 5)

"""
<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
array([[False, False],
       [False, False]])>
"""
tf.sqrt(a)

"""
tf.sqrt(a)

<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.       , 1.4142135],
       [      nan, 2.       ]], dtype=float32)>
"""
a = tf.constant([1.0, 8.0])
b = tf.constant([5.0, 6.0])
c = tf.constant([6.0, 7.0])
tf.add_n([a, b, c])

"""
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([12., 21.], dtype=float32)>
"""
tf.print(tf.maximum(a, b))

"""
[5 8]
"""
tf.print(tf.minimum(a, b))

"""
[1 6]
"""

向量运算

  • 向量运算符只在一个特定轴上运算,将一个向量映射到一个标量或者另外一个向量。 许多向量运算符都以reduce开头
# 向量reduce
a = tf.range(1, 10)
tf.print(a)
tf.print("求和:", tf.reduce_sum(a))
tf.print("均值:", tf.reduce_mean(a))
tf.print("最大值:", tf.reduce_max(a))
tf.print("最小值:", tf.reduce_min(a))

# 函数计算一个张量的各个维度上元素的乘积
tf.print("乘积:", tf.reduce_prod(a))

"""
求和: 45
均值: 5
最大值: 9
最小值: 1
乘积: 362880
"""
# 张量指定维度进行reduce
b = tf.reshape(a, (3, 3))
tf.print(b)

tf.print(tf.reduce_sum(b, axis=1, keepdims=True))  # 行方向求和
tf.print(tf.reduce_sum(b, axis=1, keepdims=False))
tf.print(tf.reduce_sum(b, axis=0, keepdims=True))  # 列方向求和

"""
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[6]
 [15]
 [24]]
[6 15 24]
[[12 15 18]]
"""
# bool类型的reduce
p = tf.constant([True, False, False])
q = tf.constant([False, False, True])
tf.print(tf.reduce_all(p))
tf.print(tf.reduce_any(q))

"""
0
1
"""
# 利用tf.foldr实现tf.reduce_sum
s = tf.foldr(lambda a, b: a+b, tf.range(10))
tf.print(s)

"""
45
"""
tf.print(tf.reduce_sum(tf.range(10)))

"""
45
"""
  • cumsum()累积连加
  • cumprod()累积连乘
# cum扫描累积
a = tf.range(1, 10)
tf.print(tf.math.cumsum(a))  # 沿着tensor(张量)x的某一个维度axis,计算累积和
tf.print(tf.math.cumprod(a))

"""
[1 3 6 ... 28 36 45]
[1 2 6 ... 5040 40320 362880]
"""
# arg最大最小值索引
a = tf.range(1, 10)
tf.print(a)
tf.print(tf.argmax(a))
tf.print(tf.argmin(a))

"""
[1 2 3 ... 7 8 9]
8
0
"""
# tf.match.top_k 用于对张量的排序
a = tf.constant([1, 3, 7, 5, 4, 8])

values, indexes = tf.math.top_k(a, 3, sorted=True)
tf.print(values)
tf.print(indexes)

"""
[8 7 5]
[5 2 3]
"""

矩阵运算

  • 矩阵必须是二维的。类似tf.constant([1,2,3])这样的不是矩阵
  • 矩阵运算包括:矩阵乘法,矩阵转置,矩阵逆,矩阵求迹,矩阵范数,矩阵行列式,矩阵求特征值,矩阵分解等运算
  • 除了一些常用的运算外,大部分和矩阵有关的运算都在tf.linalg子包中
# 矩阵乘法
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[2, 0], [0, 2]])
tf.print(a@b)
tf.print("")
tf.print(tf.matmul(a, b))

"""
[[2 4]
 [6 8]]

[[2 4]
 [6 8]]
"""
# 矩阵转置
a = tf.constant([[1.0, 2], [3, 4]])
tf.print(a)
tf.print(tf.transpose(a))

"""
[[1 2]
 [3 4]]
[[1 3]
 [2 4]]
"""
# 矩阵逆,必需为tf.float32或tf.double类型
a = tf.constant([[1.0, 2], [3.0, 4]], dtype=tf.float32)
tf.print(a)
tf.print("")
tf.print(tf.linalg.inv(a))

"""
# 矩阵逆,必需为tf.float32或tf.double类型

a = tf.constant([[1.0, 2], [3.0, 4]], dtype=tf.float32)

tf.print(a)

tf.print("")

tf.print(tf.linalg.inv(a))

[[1 2]
 [3 4]]

[[-2 1]
 [1.5 -0.5]]
"""
  • 矩阵的迹(trace)表示矩阵 AAA 主对角线所有元素的和,即 tr(A)=a11+a22+⋯+ann
# 矩阵求trace
a = tf.constant([[1.0, 2], [3, 4]])
tf.print(a)
tf.print(tf.linalg.trace(a))

"""
[[1 2]
 [3 4]]
5
"""
# 矩阵求范数
a = tf.constant([[1.0, 2], [3, 4]])
tf.linalg.norm(a)

"""
<tf.Tensor: shape=(), dtype=float32, numpy=5.4772253>
"""
# 矩阵行列式
a = tf.constant([[1.0, 2], [3, 4]])
tf.print(tf.linalg.det(a))

"""
-2
"""
# 矩阵特征值
tf.print(tf.linalg.eigvalsh(a))

"""
[-0.854101896 5.85410213]
"""
# 矩阵qr分解
# 正交分解
a = tf.constant([[1.0, 2.0], [3.0, 4.0]], dtype=tf.float32)
q, r = tf.linalg.qr(a)
tf.print(q)
tf.print(r)
tf.print(q@r)

"""
[[-0.316227794 -0.948683321]
 [-0.948683321 0.316227734]]
[[-3.1622777 -4.4271884]
 [0 -0.632455349]]
[[1.00000012 1.99999976]
 [3 4]]
"""
# 矩阵SVD分解
a = tf.constant([[1.0, 2.0], [3.0, 4.0]], dtype=tf.float32)
v, s, d = tf.linalg.svd(a)
tf.print(v)
tf.print("对角矩阵:", tf.linalg.diag(v))
tf.print(s)
tf.print(d)
tf.print(tf.matmul(tf.matmul(s, tf.linalg.diag(v)), d, transpose_b=True))
"""
transpose_a: 如果为真, a则在进行乘法计算前进行转置。
transpose_b: 如果为真, b则在进行乘法计算前进行转置。
adjoint_a: 如果为真, a则在进行乘法计算前进行共轭和转置。
adjoint_b: 如果为真, b则在进行乘法计算前进行共轭和转置。
a_is_sparse: 如果为真, a会被处理为稀疏矩阵。
b_is_sparse: 如果为真, b会被处理为稀疏矩阵。
"""

"""
[5.46498537 0.365966141]
对角矩阵: [[5.46498537 0]
 [0 0.365966141]]
[[-0.404553592 -0.914514303]
 [-0.914514303 0.404553533]]
[[-0.576048493 0.817415535]
 [-0.817415535 -0.576048493]]
[[1.00000024 2]
 [3.00000024 4]]
"""

广播机制

  • TensorFlow的广播规则和numpy是一样的
    • 1、如果张量的维度不同,将维度较小的张量进行扩展,直到两个张量的维度都一样。
    • 2、如果两个张量在某个维度上的长度是相同的,或者其中一个张量在该维度上的长度为1,那么我们就说这两个张量在该维度上是相容的。
    • 3、如果两个张量在所有维度上都是相容的,它们就能使用广播。
    • 4、广播之后,每个维度的长度将取两个张量在该维度长度的较大值。
    • 5、在任何一个维度上,如果一个张量的长度为1,另一个张量长度大于1,那么在该维度上,就好像是对第一个张量进行了复制。
  • tf.broadcast_to 以显式的方式按照广播机制扩展张量的维度。
a = tf.constant([1, 2, 3])
b = tf.constant([[0, 0, 0], [1, 1, 1], [2, 2, 2]])
tf.print(a)
tf.print("")
tf.print(b)
tf.print("")
tf.print("a + b:")
tf.print(a + b)

"""
[1 2 3]

[[0 0 0]
 [1 1 1]
 [2 2 2]]

a + b:
[[1 2 3]
 [2 3 4]
 [3 4 5]]
"""
tf.broadcast_to(a, b.shape)  # 以显示的方式按照广播机制扩展张量的维度

"""
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]], dtype=int32)>
"""
# 计算广播后结果的形状,静态形状,TensorShape类型参数
tf.broadcast_static_shape(a.shape, b.shape)

"""
TensorShape([3, 3])
"""
# 计算广播后计算结果的形状,动态形状,Tensor类型参数
c = tf.constant([1,2,3])
d = tf.constant([[1],[2],[3]])
tf.broadcast_dynamic_shape(tf.shape(c), tf.shape(d))

"""
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 3], dtype=int32)>
"""
# 广播效果
c + d  # 等价于tf.broadcast_to(c, [3, 3] + tf.broadcast_to(d, [3, 3]))

"""
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]], dtype=int32)>
"""
tf.broadcast_to(c, [3, 3]) + tf.broadcast_to(d, [3, 3])

"""
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]], dtype=int32)>
"""
posted @ 2022-06-20 22:42  lotuslaw  阅读(63)  评论(0编辑  收藏  举报