doc: update pytorch.md #138
This commit is contained in:
parent
78929052b2
commit
cec3afa139
@ -26,8 +26,8 @@ Quick Reference
|
|||||||
[FFmpeg](./docs/ffmpeg.md)<!--rehype:style=background: rgb(0 193 9/var(\-\-bg\-opacity));&class=contributing-->
|
[FFmpeg](./docs/ffmpeg.md)<!--rehype:style=background: rgb(0 193 9/var(\-\-bg\-opacity));&class=contributing-->
|
||||||
[LaTeX](./docs/latex.md)<!--rehype:style=background: rgb(0 128 128/var(\-\-bg\-opacity));&class=contributing-->
|
[LaTeX](./docs/latex.md)<!--rehype:style=background: rgb(0 128 128/var(\-\-bg\-opacity));&class=contributing-->
|
||||||
[MATLAB](./docs/matlab.md)<!--rehype:style=background: rgb(0 118 168/var(\-\-bg\-opacity));&class=contributing-->
|
[MATLAB](./docs/matlab.md)<!--rehype:style=background: rgb(0 118 168/var(\-\-bg\-opacity));&class=contributing-->
|
||||||
[Vue 3 ](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));&class=contributing-->
|
[Vue 3](./docs/vue.md)<!--rehype:style=background: rgb(64 184 131/var(\-\-bg\-opacity));&class=contributing-->
|
||||||
[Pytorch](./docs/pytorch.md)<!--rehype:style=background: rgb(43 91 132/var(\-\-bg\-opacity));&class=contributing&data-info=👆看看还缺点儿什么?-->
|
[Pytorch](./docs/pytorch.md)<!--rehype:style=background: rgb(238 76 44/var(\-\-bg\-opacity));&class=contributing tag&data-lang=Python&data-info=👆看看还缺点儿什么?-->
|
||||||
<!--rehype:class=home-card-->
|
<!--rehype:class=home-card-->
|
||||||
|
|
||||||
## 编程
|
## 编程
|
||||||
|
148
docs/pytorch.md
148
docs/pytorch.md
@ -1,31 +1,35 @@
|
|||||||
Pytorch 备忘清单
|
Pytorch 备忘清单
|
||||||
===
|
===
|
||||||
|
|
||||||
Pytorch 备忘单是 [Pytorch ](https://pytorch.org/) 官网
|
Pytorch 是一种开源机器学习框架,可加速从研究原型设计到生产部署的过程,备忘单是 官网
|
||||||
|
备忘清单为您提供了 [Pytorch](https://pytorch.org/) 基本语法和初步应用参考
|
||||||
|
|
||||||
入门
|
入门
|
||||||
-----
|
-----
|
||||||
|
|
||||||
### 介绍
|
### 介绍
|
||||||
|
|
||||||
- [Pytorch基本语法]
|
- [Pytorch 官网](https://pytorch.org/) _(pytorch.org)_
|
||||||
- [Pytorch初步应用]
|
- [Pytorch 官方备忘清单](https://pytorch.org/tutorials/beginner/ptcheat.html) _(pytorch.org)_
|
||||||
|
|
||||||
### 认识Pytorch
|
### 认识 Pytorch
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import torch
|
import torch
|
||||||
x = torch.empty(5, 3)
|
x = torch.empty(5, 3)
|
||||||
>>> print(x)
|
>>> print(x)
|
||||||
tensor([[2.4835e+27, 2.5428e+30, 1.0877e-19],
|
tensor([
|
||||||
[1.5163e+23, 2.2012e+12, 3.7899e+22],
|
[2.4835e+27, 2.5428e+30, 1.0877e-19],
|
||||||
[5.2480e+05, 1.0175e+31, 9.7056e+24],
|
[1.5163e+23, 2.2012e+12, 3.7899e+22],
|
||||||
[1.6283e+32, 3.7913e+22, 3.9653e+28],
|
[5.2480e+05, 1.0175e+31, 9.7056e+24],
|
||||||
[1.0876e-19, 6.2027e+26, 2.3685e+21]])
|
[1.6283e+32, 3.7913e+22, 3.9653e+28],
|
||||||
|
[1.0876e-19, 6.2027e+26, 2.3685e+21]
|
||||||
|
])
|
||||||
```
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
Tensors张量: 张量的概念类似于Numpy中的ndarray数据结构, 最大的区别在于Tensor可以利用GPU的加速功能.
|
Tensors 张量: 张量的概念类似于Numpy中的ndarray数据结构, 最大的区别在于Tensor可以利用GPU的加速功能.
|
||||||
|
|
||||||
### 创建一个全零矩阵
|
### 创建一个全零矩阵
|
||||||
|
|
||||||
@ -49,10 +53,10 @@ x = torch.tensor([2.5, 3.5])
|
|||||||
tensor([2.5000, 3.3000])
|
tensor([2.5000, 3.3000])
|
||||||
```
|
```
|
||||||
|
|
||||||
Pytorch的基本语法
|
Pytorch 的基本语法
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
### 加法操作
|
### 加法操作(1)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
y = torch.rand(5, 3)
|
y = torch.rand(5, 3)
|
||||||
@ -64,9 +68,7 @@ tensor([[ 1.6978, -1.6979, 0.3093],
|
|||||||
[ 2.6784, 0.1209, 1.5542]])
|
[ 2.6784, 0.1209, 1.5542]])
|
||||||
```
|
```
|
||||||
|
|
||||||
第一种加法操作
|
### 加法操作(2)
|
||||||
|
|
||||||
### 加法操作
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
>>> print(torch.add(x, y))
|
>>> print(torch.add(x, y))
|
||||||
@ -77,9 +79,7 @@ tensor([[ 1.6978, -1.6979, 0.3093],
|
|||||||
[ 2.6784, 0.1209, 1.5542]])
|
[ 2.6784, 0.1209, 1.5542]])
|
||||||
```
|
```
|
||||||
|
|
||||||
第二种加法操作
|
### 加法操作(3)
|
||||||
|
|
||||||
### 加法操作
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# 提前设定一个空的张量
|
# 提前设定一个空的张量
|
||||||
@ -94,10 +94,7 @@ tensor([[ 1.6978, -1.6979, 0.3093],
|
|||||||
[ 2.6784, 0.1209, 1.5542]])
|
[ 2.6784, 0.1209, 1.5542]])
|
||||||
```
|
```
|
||||||
|
|
||||||
第三种加法操作
|
### 加法操作(4)
|
||||||
|
|
||||||
|
|
||||||
### 加法操作
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
y.add_(x)
|
y.add_(x)
|
||||||
@ -109,17 +106,16 @@ tensor([[ 1.6978, -1.6979, 0.3093],
|
|||||||
[ 2.6784, 0.1209, 1.5542]])
|
[ 2.6784, 0.1209, 1.5542]])
|
||||||
```
|
```
|
||||||
|
|
||||||
第四种加法操作
|
注意: 所有 `in-place` 的操作函数都有一个下划线的后缀。
|
||||||
注意:所有in-place的操作函数都有一个下划线的后缀.
|
比如 `x.copy_(y)`, `x.add_(y)`, 都会直接改变x的值
|
||||||
比如x.copy_(y), x.add_(y), 都会直接改变x的值.
|
|
||||||
|
|
||||||
### 张量操作
|
### 张量操作
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
||||||
>>> print(x[:, 1])
|
>>> print(x[:, 1])
|
||||||
tensor([-2.0902, -0.4489, -0.1441, 0.8035, -0.8341])
|
tensor([-2.0902, -0.4489, -0.1441, 0.8035, -0.8341])
|
||||||
```
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
### 张量形状
|
### 张量形状
|
||||||
|
|
||||||
@ -132,6 +128,7 @@ z = x.view(-1, 8)
|
|||||||
>>> print(x.size(), y.size(), z.size())
|
>>> print(x.size(), y.size(), z.size())
|
||||||
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
|
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
|
||||||
```
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
### 取张量元素
|
### 取张量元素
|
||||||
|
|
||||||
@ -143,8 +140,7 @@ tensor([-0.3531])
|
|||||||
-0.3530771732330322
|
-0.3530771732330322
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Torch Tensor 和 Numpy array互换
|
||||||
### Torch Tensor和Numpy array互换
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
a = torch.ones(5)
|
a = torch.ones(5)
|
||||||
@ -154,7 +150,7 @@ tensor([1., 1., 1., 1., 1.])
|
|||||||
|
|
||||||
Torch Tensor和Numpy array共享底层的内存空间, 因此改变其中一个的值, 另一个也会随之被改变
|
Torch Tensor和Numpy array共享底层的内存空间, 因此改变其中一个的值, 另一个也会随之被改变
|
||||||
|
|
||||||
### Torch Tensor转换为Numpy array
|
### Torch Tensor 转换为 Numpy array
|
||||||
|
|
||||||
```python
|
```python
|
||||||
b = a.numpy()
|
b = a.numpy()
|
||||||
@ -162,7 +158,7 @@ b = a.numpy()
|
|||||||
[1. 1. 1. 1. 1.]
|
[1. 1. 1. 1. 1.]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Numpy array转换为Torch Tensor:
|
### Numpy array转换为Torch Tensor
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -174,4 +170,94 @@ np.add(a, 1, out=a)
|
|||||||
[2. 2. 2. 2. 2.]
|
[2. 2. 2. 2. 2.]
|
||||||
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
|
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
|
||||||
```
|
```
|
||||||
注意:所有在CPU上的Tensors, 除了CharTensor, 都可以转换为Numpy array并可以反向转换.
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
注意: 所有在CPU上的Tensors, 除了CharTensor, 都可以转换为Numpy array并可以反向转换.
|
||||||
|
|
||||||
|
导入 Imports
|
||||||
|
---
|
||||||
|
|
||||||
|
### 一般
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 根包
|
||||||
|
import torch
|
||||||
|
# 数据集表示和加载
|
||||||
|
from torch.utils.data import Dataset, DataLoader
|
||||||
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### 神经网络 API
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 计算图
|
||||||
|
import torch.autograd as autograd
|
||||||
|
# 计算图中的张量节点
|
||||||
|
from torch import Tensor
|
||||||
|
# 神经网络
|
||||||
|
import torch.nn as nn
|
||||||
|
# 层、激活等
|
||||||
|
import torch.nn.functional as F
|
||||||
|
# 优化器,例如 梯度下降、ADAM等
|
||||||
|
import torch.optim as optim
|
||||||
|
# 混合前端装饰器和跟踪 jit
|
||||||
|
from torch.jit import script, trace
|
||||||
|
```
|
||||||
|
|
||||||
|
### Torchscript 和 JIT
|
||||||
|
|
||||||
|
```python
|
||||||
|
torch.jit.trace()
|
||||||
|
```
|
||||||
|
|
||||||
|
使用你的模块或函数和一个例子,数据输入,并追溯计算步骤,数据在模型中前进时遇到的情况
|
||||||
|
|
||||||
|
```python
|
||||||
|
@script
|
||||||
|
```
|
||||||
|
|
||||||
|
装饰器用于指示被跟踪代码中的数据相关控制流
|
||||||
|
|
||||||
|
### ONNX
|
||||||
|
|
||||||
|
```python
|
||||||
|
torch.onnx.export(model, dummy data, xxxx.proto)
|
||||||
|
# 导出 ONNX 格式
|
||||||
|
# 使用经过训练的模型模型,dummy
|
||||||
|
# 数据和所需的文件名
|
||||||
|
|
||||||
|
model = onnx.load("alexnet.proto")
|
||||||
|
# 加载 ONNX 模型
|
||||||
|
onnx.checker.check_model(model)
|
||||||
|
# 检查模型,IT 是否结构良好
|
||||||
|
|
||||||
|
onnx.helper.printable_graph(model.graph)
|
||||||
|
# 打印一个人类可读的,图的表示
|
||||||
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### Vision
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 视觉数据集,架构 & 变换
|
||||||
|
from torchvision import datasets, models, transforms
|
||||||
|
# 组合转换
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### 分布式训练
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 分布式通信
|
||||||
|
import torch.distributed as dist
|
||||||
|
# 内存共享进程
|
||||||
|
from torch.multiprocessing import Process
|
||||||
|
```
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
另见
|
||||||
|
---
|
||||||
|
|
||||||
|
- [Pytorch 官网](https://pytorch.org/) _(pytorch.org)_
|
||||||
|
- [Pytorch 官方备忘清单](https://pytorch.org/tutorials/beginner/ptcheat.html) _(pytorch.org)_
|
||||||
|
3
scripts/assets/pytorch.svg
Normal file
3
scripts/assets/pytorch.svg
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
|
||||||
|
<path d="M12.005 0 4.952 7.053a9.865 9.865 0 0 0 0 14.022 9.866 9.866 0 0 0 14.022 0c3.984-3.9 3.986-10.205.085-14.023l-1.744 1.743c2.904 2.905 2.904 7.634 0 10.538s-7.634 2.904-10.538 0-2.904-7.634 0-10.538l4.647-4.646.582-.665zm3.568 3.899a1.327 1.327 0 0 0-1.327 1.327 1.327 1.327 0 0 0 1.327 1.328A1.327 1.327 0 0 0 16.9 5.226 1.327 1.327 0 0 0 15.573 3.9z"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 479 B |
Loading…
x
Reference in New Issue
Block a user