From 29df8a3f721cfaf9576048beec3eb577a4ebc7af Mon Sep 17 00:00:00 2001
From: Zyj <51304324+y52y@users.noreply.github.com>
Date: Fri, 18 Nov 2022 16:44:58 +0800
Subject: [PATCH] feat: add pytorch.md (#138)
---
README.md | 28 ++------
docs/pytorch.md | 177 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 183 insertions(+), 22 deletions(-)
create mode 100644 docs/pytorch.md
diff --git a/README.md b/README.md
index 0ce8c02..92f951e 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,8 @@ Quick Reference
[FFmpeg](./docs/ffmpeg.md)
[LaTeX](./docs/latex.md)
[MATLAB](./docs/matlab.md)
-[Vue 3](./docs/vue.md)
+[Vue 3 ](./docs/vue.md)
+[Pytorch](./docs/pytorch.md)
## 编程
@@ -60,6 +61,7 @@ Quick Reference
[TOML](./docs/toml.md)
[YAML](./docs/yaml.md)
[Lua](./docs/lua.md)
+[Pytorch](./docs/pytorch.md)
## 前端
@@ -173,15 +175,9 @@ Quick Reference
-
-
-
-
-
-
@@ -191,18 +187,15 @@ Quick Reference
+
+
+
-
-
-
-
-
-
@@ -212,9 +205,6 @@ Quick Reference
-
-
-
@@ -242,9 +232,6 @@ Quick Reference
-
-
-
@@ -254,9 +241,6 @@ Quick Reference
-
-
-
diff --git a/docs/pytorch.md b/docs/pytorch.md
new file mode 100644
index 0000000..ef67929
--- /dev/null
+++ b/docs/pytorch.md
@@ -0,0 +1,177 @@
+Pytorch 备忘清单
+===
+
+Pytorch 备忘单是 [Pytorch ](https://pytorch.org/) 官网
+
+入门
+-----
+
+### 介绍
+
+- [Pytorch基本语法]
+- [Pytorch初步应用]
+
+### 认识Pytorch
+
+```python
+from __future__ import print_function
+import torch
+x = torch.empty(5, 3)
+>>> print(x)
+tensor([[2.4835e+27, 2.5428e+30, 1.0877e-19],
+ [1.5163e+23, 2.2012e+12, 3.7899e+22],
+ [5.2480e+05, 1.0175e+31, 9.7056e+24],
+ [1.6283e+32, 3.7913e+22, 3.9653e+28],
+ [1.0876e-19, 6.2027e+26, 2.3685e+21]])
+```
+
+Tensors张量: 张量的概念类似于Numpy中的ndarray数据结构, 最大的区别在于Tensor可以利用GPU的加速功能.
+
+### 创建一个全零矩阵
+
+```python
+x = torch.zeros(5, 3, dtype=torch.long)
+>>> print(x)
+tensor([[0, 0, 0],
+ [0, 0, 0],
+ [0, 0, 0],
+ [0, 0, 0],
+ [0, 0, 0]])
+```
+
+创建一个全零矩阵并可指定数据元素的类型为long
+
+### 数据创建张量
+
+```python
+x = torch.tensor([2.5, 3.5])
+>>> print(x)
+tensor([2.5000, 3.3000])
+```
+
+Pytorch的基本语法
+---------------
+
+### 加法操作
+
+```python
+y = torch.rand(5, 3)
+>>> print(x + y)
+tensor([[ 1.6978, -1.6979, 0.3093],
+ [ 0.4953, 0.3954, 0.0595],
+ [-0.9540, 0.3353, 0.1251],
+ [ 0.6883, 0.9775, 1.1764],
+ [ 2.6784, 0.1209, 1.5542]])
+```
+
+第一种加法操作
+
+### 加法操作
+
+```python
+>>> print(torch.add(x, y))
+tensor([[ 1.6978, -1.6979, 0.3093],
+ [ 0.4953, 0.3954, 0.0595],
+ [-0.9540, 0.3353, 0.1251],
+ [ 0.6883, 0.9775, 1.1764],
+ [ 2.6784, 0.1209, 1.5542]])
+```
+
+第二种加法操作
+
+### 加法操作
+
+```python
+# 提前设定一个空的张量
+result = torch.empty(5, 3)
+# 将空的张量作为加法的结果存储张量
+ torch.add(x, y, out=result)
+>>> print(result)
+tensor([[ 1.6978, -1.6979, 0.3093],
+ [ 0.4953, 0.3954, 0.0595],
+ [-0.9540, 0.3353, 0.1251],
+ [ 0.6883, 0.9775, 1.1764],
+ [ 2.6784, 0.1209, 1.5542]])
+```
+
+第三种加法操作
+
+
+### 加法操作
+
+```python
+y.add_(x)
+>>> print(y)
+tensor([[ 1.6978, -1.6979, 0.3093],
+ [ 0.4953, 0.3954, 0.0595],
+ [-0.9540, 0.3353, 0.1251],
+ [ 0.6883, 0.9775, 1.1764],
+ [ 2.6784, 0.1209, 1.5542]])
+```
+
+第四种加法操作
+注意:所有in-place的操作函数都有一个下划线的后缀.
+比如x.copy_(y), x.add_(y), 都会直接改变x的值.
+
+### 张量操作
+
+```python
+
+>>> print(x[:, 1])
+tensor([-2.0902, -0.4489, -0.1441, 0.8035, -0.8341])
+```
+
+### 张量形状
+
+```python
+x = torch.randn(4, 4)
+# tensor.view()操作需要保证数据元素的总数量不变
+y = x.view(16)
+# -1代表自动匹配个数
+z = x.view(-1, 8)
+>>> print(x.size(), y.size(), z.size())
+torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
+```
+
+### 取张量元素
+
+```python
+x = torch.randn(1)
+>>> print(x)
+>>> print(x.item())
+tensor([-0.3531])
+-0.3530771732330322
+```
+
+
+### Torch Tensor和Numpy array互换
+
+```python
+a = torch.ones(5)
+>>> print(a)
+tensor([1., 1., 1., 1., 1.])
+```
+
+Torch Tensor和Numpy array共享底层的内存空间, 因此改变其中一个的值, 另一个也会随之被改变
+
+### Torch Tensor转换为Numpy array
+
+```python
+b = a.numpy()
+>>> print(b)
+[1. 1. 1. 1. 1.]
+```
+
+### Numpy array转换为Torch Tensor:
+
+```python
+import numpy as np
+a = np.ones(5)
+b = torch.from_numpy(a)
+np.add(a, 1, out=a)
+>>> print(a)
+>>> print(b)
+[2. 2. 2. 2. 2.]
+tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
+```
+注意:所有在CPU上的Tensors, 除了CharTensor, 都可以转换为Numpy array并可以反向转换.