Pytorch学习一——pytorch介绍、张量和Variable

11.Pytorch介绍

PyTorch 是由 Torch7 团队开源的,这也是Facebook 的 AI 研究团队发布了一个 Python 工具包,据该项目官网介绍,是一个 Python 优先的深度学习框架,能够在强大的 GPU 加速基础上实现张量和动态神经网络。

- [官网](http://pytorch.org/)
- [Github](https://github.com/pytorch/pytorch)

目前除了 Facebook 之外,也有大量的机构正在使用 PyTorch

![](https://ws2.sinaimg.cn/large/006tNc79ly1fmebl3ayfij30kk0c2aac.jpg)

 PyTorch 的前身是 Torch,其是一个十分老牌、对多维矩阵数据进行操作的张量(tensor )库,在机器学习和其他数学密集型应用有广泛应用,但由于其语言采用 Lua,导致在国内一直很小众,如今使用 Python 语言强势归来,快速的赢得了大量使用者。

PyTorch 提供了两种高层面的功能:
- 使用强大的 GPU 加速的 Tensor 计算(类似 numpy)
- 构建于基于 autograd 系统的深度神经网络

所以使用 PyTorch 的原因通常有两个:
- 作为 numpy 的替代,以便使用强大的 GPU 加速;
- 将其作为一个能提供最大灵活性和速度的深度学习研究平台

 2.Tensor和Variable

Tensor

(1)把 PyTorch 当做 NumPy 用

PyTorch 的官方介绍是一个拥有强力GPU加速的张量和动态构建网络的库,其主要构件是张量,所以我们可以把 PyTorch 当做 NumPy 来用,PyTorch 的很多操作好 NumPy 都是类似的,但是因为其能够在 GPU 上运行,所以有着比 NumPy 快很多倍的速度。

import torch
import numpy as np
# 创建一个 numpy ndarray
numpy_tensor = np.random.randn(10, 20)
print(numpy_tensor.shape)

(2)将numpy的ndarray转换到tensor上

pytorch_tensor1 = torch.Tensor(numpy_tensor)
pytorch_tensor2 = torch.from_numpy(numpy_tensor)

 使用以上两种方法进行转换的时候,会直接将 NumPy ndarray 的数据类型转换为对应的 PyTorch Tensor 数据类型.

(3)将 pytorch tensor 转换为 numpy ndarray

# 如果 pytorch tensor 在 cpu 上
numpy_array = pytorch_tensor1.numpy()

# 如果 pytorch tensor 在 gpu 上
numpy_array = pytorch_tensor1.cpu().numpy()

需要注意 GPU 上的 Tensor 不能直接转换为 NumPy ndarray,需要使用.cpu()先将 GPU 上的 Tensor 转到 CPU 上

PyTorch Tensor 使用 GPU 加速

(4)使用以下两种方式将 Tensor 放到 GPU 上

# 第一种方式是定义 cuda 数据类型
dtype = torch.cuda.FloatTensor # 定义默认 GPU 的 数据类型
gpu_tensor = torch.randn(10, 20).type(dtype)

# 第二种方式更简单,推荐使用
gpu_tensor = torch.randn(10, 20).cuda(0) # 将 tensor 放到第一个 GPU 上
gpu_tensor = torch.randn(10, 20).cuda(1) # 将 tensor 放到第二个 GPU 上

使用第一种方式将 tensor 放到 GPU 上的时候会将数据类型转换成定义的类型,而是用第二种方式能够直接将 tensor 放到 GPU 上,类型跟之前保持一致

推荐在定义 tensor 的时候就明确数据类型,然后直接使用第二种方法将 tensor 放到 GPU 上

(5)将tensor放回到CPU

而将 tensor 放回 CPU 的操作非常简单

cpu_tensor = gpu_tensor.cpu()

(6)也能够访问到 Tensor 的一些属性

# 可以通过下面两种方式得到 tensor 的大小
print(pytorch_tensor1.shape)
print(pytorch_tensor1.size())

   得到 tensor 的数据类型 

print(pytorch_tensor1.type())

得到 tensor 的维度

print(pytorch_tensor1.dim())

# 得到 tensor 的所有元素个数

print(pytorch_tensor1.numel())

 

小练习

查阅以下文档了解 tensor 的数据类型,创建一个 float64、大小是 3 x 2、随机初始化的 tensor,将其转化为 numpy 的 ndarray,输出其数据类型

参考输出: float64

# 答案
x = torch.randn(3, 2)
x = x.type(torch.DoubleTensor)
x_array = x.numpy()
print(x_array.dtype)

Variable

每个 Variabel都有三个属性,Variable 中的 tensor本身.data,对应 tensor 的梯度.grad以及这个 Variable 是通过什么方式得到的.grad_fn。Variable 是对 tensor 的封装,操作和 tensor 是一样的

# 通过下面这种方式导入 Variable
from torch.autograd import Variable
x_tensor = torch.randn(10, 5)
y_tensor = torch.randn(10, 5)
print(x_tensor)
print(y_tensor)
# 将 tensor 变成 Variable
x = Variable(x_tensor, requires_grad=True) # 默认 Variable 是不需要求梯度的,所以我们用这个方式申明需要对其进行求梯度
y = Variable(y_tensor, requires_grad=True)
print(x)
print(y)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值