Files
ailearning/docs/pytorch/05.md
2020-10-19 22:31:47 +08:00

74 lines
2.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 2.1 Torch vs Numpy
## 用 Numpy 还是 Torch
Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 array 放在 CPU 中加速运算. 所以神经网络的话, 当然是用 Torch 的 tensor 形式数据最好咯. 就像 Tensorflow 当中的 tensor 一样.
当然, 我们对 Numpy 还是爱不释手的, 因为我们太习惯 numpy 的形式了. 不过 torch 看出来我们的喜爱, 他把 torch 做的和 numpy 能很好的兼容. 比如这样就能自由地转换 numpy array 和 torch tensor 了:
```py
import torch
import numpy as np
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data)
tensor2array = torch_data.numpy()
print(
\'\nnumpy array:\', np_data, # [[0 1 2], [3 4 5]]
\'\ntorch tensor:\', torch_data, # 0 1 2 \n 3 4 5 [torch.LongTensor of size 2x3]
\'\ntensor to array:\', tensor2array, # [[0 1 2], [3 4 5]]
)
```
## Torch 中的数学运算
其实 torch 中 tensor 的运算和 numpy array 的如出一辙, 我们就以对比的形式来看. 如果想了解 torch 中其它更多有用的运算符, [API就是你要去的地方](http://pytorch.org/docs/torch.html#math-operations).
```py
# abs 绝对值计算
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
print(
\'\nabs\',
\'\nnumpy: \', np.abs(data), # [1 2 1 2]
\'\ntorch: \', torch.abs(tensor) # [1 2 1 2]
)
# sin 三角函数 sin
print(
\'\nsin\',
\'\nnumpy: \', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
\'\ntorch: \', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]
)
# mean 均值
print(
\'\nmean\',
\'\nnumpy: \', np.mean(data), # 0.0
\'\ntorch: \', torch.mean(tensor) # 0.0
)
```
除了简单的计算, 矩阵运算才是神经网络中最重要的部分. 所以我们展示下矩阵的乘法. 注意一下包含了一个 numpy 中可行, 但是 torch 中不可行的方式.
```py
# matrix multiplication 矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
# correct method
print(
\'\nmatrix multiplication (matmul)\',
\'\nnumpy: \', np.matmul(data, data), # [[7, 10], [15, 22]]
\'\ntorch: \', torch.mm(tensor, tensor) # [[7, 10], [15, 22]]
)
# !!!! 下面是错误的方法 !!!!
data = np.array(data)
print(
\'\nmatrix multiplication (dot)\',
\'\nnumpy: \', data.dot(data), # [[7, 10], [15, 22]] 在numpy 中可行
\'\ntorch: \', tensor.dot(tensor) # torch 会转换成 [1,2,3,4].dot([1,2,3,4) = 30.0
)
```
文章来源:[莫烦](https://morvanzhou.github.io/)