![8de440885362ba841959a46f30978d59.png](https://img-blog.csdnimg.cn/img_convert/8de440885362ba841959a46f30978d59.png)
本文旨在介绍一些常用的神经网络的张量计算公式。
主要参考资料:
- 《矩阵分析与应用》
- 矩阵求导术(上)
- 矩阵求导术(下)
- 《word2vec中的数学原理详解》
- 其他一些网页截图
矩阵基本运算
![af5efc59acd801cbbd7b250424148cb1.png](https://img-blog.csdnimg.cn/img_convert/af5efc59acd801cbbd7b250424148cb1.png)
import numpy as np
##验证:
A=np.arange(1,10).reshape(3,3)
X=np.arange(10,19).reshape(3,3)
print((A*X).reshape(9,1)==np.dot(np.diag(A.flatten()),X.reshape(9,1)))
##验证:
A=np.arange(1,10).reshape(3,3)
X=np.arange(2,14).reshape(3,4)
B=np.arange(3,23).reshape(4,5)
print(A.dot(X).dot(B).reshape(15,1)==np.kron(A,B.T).dot(X.reshape(12,1)))
![b60595b924957d66f5e272a5b603d77d.png](https://img-blog.csdnimg.cn/img_convert/b60595b924957d66f5e272a5b603d77d.png)
![c1a470dd9f9d0e7a46e34fff751a6042.png](https://img-blog.csdnimg.cn/img_convert/c1a470dd9f9d0e7a46e34fff751a6042.png)
损失函数和正则化
损失函数包含模型输出和人工标签。通常损失函数是标量函数,因此可以使用迹运算来辅助求导。
- 线性回归的损失函数
![e464bd60a5038f89a5b9880a05b5fbc3.png](https://img-blog.csdnimg.cn/img_convert/e464bd60a5038f89a5b9880a05b5fbc3.png)
- 加上正则化
![6a7f289d2ef3ea152edad721832bcaef.png](https://img-blog.csdnimg.cn/img_convert/6a7f289d2ef3ea152edad721832bcaef.png)
- 逻辑回归的损失函数
![489e1facee82f5846ac19ec1c751f8f5.png](https://img-blog.csdnimg.cn/img_convert/489e1facee82f5846ac19ec1c751f8f5.png)
- 多分类的损失函数
![99a80de0e27a7458e71fefe35cf36994.png](https://img-blog.csdnimg.cn/img_convert/99a80de0e27a7458e71fefe35cf36994.png)
word2vec
![3c58766cde7f7635904225fce5d2172f.png](https://img-blog.csdnimg.cn/img_convert/3c58766cde7f7635904225fce5d2172f.png)
![4ee65e1007e11b5754b71bff4c996c2b.png](https://img-blog.csdnimg.cn/img_convert/4ee65e1007e11b5754b71bff4c996c2b.png)
![2d00584ff1247cd37e1c94a99e91561f.png](https://img-blog.csdnimg.cn/img_convert/2d00584ff1247cd37e1c94a99e91561f.png)