![](/img/trans.png)
[英]why autoencoder tutorial of pytorch changes the view of embedding layer output?
[英]Using Dropout on output of embedding layer changes array values, Why?
觀察嵌入層的輸出(有和沒有丟失)表明 arrays 中的值被替換為 0。但隨之而來的是為什么數組的其他值發生了變化?
以下是我的 model:-
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)
從訓練的 model 構建模型 2,輸入作為輸入層,output 作為 Dropout(0.2) 的 output。 -
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)和訓練(dropout)。
這就是 dropout 正則化的工作原理。 應用 dropout 后,這些值除以保持概率(在本例中為 0.8)。
當您使用 dropout 時,function 接收將神經元變為零的概率作為輸入,例如 0.2,這意味着它有 0.8 的機會保留任何給定的神經元。 因此,剩余的值將乘以 1/(1-0.2)。
這被稱為“反向 dropout 技術”,這樣做是為了確保激活的預期值保持不變。 否則,在不使用 dropout 的情況下,推理過程中的預測將是錯誤的。
您會注意到您的 dropout 為 0.2,並且在您應用 dropout 后,您的所有值都乘以 0.8。
看看如果我把你的第二個 output 分成第一個會發生什么:
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 ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.