文章目录
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].
torch.range
将被弃用,建议使用torch.arange
torch.arange
类似于range
,范围为[start, end)
,dtype
为torch.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转为tensortensor.numpy()
: tensor转为numpynp.array(img)
: pil图像变为numpyImage.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官网描述
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