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
生成网格坐标即为两个矩阵想对应点构成的坐标


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) ,四维矩阵

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 转自知乎


发布了20 篇原创文章 · 获赞 0 · 访问量 379

猜你喜欢

转载自blog.csdn.net/yywxl/article/details/102952177