【基于PyTorch】nn.Dropout() 使用

  • 这里p=0.2 表示,每个神经元都有0.2 的概率被丢弃(变成0)
m = nn.Dropout(p=0.2)
  • 代码:
from torch import nn
import torch

m = nn.Dropout(p=0.2)
input1 = torch.randn(4, 5)
output = m(input1)
print(output)
  • 输出1:
tensor([[-0.6151, -0.0000, -1.0482, -0.5827,  2.8544],
        [ 0.1094,  0.2557,  0.0000, -0.8655,  0.4116],
        [ 0.7518,  0.1360,  0.8428,  1.9005, -2.1760],
        [-0.0000, -1.3959, -1.4706, -1.9726,  0.5573]])
  • 输出2:
tensor([[-3.1378,  0.4837,  0.3389,  0.0000,  1.6898],
        [ 0.7625,  0.3036,  0.6053,  2.1331,  0.9009],
        [ 0.0000, -0.6067,  0.4107,  0.0000, -2.7134],
        [ 0.4862,  0.0424, -0.2511, -0.3606, -1.6395]])
  • 输出3
ensor([[-1.0125, -0.0000, -1.7310, -0.0790, -0.5104],
        [ 0.4416,  0.2899, -1.0046,  1.4640,  2.3754],
        [ 0.1155,  0.0000,  0.1253, -0.0000,  1.4655],
        [-0.7001, -0.4758, -0.0000, -0.0148,  1.7307]])
并不是说,总的神经元个数(比如4×5=20),然后乘以概率p,就能得到置零的个数是4个,并不一定是四个!!!!

0.2表示的是,每个神经元都有0.2 的可能性置零。

你可能感兴趣的:(PyTorch深度学习,pytorch,深度学习,人工智能)