【问题标题】:Using Dropout on output of embedding layer changes array values, Why?在嵌入层的输出上使用 Dropout 会更改数组值,为什么?
【发布时间】:2020-09-04 11:35:39
【问题描述】:

观察带有和不带dropout的嵌入层的输出表明数组中的值被替换为0。但是为什么数组的其他值也随之改变了?

以下是我的模型:-

input = Input(shape=(23,)) 
model = Embedding(input_dim=n_words, output_dim=23, input_length=23)(input)
model = Dropout(0.2)(model)
model = Bidirectional(LSTM(units=LSTM_N, return_sequences=True, recurrent_dropout=0.1))(model)
out = TimeDistributed(Dense(n_tags, activation="softmax"))(model) # softmax output layer
model = Model(input, out)

从训练好的模型构建模型2,输入作为输入层,输出作为 Dropout(0.2) 的输出。 -

from keras import backend as K
model2 = K.function([model.layers[0].input ,  K.learning_phase()],
                  [model.layers[2].output]   )
dropout = model2([X_train[0:1] , 1])[0]
nodrop = model2([X_train[0:1] , 0])[0]

打印第一个包含 dropout 和 no dropout 的数组:

dropout[0][0]

输出-

array([ 0.        , -0.        , -0.        , -0.04656423, -0.        ,
        0.28391626,  0.12213208, -0.01187495, -0.02078421, -0.        ,
        0.10585815, -0.        ,  0.27178472, -0.21080771,  0.        ,
       -0.09336889,  0.07441022,  0.02960865, -0.2755439 , -0.11252255,
       -0.04330419, -0.        ,  0.04974075], dtype=float32)   

-

nodrop[0][0]

输出-

array([ 0.09657606, -0.06267098, -0.00049554, -0.03725138, -0.11286845,
    0.22713302,  0.09770566, -0.00949996, -0.01662737, -0.05788678,
    0.08468652, -0.22405024,  0.21742778, -0.16864617,  0.08558936,
   -0.07469511,  0.05952817,  0.02368692, -0.22043513, -0.09001804,
   -0.03464335, -0.05152775,  0.0397926 ], dtype=float32)

有些值被替换为 0 ,同意,但是为什么其他值被改变了? 由于嵌入输出具有含义并且对于每个单词都是唯一的,如果通过应用 dropout 来更改这些输出,那么 在嵌入层之后应用 dropout 是否正确?

注意-我使用“learning_phase”作为 0 和 1 进行测试(nodropout) 和训练(辍学)分别。

【问题讨论】:

    标签: python python-3.x keras deep-learning word-embedding


    【解决方案1】:

    这就是 dropout 正则化的工作原理。应用 dropout 后,这些值除以保持概率(在本例中为 0.8)。

    当您使用 dropout 时,该函数接收将神经元变为零的概率作为输入,例如 0.2,这意味着它有 0.8 的机会保留任何给定的神经元。因此,剩余的值将乘以 1/(1-0.2)。

    这被称为“反向 dropout 技术”,这样做是为了确保激活的期望值保持不变。否则,在不使用 dropout 的情况下,推理过程中的预测会出错。

    您会注意到您的 dropout 为 0.2,并且在您应用 dropout 后,您的所有值都乘以 0.8。

    看看如果我将你的第二个输出除第一个会发生什么:

    import numpy as np
    a = np.array([ 0.        , -0.        , -0.        , -0.04656423, -0.        ,
            0.28391626,  0.12213208, -0.01187495, -0.02078421, -0.        ,
            0.10585815, -0.        ,  0.27178472, -0.21080771,  0.        ,
           -0.09336889,  0.07441022,  0.02960865, -0.2755439 , -0.11252255,
           -0.04330419, -0.        ,  0.04974075])
    
    b = np.array([ 0.09657606, -0.06267098, -0.00049554, -0.03725138, -0.11286845,
        0.22713302,  0.09770566, -0.00949996, -0.01662737, -0.05788678,
        0.08468652, -0.22405024,  0.21742778, -0.16864617,  0.08558936,
       -0.07469511,  0.05952817,  0.02368692, -0.22043513, -0.09001804,
       -0.03464335, -0.05152775,  0.0397926 ])
    
    print(b/a)
    
    [       inf        inf        inf 0.79999991        inf 0.80000004
     0.79999997 0.8        0.8000001         inf 0.8               inf
     0.80000001 0.80000001        inf 0.79999998 0.79999992 0.8
     0.80000004 0.8        0.79999995        inf 0.8       ]
    

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2021-08-02
      • 2020-12-10
      • 2016-12-23
      • 2017-10-20
      • 1970-01-01
      • 2020-10-21
      • 2019-05-10
      • 2019-08-09
      相关资源
      最近更新 更多