torch.nn.functional

非线性激活函数

  • 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)

填充和压缩

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层。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
torch.nn.functional.upsample(input, size=None, scale_factor=None, mode=‘nearest’, align_corners=None)
在这里插入图片描述

网格采样

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

  • torch.nn.functional.affine_grid(theta, size, align_corners=None)
    给定一批放射矩阵theta,生成2D或3D的采样网络。

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

项目中的使用

F.log_softmax

作用:在softmax结果基础上再多做一次Log运算。
F.log_softmax函数语言格式::

F.log_softmax(x,dim=1) 或者 F.log_softmax(x,dim=0)

参数解释

  • x x x指输入的矩阵
  • d i m dim dim指归一化的方式,如果为0,则对列作归一化,如果为1,则对行作归一化。

Flatten()

是对多维数据降维的函数。
latten(),默认缺省参数为0,也就是说flatten()和flatte(0)效果一样

python里的flatten(dim)表示,从第dim个维度开始展开,将后面的维度转化为一维.也就是说,只保留dim之前的维度,其他维度的数据全都挤在dim这一维。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

big_matster

您的鼓励,是给予我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值