张量的数学运算
张量数学运算主要有:标量运算,向量运算,矩阵运算。另外我们会介绍张量运算的广播机制。
1.标量运算
张量的数学运算符可以分为标量运算符、向量运算符、以及矩阵运算符。
加减乘除乘方,以及三角函数,指数,对数等常见函数,逻辑比较运算符等都是标量运算符。
标量运算符的特点是对张量实施逐元素运算。
有些标量运算符对常用的数学运算符进行了重载。并且支持类似numpy的广播特性。
许多标量运算符都在 tf.math模块下。
a = tf.constant([[1.0,2],[-3,4.0]])
b = tf.constant([[5.0,6],[7.0,8.0]])
#mod的运算符重载,等价于m = tf.math.mod(a,3)
a%3
》》<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 0], dtype=int32)>
(a>=2)&(a<=3)
》》<tf.Tensor: shape=(2, 2), dtype=bool, numpy=
》》array([[False, True],
》》 [False, False]])>
x = tf.constant([2.6,-2.7])
tf.print(tf.math.round(x)) #保留整数部分,四舍五入==》[3 -3]
tf.print(tf.math.floor(x)) #保留整数部分,向下归整==》[2 -3]
tf.print(tf.math.ceil(x)) #保留整数部分,向上归整==》[3 -2]
# 幅值裁剪
x = tf.constant([0.9,-0.8,100.0,-20.0,0.7])
y = tf.clip_by_value(x,clip_value_min=-1,clip_value_max=1)
#tf.clip_by_norm(t, clip_norm, axes=None, name=None)
#类似一种规范化,使得沿某一维度的L2范数小于clip_norm值
# * clip_norm / l2norm(t)
z = tf.clip_by_norm(x,clip_norm = 3)
tf.print(y) ==》[0.9 -0.8 1 -1 0.7]
tf.print(z) ==》[0.0264732055 -0.0235317405 2.94146752 -0.588293493 0.0205902718]
2.向量运算
2.1.reduce
向量运算符只在一个特定轴上运算,将一个向量映射到一个标量或者另外一个向量。 许多向量运算符都以reduce开头。
#向量reduce
a = tf.range(1,10)
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)) ===》362880
张量指定维度进行reduce
b = tf.reshape(a,(3,3))
tf.print(tf.reduce_sum(b, axis=1, keepdims=True))
tf.print(tf.reduce_sum(b, axis=0, keepdims=True))
结果:
[[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)) ==》0
tf.print(tf.reduce_any(q)) ==》1
2.2.cum扫描累积
cum扫描累积:
a = tf.range(1,10)
tf.print(tf.math.cumsum(a)) ==========》[1 3 6 ... 28 36 45]
tf.print(tf.math.cumprod(a)) =========》[1 2 6 ... 5040 40320 362880]
2.3.arg最大最小值索引
a = tf.range(1,10)
tf.print(tf.argmax(a)) ==》8
tf.print(tf.argmin(a)) ==》0
2.4.math.top_k对张量排序
tf.math.top_k可以用于对张量排序
a = tf.constant([1,3,7,5,4,8])
values,indices = tf.math.top_k(a,3,sorted=True)
tf.print(values)
tf.print(indices)
3.矩阵运算
矩阵必须是二维的。类似tf.constant([1,2,3])这样的不是矩阵。
矩阵运算包括:矩阵乘法,矩阵转置,矩阵逆,矩阵求迹,矩阵范数,矩阵行列式,矩阵求特征值,矩阵分解等运算。
除了一些常用的运算外,大部分和矩阵有关的运算都在tf.linalg子包中。
#矩阵乘法
a = tf.constant([[1,2],[3,4]])
b = tf.constant([[2,0],[0,2]])
a@b #等价于tf.matmul(a,b)
#矩阵转置
tf.transpose(a)
#矩阵逆,必须为tf.float32或tf.double类型
tf.linalg.inv(a)
#矩阵求trace
tf.linalg.trace(a)
#矩阵求范数
tf.linalg.norm(a)
#矩阵行列式
tf.linalg.det(a)
#矩阵特征值
tf.linalg.eigvals(a)
#矩阵QR分解, 将一个方阵分解为一个正交矩阵q和上三角矩阵r
#QR分解实际上是对矩阵a实施Schmidt正交化得到q
q,r = tf.linalg.qr(a)
#矩阵svd分解
#svd分解可以将任意一个矩阵分解为一个正交矩阵u,一个对角阵s和一个正交矩阵v.t()的乘积
#svd常用于矩阵压缩和降维
s,u,v = tf.linalg.svd(a)
4.广播机制tf.broadcast_to(a,b.shape)
Broadcasting 也叫广播机制(自动扩展也许更合适),它是一种轻量级张量复制的手段,在逻辑上扩展张量数据的形状,但是只要在需要时才会执行实际存储复制操作。
- 如果张量的维度不同,将维度较小的张量进行扩展,直到两个张量的维度都一样。
- 如果两个张量在某个维度上的长度是相同的,或者其中一个张量在该维度上的长度为1,那么我们就说这两个张量在该维度上是相容的。
- 如果两个张量在所有维度上都是相容的,它们就能使用广播。
- 广播之后,每个维度的长度将取两个张量在该维度长度的较大值。
- 在任何一个维度上,如果一个张量的长度为1,另一个张量长度大于1,那么在该维度上,就好像是对第一个张量进行了复制。
tf.broadcast_to(a,b.shape)`
示例:
a = tf.constant([1,2,3])
b = tf.constant([[0,0,0],[1,1,1],[2,2,2]])
tf.broadcast_to(a,b.shape)
c = tf.constant([1,2,3])
d = tf.constant([[1],[2],[3]])
c+d #等价于 tf.broadcast_to(c,[3,3]) + tf.broadcast_to(d,[3,3])
结果:
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]], dtype=int32)
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6]], dtype=int32)>