PyTorch面试精华


1 PyTorch全局设置

    1.1  全局设置当前设备

    1.2  全局设置浮点精度

    1.3  设置控制台输出格式

2 向量与梯度之核心

    2.1  Tensor的组成与存储

    2.2  Tensor的grad属性

    2.3  Tensor内存布局的调整

    2.4  Tensor的叠加

    2.5  禁用梯度计算

    2.6  向量的保存和加载

    2.7  梯度消失与梯度爆炸

3 神经网络基础

    3.1  线性网络的参数以及初始化

    3.2  PyTorch计算图

    3.3  查看网络权重参数

    3.4  保存模型

    3.5  Adam相关面试题

    3.6  ReLu与非线性的理解

    3.7  Train模式和Eval模式

    3.8  线性网络

    3.9  双线性网络

    3.10  惰性线性层

    3.11  参数向量

    3.12  叶子节点

    3.13  自动求导与链式求导

    3.14  Dropout机制

    3.15  detach原理

    3.16  半精度训练

    3.17  Xavier初始化

    3.18  通道的深刻理解

    3.19  1x1卷积的作用

    3.20  注意力机制

    3.21  requires_grad属性

    3.22  向量的device

    3.23  tensor与numpy互换

    3.24  DataParallel用法详解

    3.25  to(device)和.cuda()的区别

    3.26  Dataset数据处理

    3.27  StepLR学习率调度器

    3.28  词嵌入的理解

    3.29  特征提取和可视化

    3.30  TensorDataset的使用

向量的device

创建时间:2024-09-15 | 更新时间:2024-09-15 | 阅读次数:1012 次

1、torch.device简介

torch.device是一个代表张量所在设备的对象。它表示张量Tensor已经或将会被分配的地方。

2、torch.device参数

torch.device的参数包含设备类型(最常见的是“cpu”或“cuda”)和设备序号(可选)。

如果设备序号没有给出,则使用当前设备。大家如果不知道当前设备,请使用下面代码查看一下:

import torch
print(torch.cuda.current_device())

综上所示,用设备"cuda"构造的张量等价于"cuda:X",其中Xtorch.cuda.current_device()的结果。

3、torch.device生成

新建torch.device的方式有两种,如下所示:

(1)通过字符串新建torch.device

>>> torch.device("cuda:0")
device(type="cuda", index=0)

>>> torch.device("cpu")
device(type="cpu")

>>> torch.device("mps")
device(type="mps")

>>> torch.device("cuda")  # current cuda device
device(type="cuda")

(2)通过字符串(设备类型)和设备序号新建torch.device

>>> torch.device("cuda", 0)
device(type="cuda", index=0)

>>> torch.device("mps", 0)
device(type="mps", index=0)

>>> torch.device("cpu", 0)
device(type="cpu", index=0)

4、torch.device上下文管理器

>>> with torch.device("cuda:1"):
...     r = torch.randn(2, 3)
>>> r.device
device(type="cuda", index=1)

5、torch.device缩写

函数中的torch.device参数通常可以用字符串替换。代码如下所示:

>>> cuda1 = torch.device("cuda:1")
>>> torch.randn((2,3), device=cuda1)

>>> # 可以用字符串代替orch.device
>>> torch.randn((2,3), device="cuda:1")

甚至还可以更简单。出于传统原因,可以通过单个设备序号构建设备,该序号被视为cuda设备。这与Tensor.get_device()匹配,后者返回cuda张量的序号,不支持cpu张量。

>>> torch.randn((2,3), device=1) 
device(type="cuda", index=1)
>>> torch.randn((2,3), device=1)

6、自动设备切换原则

张量永远不会在设备之间自动移动,需要用户进行显式调用。标量张量(tensor.dim()==0)是此规则的唯一例外,当需要时,它们会自动从CPU转移到GPU,因为此操作可以“自动”完成。例子:

>>> # two scalars
>>> torch.ones(()) + torch.ones(()).cuda()  # OK, scalar auto-transferred from CPU to GPU
>>> torch.ones(()).cuda() + torch.ones(())  # OK, scalar auto-transferred from CPU to GPU

>>> # one scalar (CPU), one vector (GPU)
>>> torch.ones(()) + torch.ones(1).cuda()  # OK, scalar auto-transferred from CPU to GPU
>>> torch.ones(1).cuda() + torch.ones(())  # OK, scalar auto-transferred from CPU to GPU

>>> # one scalar (GPU), one vector (CPU)
>>> torch.ones(()).cuda() + torch.ones(1)  # Fail, scalar not auto-transferred from GPU to CPU and non-scalar not auto-transferred from CPU to GPU
>>> torch.ones(1) + torch.ones(()).cuda()  # Fail, scalar not auto-transferred from GPU to CPU and non-scalar not auto-transferred from CPU to GPU
本教程共40节,当前为第32节!