Pytorch学习教程(一)----pytorch是什么?

一、PyTorch 是什么

他是一个基于Python的科学计算包,目标用户有两类

  • 为了使用GPU来替代numpy
  • 一个深度学习援救平台:提供最大的灵活性和速度

开始

张量(Tensors)

张量类似于numpy的ndarrays,不同之处在于张量可以使用GPU来加快计算。

from __future__ import print_function
import torch

构建一个未初始化的5*3的矩阵:

x = torch.Tensor(5, 3)
print(x)
tensor([[1.7753e+28, 7.0744e+31, 1.1285e+27],
        [2.7556e+23, 2.8926e+12, 7.5338e+28],
        [7.6286e-19, 7.1758e+22, 1.4603e-19],
        [1.8888e+31, 4.9656e+28, 4.5439e+30],
        [1.6907e-01, 1.7743e+28, 1.3458e-14]])

构建一个零矩阵,使用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]])

从数据中直接构建一个张量(tensor):

x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])

或者在已有的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)    # 覆盖类型!
print(x)                                      # result 的size相同
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[ 1.1052,  1.4523, -1.1006],
        [-1.9788, -2.0765, -0.2835],
        [ 0.3691, -0.5328,  2.5717],
        [-0.6540,  1.5084, -0.6631],
        [-0.0236,  1.8366,  0.0184]])

获取张量(tensor)的大小

print(x.size())
torch.Size([5, 3])
  • 注意

torch.Size实际上是一个元组,所以它支持元组的所有操作。

操作

张量上的操作有多重语法形式,下面我们以加法为例进行讲解。

语法1

y = torch.rand(5, 3)
print(x + y)
tensor([[ 1.2351,  2.0633, -0.1270],
        [-1.5097, -1.2631,  0.4004],
        [ 0.6075,  0.1822,  3.3232],
        [-0.3175,  2.1033, -0.2929],
        [ 0.2614,  2.1303,  0.2800]])

语法二

print(torch.add(x, y))
tensor([[ 1.2351,  2.0633, -0.1270],
        [-1.5097, -1.2631,  0.4004],
        [ 0.6075,  0.1822,  3.3232],
        [-0.3175,  2.1033, -0.2929],
        [ 0.2614,  2.1303,  0.2800]])

语法三:

给出一个输出张量作为参数

result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[ 1.2351,  2.0633, -0.1270],
        [-1.5097, -1.2631,  0.4004],
        [ 0.6075,  0.1822,  3.3232],
        [-0.3175,  2.1033, -0.2929],
        [ 0.2614,  2.1303,  0.2800]])

语法四:

原地操作(in-place)

# 把x加到y上
y.add_(x)
print(y)
tensor([[ 1.2351,  2.0633, -0.1270],
        [-1.5097, -1.2631,  0.4004],
        [ 0.6075,  0.1822,  3.3232],
        [-0.3175,  2.1033, -0.2929],
        [ 0.2614,  2.1303,  0.2800]])
  • 注意

任何在原地(in-place)改变张量的操作都有一个_后缀。例如x.copy_(y), x.t_()操作将改变x.

你可以使用所有的numpy索引操作。

你可以使用各种类似标准NumPy的花哨的索引功能

print(x[:, 1])
tensor([ 0.3058, -0.4359, -1.5352, -0.5244, -0.0097])

调整大小:如果要调整张量/重塑张量,可以使用torch.view:

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # -1的意思是没有指定维度
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

如果你有一个单元素张量,使用.item()将值作为Python数字

x = torch.randn(1)
print(x)
print(x.item())
tensor([-1.6318])
-1.6317875385284424

稍后阅读

这里描述了一百多种张量操作,包括转置,索引,数学运算,线性代数,随机数等。

numpy桥

把一个torch张量转换为numpy数组或者反过来都是很简单的。

Torch张量和numpy数组将共享潜在的内存,改变其中一个也将改变另一个。

把Torch张量转换为numpy数组

a = torch.ones(5)
print(a)
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
print(type(b))
[ 1.  1.  1.  1.  1.]
<class 'numpy.ndarray'>

通过如下操作,我们看一下numpy数组的值如何在改变。

a.add_(1)
print(a)
print(b)
tensor([2., 2., 2., 2., 2.])
[ 2.  2.  2.  2.  2.]

把numpy数组转换为torch张量

看看改变numpy数组如何自动改变torch张量。

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)

所有在CPU上的张量,除了字符张量,都支持在numpy之间转换。

CUDA张量

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

# 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.6318], device='cuda:0')
tensor([-0.6318], dtype=torch.float64)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值