@@ -13,6 +13,7 @@ Pytorch 是一种开源机器学习框架,可加速从研究原型设计到生
13
13
- [ Pytorch 官方备忘清单] ( https://pytorch.org/tutorials/beginner/ptcheat.html ) _ (pytorch.org)_
14
14
15
15
### 认识 Pytorch
16
+ <!-- rehype:wrap-class=row-span-2-->
16
17
17
18
``` python
18
19
from __future__ import print_function
@@ -32,6 +33,7 @@ tensor([
32
33
Tensors 张量: 张量的概念类似于Numpy中的ndarray数据结构, 最大的区别在于Tensor可以利用GPU的加速功能.
33
34
34
35
### 创建一个全零矩阵
36
+ <!-- rehype:wrap-class=row-span-2-->
35
37
36
38
``` python
37
39
x = torch.zeros(5 , 3 , dtype = torch.long)
@@ -95,6 +97,7 @@ tensor([[ 1.6978, -1.6979, 0.3093],
95
97
```
96
98
97
99
### 加法操作(4)
100
+ <!-- rehype:wrap-class=row-span-2-->
98
101
99
102
``` python
100
103
y.add_(x)
@@ -118,6 +121,7 @@ tensor([-2.0902, -0.4489, -0.1441, 0.8035, -0.8341])
118
121
<!-- rehype:className=wrap-text-->
119
122
120
123
### 张量形状
124
+ <!-- rehype:wrap-class=row-span-2-->
121
125
122
126
``` python
123
127
x = torch.randn(4 , 4 )
@@ -178,61 +182,81 @@ tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
178
182
179
183
``` python
180
184
>> > x = torch.rand(1 , 2 , 1 , 28 , 1 )
181
- >> > x.squeeze().shape # squeeze不加参数,默认去除所有为1的维度
185
+
186
+ # squeeze不加参数,默认去除所有为1的维度
187
+ >> > x.squeeze().shape
182
188
torch.Size([2 , 28 ])
183
- >> > x.squeeze(dim = 0 ).shape # squeeze加参数,去除指定为1的维度
189
+
190
+ # squeeze加参数,去除指定为1的维度
191
+ >> > x.squeeze(dim = 0 ).shape
184
192
torch.Size([2 , 1 , 28 , 1 ])
185
- >> > x.squeeze(1 ).shape # squeeze加参数,如果不为1,则不变
193
+
194
+ # squeeze加参数,如果不为1,则不变
195
+ >> > x.squeeze(1 ).shape
186
196
torch.Size([1 , 2 , 1 , 28 , 1 ])
187
- >> > torch.squeeze(x,- 1 ).shape # 既可以是函数,也可以是方法
197
+
198
+ # 既可以是函数,也可以是方法
199
+ >> > torch.squeeze(x,- 1 ).shape
188
200
torch.Size([1 , 2 , 1 , 28 ])
189
201
```
190
202
191
203
### unsqueeze函数
192
204
193
205
``` python
194
206
>> > x = torch.rand(2 , 28 )
195
- >> > x.unsqueeze(0 ).shape # unsqueeze必须加参数, _ 2 _ 28 _
196
- torch.Size([1 , 2 , 28 ]) # 参数代表在哪里添加维度 0 1 2
197
- >> > torch.unsqueeze(x, - 1 ).shape # 既可以是函数,也可以是方法
207
+ # unsqueeze必须加参数, _ 2 _ 28 _
208
+ >> > x.unsqueeze(0 ).shape
209
+ # 参数代表在哪里添加维度 0 1 2
210
+ torch.Size([1 , 2 , 28 ])
211
+ # 既可以是函数,也可以是方法
212
+ >> > torch.unsqueeze(x, - 1 ).shape
198
213
torch.Size([2 , 28 , 1 ])
199
214
```
200
215
201
216
Cuda 相关
202
217
---
218
+
203
219
### 检查 Cuda 是否可用
220
+
204
221
``` python
205
222
>> > import torch.cuda
206
223
>> > torch.cuda.is_available()
207
224
>> > True
208
225
```
226
+
209
227
### 列出 GPU 设备
228
+ <!-- rehype:wrap-class=col-span-2 row-span-2-->
229
+
210
230
``` python
211
231
import torch
232
+
212
233
device_count = torch.cuda.device_count()
213
234
print (" CUDA 设备" )
235
+
214
236
for i in range (device_count):
215
237
device_name = torch.cuda.get_device_name(i)
216
238
total_memory = torch.cuda.get_device_properties(i).total_memory / (1024 ** 3 )
217
239
print (f " ├── 设备 { i} : { device_name} , 容量: { total_memory:.2f } GiB " )
240
+
218
241
print (" └── (结束)" )
219
242
```
243
+
220
244
### 将模型、张量等数据在 GPU 和内存之间进行搬运
245
+
221
246
``` python
222
247
import torch
223
- # Replace 0 to your GPU device index. or use "cuda" directly.
248
+ # 将 0 替换为您的 GPU 设备索引或者直接使用 "cuda"
224
249
device = f " cuda:0 "
225
- # Move to GPU
250
+ # 移动到GPU
226
251
tensor_m = torch.tensor([1 , 2 , 3 ])
227
252
tensor_g = tensor_m.to(device)
228
253
model_m = torch.nn.Linear(1 , 1 )
229
254
model_g = model_m.to(device)
230
- # Move back.
255
+ # 向后移动
231
256
tensor_m = tensor_g.cpu()
232
257
model_m = model_g.cpu()
233
258
```
234
259
235
-
236
260
导入 Imports
237
261
---
238
262
@@ -241,61 +265,87 @@ model_m = model_g.cpu()
241
265
``` python
242
266
# 根包
243
267
import torch
244
- # 数据集表示和加载
268
+ ```
269
+
270
+ 数据集表示和加载
271
+
272
+ ``` python
245
273
from torch.utils.data import Dataset, DataLoader
246
274
```
247
275
<!-- rehype:className=wrap-text-->
248
276
249
277
### 神经网络 API
278
+ <!-- rehype:wrap-class=row-span-2-->
250
279
251
280
``` python
252
281
# 计算图
253
282
import torch.autograd as autograd
254
283
# 计算图中的张量节点
255
284
from torch import Tensor
256
- # 神经网络
285
+ ```
286
+
287
+ 神经网络
288
+
289
+ ``` python
257
290
import torch.nn as nn
291
+
258
292
# 层、激活等
259
293
import torch.nn.functional as F
260
294
# 优化器,例如 梯度下降、ADAM等
261
295
import torch.optim as optim
262
- # 混合前端装饰器和跟踪 jit
263
- from torch.jit import script, trace
264
296
```
265
297
266
- ### Torchscript 和 JIT
267
-
268
- ``` python
269
- torch.jit.trace()
270
- ```
271
-
272
- 使用你的模块或函数和一个例子,数据输入,并追溯计算步骤,数据在模型中前进时遇到的情况
298
+ 混合前端装饰器和跟踪 jit
273
299
274
300
``` python
275
- @ script
301
+ from torch.jit import script, trace
276
302
```
277
303
278
- 装饰器用于指示被跟踪代码中的数据相关控制流
279
-
280
304
### ONNX
305
+ <!-- rehype:wrap-class=row-span-2-->
281
306
282
307
``` python
283
308
torch.onnx.export(model, dummy data, xxxx.proto)
284
309
# 导出 ONNX 格式
285
310
# 使用经过训练的模型模型,dummy
286
311
# 数据和所需的文件名
312
+ ```
313
+ <!-- rehype:className=wrap-text-->
287
314
315
+ 加载 ONNX 模型
316
+
317
+ ``` python
288
318
model = onnx.load(" alexnet.proto" )
289
- # 加载 ONNX 模型
319
+ ```
320
+
321
+ 检查模型,IT 是否结构良好
322
+
323
+ ``` python
290
324
onnx.checker.check_model(model)
291
- # 检查模型,IT 是否结构良好
325
+ ```
326
+
327
+ 打印一个人类可读的,图的表示
292
328
329
+ ``` python
293
330
onnx.helper.printable_graph(model.graph)
294
- # 打印一个人类可读的,图的表示
295
331
```
296
- <!-- rehype:className=wrap-text-->
332
+
333
+ ### Torchscript 和 JIT
334
+
335
+ ``` python
336
+ torch.jit.trace()
337
+ ```
338
+
339
+ 使用你的模块或函数和一个例子,数据输入,并追溯计算步骤,数据在模型中前进时遇到的情况
340
+
341
+ ``` python
342
+ @script
343
+ ```
344
+
345
+ 装饰器用于指示被跟踪代码中的数据相关控制流
297
346
298
347
### Vision
348
+ <!-- rehype:wrap-class=col-span-2-->
299
349
300
350
``` python
301
351
# 视觉数据集,架构 & 变换
0 commit comments