当前位置:网站首页>李沐 《动手学深度学习》学习笔记 (5)第一章 预备知识 第三节 线性代数
李沐 《动手学深度学习》学习笔记 (5)第一章 预备知识 第三节 线性代数
2022-07-21 05:12:00 【Artificial Idiots】
1.3 线性代数
1.3.1 标量
#标量由只有一个元素的张量表示
from mxnet import np, npx
npx.set_np()
x = np.array(3.0)
y = np.array(2.0)
x + y, x * y, x / y, x**y
(array(5.), array(6.), array(1.5), array(9.))
1.3.2 向量
x = np.arange(4)
x
array([0., 1., 2., 3.])
len(x)
4
x.shape
(4,)
x = np.array([[0],[1],[2],[3]])
x.shape
(4, 1)
1.3.3 矩阵
A = np.arange(20).reshape(5,4)
A
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
A.T
array([[ 0., 4., 8., 12., 16.],
[ 1., 5., 9., 13., 17.],
[ 2., 6., 10., 14., 18.],
[ 3., 7., 11., 15., 19.]])
1.3.4 张量
X = np.arange(24).reshape(2,3,4)
X
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[12., 13., 14., 15.],
[16., 17., 18., 19.],
[20., 21., 22., 23.]]])
1.3.5 张量算法的基本性质
A = np.arange(20).reshape(5,4)
B = A.copy()
A, A + B
(array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]]),
array([[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.],
[16., 18., 20., 22.],
[24., 26., 28., 30.],
[32., 34., 36., 38.]]))
#两个矩阵按元素乘法称为达哈玛积
A * B, A**2
(array([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]]),
array([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]]))
#将张量乘以或者加上一个标量不会改变张量的形状,张量的每个元素都将与标量相乘或相加
a = 2
X = np.arange(24).reshape(2,3,4)
a + X, (a * X).shape
(array([[[ 2., 3., 4., 5.],
[ 6., 7., 8., 9.],
[10., 11., 12., 13.]],
[[14., 15., 16., 17.],
[18., 19., 20., 21.],
[22., 23., 24., 25.]]]),
(2, 3, 4))
1.3.6 汇总
x = np.arange(4)
x, x.sum()
(array([0., 1., 2., 3.]), array(6.))
A, A.shape, A.sum()
(array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]]),
(5, 4),
array(190.))
A_sum_axis0 = A.sum(axis = 0)
A_sum_axis0, A_sum_axis0.shape
(array([40., 45., 50., 55.]), (4,))
A_sum_axis1 = A.sum(axis = 1)
A_sum_axis1, A_sum_axis1.shape
(array([ 6., 22., 38., 54., 70.]), (5,))
A.sum(axis = [0,1]) #沿着行列进行求和
#求平均值
A.mean(), A.sum() / A.size
(array(9.5), array(9.5))
A.mean(axis = 0), A.sum(axis = 0) / A.shape[0]
(array([ 8., 9., 10., 11.]), array([ 8., 9., 10., 11.]))
#非汇总求和,在调用函数来计算总和或均值时保持轴数不变
sum_A = A.sum(axis = 1, keepdims = True)
sum_A
array([[ 6.],
[22.],
[38.],
[54.],
[70.]])
A / sum_A
array([[0. , 0.16666667, 0.33333334, 0.5 ],
[0.18181819, 0.22727273, 0.27272728, 0.3181818 ],
[0.21052632, 0.23684211, 0.2631579 , 0.28947368],
[0.22222222, 0.24074075, 0.25925925, 0.2777778 ],
[0.22857143, 0.24285714, 0.25714287, 0.27142859]])
#计算沿着某个轴计算A元素的累积总和
A.cumsum(axis = 0)
array([[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[12., 15., 18., 21.],
[24., 28., 32., 36.],
[40., 45., 50., 55.]])
1.3.7 点积
y = np.ones(4)
x, y, np.dot(x, y), np.sum(x * y)
(array([0., 1., 2., 3.]), array([1., 1., 1., 1.]), array(6.), array(6.))
1.3.8 矩阵-向量积
A.shape, x.shape, np.dot(A, x)
((5, 4), (4,), array([ 14., 38., 62., 86., 110.]))
1.3.9 矩阵-矩阵乘法
B = np.ones(shape = (4,3))
np.dot(A, B)
array([[ 6., 6., 6.],
[22., 22., 22.],
[38., 38., 38.],
[54., 54., 54.],
[70., 70., 70.]])
1.3.10 范数
u = np.array([3,-4])
np.linalg.norm(u)
array(5.)
np.abs(u).sum()
array(7.)
np.linalg.norm(np.ones((4,9)))
array(6.)
边栏推荐
- Resolution: failed to connect the pipeline to the virtual machine: all pipeline instances are in use
- YOLO7环境搭建、代码测试
- Transplant grpc to arm board
- 生成模型笔记(四):MCMC和Gibbs Sampling
- [wechat applet learning notes] two positions of pop-up window
- 一文搞懂静态库/动态库链接问题
- PostgreSQL database master-slave deployment
- 论文导航
- 基于EasyCV复现ViTDet:单层特征超越FPN
- Flutter实战-WidgetsFlutterBinding
猜你喜欢
Overview of quantum measurement, quantum computation and quantum communication
量子测量 量子计算 量子通讯 概述
生成模型笔记(二):最大似然,贝叶斯和最大后验概率
基于EasyCV复现DETR和DAB-DETR,Object Query的正确打开方式
GRPC移植到ARM板子上
002_SSSS_ Denoising Diffusion Implicit Models
Detailed explanation of SQL Server index Foundation___ Concept and principle
Configure alarm messages for SQL Server
Invalid bound statement (not found): com. example. demo1.mapper. UserMapper. XXX
Getting started with kotlin
随机推荐
音视频开发学习笔记(一)
【乐视云学习笔记】关于Letv乐视云点播的视频暂停之后,按home回到桌面后重新onResume回到Activity,视频自动播放的情况
007_SSSSS_ Neural Ordinary Differential Equtions
Swin_Transformer_minivit代码解读
Kubernetes资源编排系列之一: Pod YAML篇
生成模型笔记(一):概率基础知识
Transplant grpc to arm board
基于EasyCV复现ViTDet:单层特征超越FPN
007_SSSSS_ Neural Ordinary Differential Equtions
子View设置LayoutParams滑动不流畅闪动问题
Wonderful journey of quantum mechanics - Operator / Schrodinger equation / probability current density
音视频学习笔记(雷神)—技术解析
openssl+poly1305+sm4实现
SQL server and azure SQL index architecture and Design Guide
Camera2 闪光灯梳理
高质量文章导航
[SQLite3 database]
Install PostgreSQL on centos7
【使用Kotlin编写您编写的第一个程序】
Do you know what are the schemes of the list paging interface?