• 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层。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    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)
    
    
    • 1
    • 2

    参数解释

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

    Flatten()

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

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

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

  • 相关阅读:
    std : : vector
    AlphaFold2源码解析(3)--数据预处理
    多维数组的【】和多级指针*转化推演
    python自动化系列之python操作pptx文件
    【LeetCode】【剑指offer】【扑克牌中的顺子】
    LeetCode2409——统计共同度过的日子数
    安装libX11过程记录
    pytest的使用
    JMeter 扩展开发:自定义 Java Sampler
    Tsinghua:Finding Skill Neurons in Pre-trained Transformer-based Language Models
  • 原文地址:https://blog.csdn.net/kuxingseng123/article/details/128210412