• 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这一维。

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

  • 相关阅读:
    web前端大作业:旅游网页主题网站设计——武汉旅游网页设计(11页)HTML+CSS+JavaScript
    QCC51XX---Task list分析
    2216.美化数组的最少删除数
    谷歌AudioLM :通过歌曲片段生成后续的音乐
    05 动态SQL&&标签
    CDH6下线DataNode节点
    【智能制造-17】路径规划算法-2
    学完自动化测试一年,达到月薪15K、我的这份笔记希望能让你受益良多...
    剑指offer(C++)-JZ38:字符串的排列(算法-搜索算法)
    【spring boot 使用模板引擎】
  • 原文地址:https://blog.csdn.net/kuxingseng123/article/details/128210412