观察嵌入层在使用和不使用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[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是否正确?
注意,我在测试(nodropout)和训练(droput)中分别使用了 "learning_phase "作为0和1。
这就是 dropout 正则化的工作原理。在应用 dropout 后,数值被除以保持概率(在本例中为 0.8)。
当你使用dropout时,函数会接收将一个神经元变成零的概率作为输入,例如,0.2,这意味着它有0.8的机会保持任何给定的神经元。所以,剩下的值将乘以1(1-0.2)。
这就是所谓的 "倒置丢弃技术",这样做是为了确保激活的预期值保持不变。否则,当不使用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 ]