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的使用

设置控制台输出格式

创建时间:2024-09-10 | 更新时间:2024-09-10 | 阅读次数:1158 次

提示:平心而论,此内容不属于面试题,没有可考性,但是对大家平时代码调试很有帮助,所以我就收录进来,自成一个小节。

torch.set_printoptions函数用于设置打印选项:

torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)

参数介绍:

  • precision – Number of digits of precision for floating point output (default = 4).
  • threshold – Total number of array elements which trigger summarization rather than full repr (default = 1000).
  • edgeitems – Number of array items in summary at beginning and end of each dimension (default = 3).
  • linewidth – The number of characters per line for the purpose of inserting line breaks (default = 80). Thresholded matrices will ignore this parameter.
  • profile – Sane defaults for pretty printing. Can override with any of the above options. (any one of default, short, full)
  • sci_mode – Enable (True) or disable (False) scientific notation. If None (default) is specified, the value is defined by torch._tensor_str._Formatter. This value is automatically chosen by the framework.

说明:参数设置来源于NumPy。Pytorch官方用调侃的口吻说:Items shamelessly taken from NumPy。

代码举例:

>>> # Limit the precision of elements
>>> torch.set_printoptions(precision=2)
>>> torch.tensor([1.12345])
tensor([1.12])
>>> # Limit the number of elements shown
>>> torch.set_printoptions(threshold=5)
>>> torch.arange(10)
tensor([0, 1, 2, ..., 7, 8, 9])
>>> # Restore defaults
>>> torch.set_printoptions(profile="default")
>>> torch.tensor([1.12345])
tensor([1.1235])
>>> torch.arange(10)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
本教程共40节,当前为第3节!