Skip to content

Commit 7915309

Browse files
committed
d2l: v05 notes
1 parent acda71c commit 7915309

File tree

2 files changed

+163
-2
lines changed

2 files changed

+163
-2
lines changed

content/posts/2021-03-21_d2l-v04.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
+++
2-
date = '2021-03-21T5:05:25+08:00'
2+
date = '2021-03-21T05:05:25+08:00'
33
draft = false
44
title = 'Dive into DeepLearning - Preliminaries'
55
tags = ['DeepLearning']
66
+++
77

88
- Course Note: d2l-video-04 - 数据操作+数据预处理
9-
- Jupyter Notebook: chapter\_preliminaries/
9+
- Jupyter Notebook: chapter\_preliminaries/pandas.ipynb
10+
11+
预备知识中 Data Manipulation 和 Data Preprocessing 的部分
1012

1113
### 介绍
1214

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
+++
2+
date = '2021-03-21T05:20:08+08:00'
3+
draft = false
4+
title = 'Dive into DeepLearning - Preliminaries'
5+
tags = ['DeepLearning']
6+
+++
7+
8+
- Course Note: d2l-video-05 - 线性代数
9+
- Jupyter Notebook: chapter\_preliminaries/linear-algebra.ipynb
10+
11+
预备知识中 Liner Algebra 的部分
12+
13+
### 线性代数
14+
- Scalars 标量: 指只有一个元素的张量 tensors
15+
```Python
16+
import torch
17+
x = torch.tensor(3.0) # scalar
18+
y = torch.tensor(2.0)
19+
```
20+
21+
- Vectors 向量: 可以视作标量构成的列表
22+
```Python
23+
x = torch.arange(4)
24+
x[3] # 通过张量索引访问任一元素
25+
len(x) # 访问张量长度
26+
x.shape # torch.Size([4]) 只有一个轴的张量, 形状只有一个元素
27+
```
28+
29+
- Matrices 矩阵: 类似向量的推广, 可以构建更多轴的数据结构
30+
```Python
31+
# 构建矩阵
32+
A = torch.arange(20).reshape(5, 4)
33+
A.T # 转置
34+
35+
# 对称矩阵
36+
B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])
37+
B = B.T
38+
```
39+
40+
形状相同张量的计算
41+
```Python
42+
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
43+
B = A.clone()
44+
A, A + B
45+
A * B # 对应元素相乘: Hadamard 积
46+
```
47+
48+
计算元素的和
49+
```Python
50+
x = torch.arange(4, detype=torch.float64)
51+
x.sum() # 任意形状张量的和
52+
```
53+
54+
计算平均值
55+
```Python
56+
A.mean() # 均值
57+
A.sum() / A.numel() # 另一种计算均值的方法: 和 / 数量
58+
```
59+
60+
点乘是相同位置元素乘积的和
61+
```Python
62+
x = torch.tensor([0., 1., 2., 3.]])
63+
y = torch.tensor([1., 1., 1., 1.]])
64+
torch.dot(x, y) # torch(6.)
65+
66+
# 或者通过元素乘法, 求和表示点积
67+
torch.sum(x * y) # torch(6.)
68+
```
69+
70+
- 降维: axis 指定沿着哪一个轴来降低纬度
71+
72+
假如现在有个张量A如下
73+
```
74+
tensor([[ 0., 1., 2., 3.],
75+
[ 4., 5., 6., 7.],
76+
[ 8., 9., 10., 11.],
77+
[12., 13., 14., 15.],
78+
[16., 17., 18., 19.]])
79+
```
80+
现在沿着第0轴, 通过求和降低纬度
81+
```Python
82+
A_sum_axis0 = A.sum(axis=0)
83+
A_sum_axis0, A_sum_axis0.shape
84+
```
85+
输出如下
86+
87+
上面降维的原理就是, 由于axis=0, 就将最外层的纬度去掉, 原来 A.shape=torch.Size([5, 4]), 现在变成了A_sum_axis0=torch.Size([4])
88+
```
89+
(tensor([40., 45., 50., 55.]), torch.Size([4]))
90+
```
91+
92+
类似的, 还可以降低多个纬度
93+
```Python
94+
A.sum(axis=[0, 1])
95+
```
96+
由于A就两个轴, 两个轴都被降低就成了标量
97+
```
98+
tensor(190.)
99+
```
100+
101+
此外, 还可以保持纬度不变, 将要降的纬度变成1
102+
```Python
103+
sum_A = A.sum(axis=1, keepdims=True) # keepdims=True 不丢掉原来的纬度
104+
```
105+
输出如下:
106+
原来 A.shape=torch.Size([5, 4]) 现在变成了sum_A.shape=torch.Size([5,1])
107+
```
108+
tensor([[ 6.],
109+
[22.],
110+
[38.],
111+
[54.],
112+
[70.]])
113+
```
114+
这种机制常用于广播, 广播要求纬度相同, 例如 `A / sum_A` 的计算结果如下
115+
```
116+
tensor([[0.0000, 0.1667, 0.3333, 0.5000],
117+
[0.1818, 0.2273, 0.2727, 0.3182],
118+
[0.2105, 0.2368, 0.2632, 0.2895],
119+
[0.2222, 0.2407, 0.2593, 0.2778],
120+
[0.2286, 0.2429, 0.2571, 0.2714]])
121+
```
122+
123+
还可以通过某个轴计算A元素的累积总和 `A.cumsum(axis=0)`
124+
```
125+
tensor([[ 0., 1., 2., 3.],
126+
[ 4., 6., 8., 10.],
127+
[12., 15., 18., 21.],
128+
[24., 28., 32., 36.],
129+
[40., 45., 50., 55.]])
130+
```
131+
132+
- Norms 范数
133+
134+
范数可以理解为"向量的长度/大小"的一种度量方式,
135+
136+
- 向量范数
137+
138+
L1范数, 它表示为**向量**元素的绝对值之和 (曼哈顿距离)
139+
```Python
140+
torch.abs(u).sum()
141+
```
142+
143+
L2范数是**向量**元素平方和的平方根 (欧几里德距离)
144+
```Python
145+
u = torch.tensor([3.0, -4.0])
146+
torch.norm(u)
147+
```
148+
149+
- 矩阵范数: 最小的满足下面公式的值
150+
$$
151+
c = A \cdot b \quad \text{hence} \quad \|c\| \leq \|A\| \cdot \|b\|
152+
$$
153+
**矩阵**的Frobenius范数(Frobenius norm)是矩阵元素平方和的平方根
154+
$$
155+
\|A\|_{Frob} = \left( \sum_{i,j} A_{ij}^2 \right)^{\tfrac{1}{2}}
156+
$$
157+
```Python
158+
torch.norm(torch.ones((4, 9)))
159+
```

0 commit comments

Comments
 (0)