# handmail/pytorch

2 years ago

PyTorch进阶之路（一）：张量与梯度

http://www.heijing.co/almosthuman2014/2019031212102495499

PyTorch 是 Facebook 开发和维护的一个开源的神经网络库

PyTorch 模型的基本构件：张量和梯度

In [1]:
``import torch``
In [10]:
``````#Number
t1 = torch.tensor(4.0)
t1
``````
Out[10]:
``tensor(4.)``
In [11]:
``t1.dtype``
Out[11]:
``torch.float32``
In [12]:
``````#Vector
t2 = torch.tensor([1.0,2,3,4])
t2
``````
Out[12]:
``tensor([1., 2., 3., 4.])``
In [15]:
``````#Matrix
t3 = torch.tensor([[5,6], [7,8], [9,10]])
t3
``````
Out[15]:
``````tensor([[ 5,  6],
[ 7,  8],
[ 9, 10]])``````
In [22]:
``````#3D array
t4 = torch.tensor([[[1,2,3],[4,5,6]], [[1,2,3],[4,5,6]]])``````
In [23]:
``t1.shape``
Out[23]:
``torch.Size([])``
In [24]:
``t2.shape``
Out[24]:
``torch.Size([4])``
In [25]:
``t3.shape``
Out[25]:
``torch.Size([3, 2])``
In [26]:
``t4.shape``
Out[26]:
``torch.Size([2, 2, 3])``

In [46]:
``````# create tensors.
x = torch.tensor(3.)
w = torch.tensor(4., requires_grad = True)
b = torch.tensor(5., requires_grad = True)``````

In [47]:
``````#Arithmetic operations 运算操作
y = x * w + b
y
``````
Out[47]:
``tensor(17., grad_fn=<AddBackward0>)``

y 是值为 3 * 4 + 5 = 17 的张量。PyTorch 的特殊之处在于，我们可以自动计算 y 相对于张量（requires_grad 设置为 True）的导数，即 w 和 b。为了计算导数，我们可以在结果 y 上调用.backward 方法。

In [48]:
``````#Compute derivatives 计算派生
y.backward( )
``````
In [55]:
``````print("dy/dx:", x.grad)
```dy/dx: None dy/dw tensor(3.) dy/db tensor(1.) ```

###### PyTorch 并没有重新创造 wheel，而是与 Numpy 很好地交互，以利用它现有的工具和库生态系统。
In [57]:
``import numpy as np``
In [58]:
``x = np.array([[1,2],[3,4]])``
In [62]:
``````y = torch.from_numpy(x) #  torch.fron_numpy 将 Numpy 数组转化为 PyTorch 张量
y
``````
Out[62]:
``````tensor([[1, 2],
[3, 4]])``````
In [64]:
``````# 接下来可以验证 Numpy 数组和 PyTorch 张量是否拥有类似的数据类型
print(x.dtype)
print(y.dtype)
``````
```int64 torch.int64 ```
In [66]:
``````#使用张量的.to_numpy 方法将 PyTorch 张量转化为 Numpy 数组
z = y.numpy()
z
``````
Out[66]:
``````array([[1, 2],
[3, 4]])``````
###### PyTorch 和 Numpy 之间的互操作性真的非常重要，因为你要用的大部分数据集都可能被读取并预处理为 Numpy 数组
In [1]:
``import jovian #[ˈdʒəʊvɪən] 木星``
In [ ]:
``jovian.commit()``
```[jovian] Saving notebook.. ```
In [ ]:
`` ``