当前位置:   article > 正文

torch.nn.functional

torch.nn.functional

1. 非线性激活函数

  • torch.nn.functional.threshold(input, threshold, value, inplace=False)
  • torch.nn.functional.relu(input, inplace=False)
  • torch.nn.functional.relu6(input, inplace=False)
  • torch.nn.functional.elu(input, alpha=1.0, inplace=False)
  • torch.nn.functional.leaky_relu(input, negative_slope=0.01, inplace=False)
  • torch.nn.functional.prelu(input, weight)
  • torch.nn.functional.rrelu(input, lower=0.125, upper=0.3333333333333333, training=False, inplace=False)
  • torch.nn.functional.logsigmoid(input)
  • torch.nn.functional.softmax(input)
  • torch.nn.functional.log_softmax(input)
  • torch.nn.functional.tanh(input)
  • torch.nn.functional.sigmoid(input)

2. 填充和放缩

torch.nn.functional.pad(input, pad, mode=‘constant’, value=0)

对数据集图像或中间层特征进行维度扩充。

参数:

  • input:需要扩充的tensor,可以是图像数据,抑或是特征矩阵数据
  • pad:扩充维度
  • mod:扩充方法,’constant‘, ‘reflect’ or ‘replicate’三种模式,分别表示常量,反射,复制
  • value:扩充时指定补充值,但是value只在mode='constant’有效,即使用value填充在扩充出的新维度位置,而在’reflect’和’replicate’模式下,value不可赋值
import torch
import torch.nn.functional as F
 
t4d = torch.empty(1, 3, 5, 3)

p1d_ = (1, 2, 0, 0)
t1 = F.pad(t4d, p1d_, 'constant', 1) # 对图像的左边填充1列,右边填充2列

p2d = (1, 2, 3, 4)
t2 = F.pad(t4d, p2d, 'constant', 2) # 对图像的左边填充1列,右边填充2列,上边填充3行,下边填充4行

p3d = (1, 2, 3, 4, 5, 6)
t3 = F.pad(t4d, p3d, 'constant', 3) # 对图像的左边填充1列,右边填充2列,上边填充3行,下边填充4行,对通道方向的前面填充5层,对通道方向的后面填充6层。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode=‘nearest’, align_corners=None, recompute_scale_factor=None, antialias=False) -> Tensor

在这里插入图片描述
在这里插入图片描述

torch.nn.functional.upsample(input, size=None, scale_factor=None, mode=‘nearest’, align_corners=None)

在这里插入图片描述

3. 网格采样

torch.meshgrid(*tensors, indexing=None)

创建由 tensors 中的一维输入指定的坐标网格。

在这里插入图片描述

torch.nn.functional.grid_sample(input, grid, mode=‘bilinear’, padding_mode=‘zeros’, align_corners=None)

给定输入和网格,使用输入值和网格中的像素位置计算输出。目前,仅支持空间 (4-D) 和体积 (5-D) 输入。

此函数通常与 affine_grid() 结合使用来构建Spatial Transformer Networks

在这里插入图片描述

注意:girid 指定由输入空间维度归一化的采样像素位置。 因此,它应该具有 [-1, 1] 范围内的大多数值。 例如,值 x = -1, y = -1 是输入的左上角像素,值 x = 1, y = 1 是输入的右下角像素。

torch.nn.functional.affine_grid(theta, size, align_corners=None)

给定一批仿射矩阵 theta,生成 2D 或 3D 采样网格。

此函数通常与 grid_sample() 结合使用来构建Spatial Transformer Networks

在这里插入图片描述
例子请移步:Pytorch中的仿射变换(affine_grid)

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号