PyTorch是什么?

目录


一、PyTorch是什么

它是一个基于Python的科学计算包,有以下两个目标:

  • 作为NumPy的替代者,充分发挥GPU的能力
  • 一个深度学习研究平台,提供最大的灵活性和速度

二、入门

1、Tensor

Tensor 类似于 NumPy 中的 ndarray,除此之外,tensor 能够运行在 GPU上以加速计算。

创建tensor

  1. 创建一个5x3的未初始化的矩阵:
x = torch.empty(5,3)
print(x)

输出:


tensor([[ 1.8918e-16,  4.5644e-41, -3.0675e-26],
        [ 3.0773e-41,  0.0000e+00,  1.4013e-45],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00]])
  1. 创建一个随机初始化的矩阵
x = torch.rand(5,3)
print(x)

输出

tensor([[0.5524, 0.8042, 0.8140],
        [0.2944, 0.1345, 0.2921],
        [0.3099, 0.3343, 0.7377],
        [0.0685, 0.1461, 0.9683],
        [0.8908, 0.4492, 0.9506]])
  1. 创建一个零矩阵,类型为long
x = torch.zeros(5,3, dtype=torch.long)
print(x)

输出

tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
  1. 从数据创建
x = torch.tensor([5.5,3])
print(x)

输出:

tensor([5.5000, 3.0000])
  1. 基于存在的tensor创建tensor,这些方法会使用输入tensor的属性,比如dtype,也可以自行指定。
x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float)    # override dtype!
print(x)                                      # result has the same size

输出:

tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[-1.9325,  1.2030, -2.1360],
        [-0.1392,  1.2079,  1.3194],
        [-1.0496,  0.9343,  2.4428],
        [-0.3567, -0.5195, -0.0276],
        [ 1.8236,  0.1193, -0.5549]])

生成的 tensor 的大小:

print(x.size())

输出:

torch.Size([5, 3])

注意

torch.Size是一个元组,支持所有的元组操作。

2、 运算

对于运算有许多的语法,接下来,以加法运算为例

  1. 语法1
y = torch.rand(5,3)
print(x + y)

输出:

tensor([[-1.8346,  1.6518, -1.2673],
        [ 0.1698,  1.8538,  2.2153],
        [-0.0803,  0.9863,  3.2740],
        [-0.0906, -0.3752,  0.8960],
        [ 1.8271,  0.8586, -0.0483]])
  1. 语法2
print(torch.add(x, y))

输出:

tensor([[-1.8346,  1.6518, -1.2673],
        [ 0.1698,  1.8538,  2.2153],
        [-0.0803,  0.9863,  3.2740],
        [-0.0906, -0.3752,  0.8960],
        [ 1.8271,  0.8586, -0.0483]])
  1. 提供输出tensor作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

输出:

tensor([[-1.8346,  1.6518, -1.2673],
        [ 0.1698,  1.8538,  2.2153],
        [-0.0803,  0.9863,  3.2740],
        [-0.0906, -0.3752,  0.8960],
        [ 1.8271,  0.8586, -0.0483]])
  1. 原地操作
y.add_(x)         # adds x to y
print(y)

输出:

tensor([[-1.8346,  1.6518, -1.2673],
        [ 0.1698,  1.8538,  2.2153],
        [-0.0803,  0.9863,  3.2740],
        [-0.0906, -0.3752,  0.8960],
        [ 1.8271,  0.8586, -0.0483]])

注意

任何原地改变tensor的操作以下划线(_)作为后缀,例如:x.copy_(y),x.t_()

  1. 可以使用标准的 NumPy 索引操作
print(x[:, 1])

输出:

tensor([ 1.2030,  1.2079,  0.9343, -0.5195,  0.1193])
  1. 使用torch.view改变tensor的大小和形状
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)        # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

输出:

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
  1. 对于只包含一个元素的tensor,使用.item()将值作为Python数字
x = torch.randn(1)
print(x)
print(x.item())

输出:

tensor([-0.7521])
-0.7521063685417175

三、Torch tensor & NumPy array

Torch tensorNumpy array 之间的转换非常简单。

Torch tensorNumpy array 将共享底层的内存位置(如果 torch tensorCPU 上),改变其中的一个,另一个也会改变。

  1. torch tensor 转换为 NumPy array
a = torch.ones(5)
print(a)

输出:

tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)

输出:

[1. 1. 1. 1. 1.]

看一看 NumPy array 的值如何改变

a.add_(1)
print(a)
print(b)

输出:

tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
  1. NumPy array 转化为 torch tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

输出:

[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

除了 CharTensor 外,CPU 上的所有tensorNumPy array 均可相互转化。

四、CUDA tensors

使用 .to 方法可以将 tensor 移动到任何设备上。

# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!

输出:

tensor([0.2479], device='cuda:0')
tensor([0.2479], dtype=torch.float64)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值