PyTorch简介

PyTorch 是 Torch 在 Python 上的衍生. 因为 Torch 是一个使用 Lua 语言的神经网络库,由于 PyTorch 采用了动态计算图(dynamic computational graph)结构,PyTorch 有一种独特的神经网络构建方法:使用和重放 tape recorder。而不是大多数开源框架,比如 TensorFlow、Caffe、CNTK、Theano 等采用的静态计算图。 使用 PyTorch,通过一种我们称之为「Reverse-mode auto-differentiation(反向模式自动微分)」的技术,你可以零延迟或零成本地任意改变你的网络的行为。

torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 array 放在 CPU 中加速运算

进一步说,torch是一个支持 GPU 的 Tensor 库,如果你使用 numpy,那么你就使用过 Tensor(即 ndarray)。PyTorch 提供了支持 CPU 和 GPU 的 Tensor.

PyTorch组成

PyTorch由4个主要包装组成:

1.torch:类似于Numpy的通用数组库,可以在将张量类型转换为(torch.cuda.TensorFloat)并在GPU上进行计算。

2.torch.autograd:用于构建计算图形并自动获取渐变的包

3.torch.nn:具有共同层和成本函数的神经网络库

4.torch.optim:具有通用优化算法(如SGD,Adam等)的优化包

PyTorch简单使用

torch 做的和 numpy 有很好的兼容. 这样就能自由地转换numpy array 和 torch tensor

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import torch
import numpy as np

np_data = np.arange(6).reshape((2, 3))
#将numpy转换为torch tensor
torch_data = torch.from_numpy(np_data)
#将torch tensor转换为tensor
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做数学运算

#计算绝对值
data = [-1, -2, 1, 2]
#转换成32位浮点tensor
tensor = torch.FloatTensor(data)
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
)

torch做矩阵运算

#矩阵运算
#矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data)  # 转换成32位浮点 tensor
print(
    '\nmatrix multiplication (matmul)',
    '\nnumpy: ', np.matmul(data, data),     # [[7, 10], [15, 22]]
    '\ntorch: ', torch.mm(tensor, tensor)   # [[7, 10], [15, 22]]
)

torch中的变量

先定义一个变量

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import torch
from torch.autograd import Variable

tensor = torch.FloatTensor([[1, 2],[3, 4]])
#在BP的时候,pytorch是将Variable的梯度放在Variable对象中的,
# 我们随时都可以使用Variable.grad得到对应Variable的grad。
# 刚创建Variable的时候,它的grad属性是初始化为0.0的。
#需要求导的话,requires_grad=True属性是必须的。
variable = Variable(tensor, requires_grad = True)
print tensor
"""
output:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""
#多了一个Variable containing:
print variable
"""
output:
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""

Variable 计算时, 会一步步搭建着一个庞大的系统, 叫做计算图, computational graph. 这个图是将所有的计算步骤 (节点) 都连接起来, 最后进行误差反向传递的时候, 一次性将所有 variable 里面的修改幅度 (梯度) 都计算出来

#模拟v_out 反向误差
# v_out = 1/4 * sum(variable*variable) 这是计算图中的 v_out 计算步骤
# 针对于 v_out 的梯度就是, d(v_out)/d(variable) = 1/4*2*variable = variable/2
v_out.backward()

print variable.grad

#获取variable的数据
print variable
"""
Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""
print variable.data
"""
 1  2
 3  4
[torch.FloatTensor of size 2x2]
"""
print variable.data.numpy()
"""
[[ 1.  2.]
 [ 3.  4.]]
 """