pytorch入门教程

pytorch与tensorflow区别

https://yq.aliyun.com/articles/183473

pytorch安装

进入官网:https://pytorch.org/
根据配置可以生成相应的安装命令,运行之后,会安装torchtorchvision这两个库。 torch 是主模块, 用来搭建神经网络; torchvision 是辅模块, 有数据库, 还有一些已经训练好的神经网络可以直接使用 , 比如 (VGG, AlexNet, ResNet)

Tensor的创建和操作

参考:http://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/
pytorch与numpy
pytorch相当于神经网络中的numpy库,pytorch与numpy具有很好的兼容性,pytorch中的tensor与numpy中的array能自由转换。

numpy_data = np.arange(4).reshape(2, 2)
torch_data = torch.from_numpy(numpy_data)
tensor2array = torch_data.numpy()

通过torch.Tensor创建张量
张量tensor可以从Python的list或序列构建:

>>> torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.]])

空张量tensor可以通过规定其大小来构建:

>>> torch.IntTensor(2, 4).zero_()
tensor([[ 0,  0,  0,  0],
        [ 0,  0,  0,  0]], dtype=torch.int32)

以用python的索引和切片来获取和修改一个张量tensor中的内容:

>>> x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
>>> x[1][2]
tensor(6.)
>>> print(x[1][2])
tensor(6.)
>>> type(x[1][2])
<class 'torch.Tensor'>
>>> x[0][1]=100
>>> x
tensor([[   1.,  100.,    3.],
        [   4.,    5.,    6.]])

tensor函数操作
改变tensor的函数操作会用一个下划线后缀来标示。比如,torch.FloatTensor.abs_()会在原地计算绝对值,并返回改变后的tensor,而tensor.FloatTensor.abs()将会在一个新的tensor中计算结果。
常用操作:
tensor的加法:

  • torch.add(input, value, out=None)
    对输入张量input逐元素加上标量值value,并返回结果到一个新的张量out
>>> a = torch.randn(4)
>>> a
tensor([ 0.1280, -0.1634, -0.5978,  0.4619])
>>> torch.add(a, 20)
tensor([ 20.1280,  19.8366,  19.4022,  20.4619])
  • torch.add(input, value=1, other, out=None)
    other 张量的每个元素乘以一个标量值value,并加到input 张量上。返回结果到输出张量out
>>> a = torch.randn(2,2)
>>> a
tensor([[-1.5407, -2.6222],
        [-0.4579, -0.9512]])
>>> b = torch.randn(2, 2)
>>> b
tensor([[ 0.1718,  0.4251],
        [-0.6100,  1.3532]])
>>> torch.add(a, 10, b)
tensor([[  0.1777,   1.6285],
        [ -6.5576,  12.5812]])

tensor的乘法

  • torch.mul(input, value, out=None)
    用标量值value乘以输入input的每个元素,并返回一个新的结果张量
>>> a
tensor([-0.4965, -0.1719, -0.9845])
>>> torch.mul(a, 100)
tensor([-49.6475, -17.1865, -98.4483])
  • torch.mul(input, other, out=None)
    两个张量input,other按元素位置进行相乘,并返回到输出张量
>>> a
tensor([[ 1,  2],
        [ 3,  4]], dtype=torch.int32)
>>> b = torch.IntTensor([[1,1],[1,1]])
>>> torch.mul(a, b)
tensor([[ 1,  2],
        [ 3,  4]], dtype=torch.int32)

矩阵相乘:
- torch.mm(mat1, mat2, out=None) → Tensor
对矩阵mat1和mat2进行相乘。 如果mat1 是一个n×m 张量,mat2 是一个 m×p 张量,将会输出一个 n×p 张量out。

>>> torch.mm(a, b)
tensor([[ 3,  3],
        [ 7,  7]], dtype=torch.int32)

变量的定义和误差反向传播

由于autograd只支持标量值的反向求导(即:y是标量),梯度的大小总是和数据的大小匹配。使用requires_grad=True属性来确定用于更新梯度的变量。

import torch

# requires_grad=True表明该张量为变量,需要更新梯度
x = torch.tensor([[1.,2.],[3.,4.]], requires_grad=True)
# 求梯度的函数的输出结果只能是标量
# 否则出现grad can be implicitly created only for scalar outputs的错误
# 例如y=x*x是不对的,因为此时的输出结果为多维的tensor
y = torch.mean(x*x)

y.backward()
print(y)    # tensor(7.5000)

print(x.grad.numpy())  # 输出梯度
'''
[[0.5 1. ]
 [1.5 2. ]]
'''


# 转为numpy格式,
# 包含梯度运算的张量不能直接转为numpy的array
'''将计算图中的tensor转为numpy的方法'''
# 使用data属性访问原始的tensor,返回的tensor依旧指向x
print(x.data.numpy())

# 会用detach方法从计算图中分离出一个新的张量
print(x.detach().numpy())
'''
[[ 1.  2.]
 [ 3.  4.]]
'''

你可能感兴趣的:(深度学习与pytorch)