• torch.nn.Dropout()


    1. torch.nn.Dropout()

    class torch.nn.Dropout(p=0.5, inplace=False)
    

    随机将输入张量中部分元素设置为 \(0\)。对于每次前向调用,被置 \(0\) 的元素都是随机的。

    参数:

    • p:将元素置 \(0\) 的概率。(默认:0.5)
    • inplace:设置为 \(True\),会在原地执行操作。(默认:\(False\)

    示例:

    import torch
    from torch import nn, autograd
    
    m = nn.Dropout(p=0.5)
    input = autograd.Variable(torch.randn(4, 4))
    output = m(input)
    print(output)
    
    tensor([[ 2.3192,  0.0000,  0.0000, -0.0000],
            [ 0.0000,  0.3854, -1.9737, -0.0000],
            [-2.8518, -0.7907, -1.2949, -0.0000],
            [-0.0000, -3.0302, -0.0000, -0.0000]])
    

    2. torch.nn.Dropout2d()

    class torch.nn.Dropout2d(p=0.5, inplace=False)
    

    随机将输入张量中整个通道设置为 \(0\)。对于每次前向调用,被置 \(0\) 的通道都是随机的。

    通常输入来自 \(Conv2d\) 模块。

    如果特征图中相邻像素是强相关的(在前几层卷集层很常见),那么nn.Dropout不会归一化激活,而只会降低学习率。

    在这种情形下,nn.Dropout2d()可以提高特征图之间的独立程度,所以应该使用它。

    参数:

    • p (\(float,optional\)):将元素置 \(0\) 的概率。(默认:0.5)
    • inplace (\(bool, optional\)):设置为 \(True\),会在原地执行操作。(默认:\(False\)

    形状:

    • 输入:\(((N, C,H,W))\)
    • 输出:\(((N, C,H,W))\)(与输入相同)

    示例:

    import torch
    from torch import nn, autograd
    
    m = nn.Dropout2d(p=0.2)
    input = autograd.Variable(torch.randn(2, 3, 2, 2))
    output = m(input)
    print(output)
    
    tensor([[[[-0.2310,  0.2985],
              [-1.1936, -0.7231]],
    
             [[-0.7498,  0.2923],
              [-0.2470, -0.7570]],
    
             [[ 2.0942, -0.2796],
              [ 0.7177,  3.7006]]],
    
    
            [[[-0.0000, -0.0000],
              [-0.0000,  0.0000]],
    
             [[ 0.1563,  0.9973],
              [ 0.8000,  1.1723]],
    
             [[-0.1494,  0.2946],
              [-0.4893, -0.3955]]]])
    


    来自:Dropout layers

  • 相关阅读:
    【强转】QEMU+GDB调试linux内核全过程
    从上往下打印二叉树
    栈的压入、弹出序列
    包含main函数的栈
    顺时针打印矩阵
    Linux终端美化
    Linux 终端美化
    KDE桌面环境自带的Konsole终端配置
    KDE美化及常用设置
    KDE常用桌面插件总结
  • 原文地址:https://www.cnblogs.com/keye/p/16591856.html
Copyright © 2020-2023  润新知