pytorch一些用到的函数记录

torch.range troch.arange

import torch
x = torch.range(0, 4)
y = torch.arange(0, 4)
print(x, x.dtype)
print(y, y.dtype)
tensor([0., 1., 2., 3., 4.]) torch.float32
tensor([0, 1, 2, 3]) torch.int64
__main__:2: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].
  1. torch.range将被弃用,建议使用torch.arange
  2. torch.arange类似于range,范围为[start, end), dtypetorch.int64,也可以指定类型dtype=torch.float32

torch.meshgrid

先看代码

import torch
x = torch.arange(0,2) #[H]
y = torch.arange(0, 4) #[W]
z = torch.meshgrid(x, y) #([H,W], [H, W])
print(x)
print(y)
print(z)

'''out
tensor([0, 1])
tensor([0, 1, 2, 3])
(tensor([[0, 0, 0, 0],
        [1, 1, 1, 1]]), tensor([[0, 1, 2, 3],
        [0, 1, 2, 3]]))
'''

以上代码生成的grid为两个相同size的矩阵,grid坐标矩阵即为两个矩阵对应的点组成
rows为行坐标的列向量,cols为列坐标的列向量,他们能够构成的网格大小即为 H × W H\times W H×W
生成网格坐标即为两个矩阵想对应点构成的坐标


torch.cat和torch.stack

torch.cat(tensor, dim),拼接,不会增加维度,只会增大tensor的大小,拼接的维度大小需要一致
torch.stack(tensor, dim),堆叠,在维度上进行堆叠,会增加矩阵维度
看代码

import torch
x = torch.arange(0, 4)
y = torch.arange(0, 4)
z = torch.meshgrid(x, y)
a = torch.cat((x, y))
b = torch.stack((x, y))
print(a.shape)
print(b.shape)

'''out
torch.Size([8])
torch.Size([2, 4])
'''

torch.tensor&numpy&PIL

  • torch.from_numpy(array): numpy转为tensor
  • tensor.numpy(): tensor转为numpy
  • np.array(img): pil图像变为numpy
  • Image.fromarray(img_array): numpy转为PIL图像

torch插值

  • torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)

input 必须是一个 ( n , c , h , w ) (n,c,h,w) (n,c,h,w),四维矩阵

Parameters
- input (Tensor) – the input tensor

- size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) – output spatial size.

- scale_factor (float or Tuple[float]) – multiplier for spatial size. Has to match input size if it is a tuple.

- mode (str) – algorithm used for upsampling: 'nearest' | 'linear' | 'bilinear' | 'bicubic' | 'trilinear' | 'area'. Default: 'nearest'

- align_corners (bool, optional) – Geometrically, we consider the pixels of the input and output as squares rather than points. If set to True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If set to False, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size when scale_factor is kept the same. This only has an effect when mode is 'linear', 'bilinear', 'bicubic' or 'trilinear'. Default: False

torchvison.transforms.functional PIL和tensor转换

先看torch官网描述
torch官网描述

import torchvision.transforms.functional as tf
pil_image = tf.to_pil_image(tensor)
tensor= tf.to_tensor(pil_img)

在数据预处理和训练结果保存时使用很方便


ModuleList 和 Sequential

PyTorch 中的 ModuleList 和 Sequential 转自知乎


torch.nn.utils.clip_grad_norm()

source code
防止梯度爆炸

def clip_grad_norm_(parameters, max_norm, norm_type=2):
    r"""Clips gradient norm of an iterable of parameters.

    The norm is computed over all gradients together, as if they were
    concatenated into a single vector. Gradients are modified in-place.

    Arguments:
        parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a
            single Tensor that will have gradients normalized
        max_norm (float or int): max norm of the gradients
        norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for
            infinity norm.

    Returns:
        Total norm of the parameters (viewed as a single vector).
    """
    if isinstance(parameters, torch.Tensor):
        parameters = [parameters]
    parameters = list(filter(lambda p: p.grad is not None, parameters))
    max_norm = float(max_norm)
    norm_type = float(norm_type)
    if norm_type == inf:
        total_norm = max(p.grad.data.abs().max() for p in parameters)
    else:
        total_norm = 0
        for p in parameters:
            param_norm = p.grad.data.norm(norm_type)
            total_norm += param_norm.item() ** norm_type
        total_norm = total_norm ** (1. / norm_type)
    clip_coef = max_norm / (total_norm + 1e-6)
    if clip_coef < 1:
        for p in parameters:
            p.grad.data.mul_(clip_coef)
    return total_norm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值