# 4-2,张量的数学运算

``````
``````

### 一，标量运算

``````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=(3,), dtype=int32, numpy=array([1, 2, 0], dtype=int32)>
``````
``````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.equal(a,5)
``````
``````<tf.Tensor: shape=(3,), dtype=bool, numpy=array([False, False, False])>
``````
``````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.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]
``````
``````x = tf.constant([2.6,-2.7])

tf.print(tf.math.round(x)) #保留整数部分，四舍五入
tf.print(tf.math.floor(x)) #保留整数部分，向下归整
tf.print(tf.math.ceil(x))  #保留整数部分，向上归整

``````
``````[3 -3]
[2 -3]
[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)
z = tf.clip_by_norm(x,clip_norm = 3)
tf.print(y)
tf.print(z)
``````
``````[0.9 -0.8 1 -1 0.7]
[0.0264732055 -0.0235317405 2.94146752 -0.588293493 0.0205902718]
``````
``````
``````
``````
``````

### 二，向量运算

``````#向量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))

``````
``````45
5
9
1
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))
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
``````
``````#cum扫描累积
a = tf.range(1,10)
tf.print(tf.math.cumsum(a))
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(tf.argmax(a))
tf.print(tf.argmin(a))
``````
``````8
0
``````
``````#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)

#利用tf.math.top_k可以在TensorFlow中实现KNN算法
``````
``````[8 7 5]
[5 2 3]
``````
``````
``````
``````
``````

### 三，矩阵运算

``````#矩阵乘法
a = tf.constant([[1,2],[3,4]])
b = tf.constant([[2,0],[0,2]])
a@b  #等价于tf.matmul(a,b)
``````
``````<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[2, 4],
[6, 8]], dtype=int32)>
``````
``````#矩阵转置
a = tf.constant([[1,2],[3,4]])
tf.transpose(a)
``````
``````<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 3],
[2, 4]], dtype=int32)>
``````
``````#矩阵逆，必须为tf.float32或tf.double类型
a = tf.constant([[1.0,2],[3,4]],dtype = tf.float32)
tf.linalg.inv(a)
``````
``````<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-2.0000002 ,  1.0000001 ],
[ 1.5000001 , -0.50000006]], dtype=float32)>
``````
``````#矩阵求trace
a = tf.constant([[1.0,2],[3,4]],dtype = tf.float32)
tf.linalg.trace(a)
``````
``````<tf.Tensor: shape=(), dtype=float32, numpy=5.0>
``````
``````#矩阵求范数
a = tf.constant([[1.0,2],[3,4]])
tf.linalg.norm(a)
``````
``````<tf.Tensor: shape=(), dtype=float32, numpy=5.477226>
``````
``````#矩阵行列式
a = tf.constant([[1.0,2],[3,4]])
tf.linalg.det(a)
``````
``````<tf.Tensor: shape=(), dtype=float32, numpy=-2.0>
``````
``````#矩阵特征值
a = tf.constant([[1.0,2],[-5,4]])
tf.linalg.eigvals(a)
``````
``````<tf.Tensor: shape=(2,), dtype=complex64, numpy=array([2.4999995+2.7838817j, 2.5      -2.783882j ], dtype=complex64)>
``````
``````#矩阵QR分解, 将一个方阵分解为一个正交矩阵q和上三角矩阵r
#QR分解实际上是对矩阵a实施Schmidt正交化得到q

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分解
#svd分解可以将任意一个矩阵分解为一个正交矩阵u,一个对角阵s和一个正交矩阵v.t()的乘积
#svd常用于矩阵压缩和降维

a  = tf.constant([[1.0,2.0],[3.0,4.0],[5.0,6.0]], dtype = tf.float32)
s,u,v = tf.linalg.svd(a)
tf.print(u,"\n")
tf.print(s,"\n")
tf.print(v,"\n")
tf.print(u@tf.linalg.diag(s)@tf.transpose(v))

#利用svd分解可以在TensorFlow中实现主成分分析降维

``````
``````[[0.229847744 -0.88346082]
[0.524744868 -0.240782902]
[0.819642067 0.401896209]]

[9.52551842 0.51429987]

[[0.619629562 0.784894466]
[0.784894466 -0.619629562]]

[[1.00000119 2]
[3.00000095 4.00000048]
[5.00000143 6.00000095]]
``````
``````
``````
``````
``````

### 四，广播机制

TensorFlow的广播规则和numpy是一样的:

• 1、如果张量的维度不同，将维度较小的张量进行扩展，直到两个张量的维度都一样。
• 2、如果两个张量在某个维度上的长度是相同的，或者其中一个张量在该维度上的长度为1，那么我们就说这两个张量在该维度上是相容的。
• 3、如果两个张量在所有维度上都是相容的，它们就能使用广播。
• 4、广播之后，每个维度的长度将取两个张量在该维度长度的较大值。
• 5、在任何一个维度上，如果一个张量的长度为1，另一个张量长度大于1，那么在该维度上，就好像是对第一个张量进行了复制。

``````a = tf.constant([1,2,3])
b = tf.constant([[0,0,0],[1,1,1],[2,2,2]])
b + a  #等价于 b + tf.broadcast_to(a,b.shape)
``````
``````<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]], dtype=int32)>
``````
``````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类型参数
``````
``````TensorShape([3, 3])
``````
``````#计算广播后计算结果的形状，动态形状，Tensor类型参数
c = tf.constant([1,2,3])
d = tf.constant([[1],[2],[3]])
``````
``````<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 3], dtype=int32)>
``````
``````#广播效果
``````
``````<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6]], dtype=int32)>
``````
``````
``````

``````
``````