Dropout 设置图层权重数组为空

Dropout setting layer weights array to empty

我正在尝试访问该网络的训练权重值:

X = [[1] , [2], [3], [4], [5], [6], [7], [8]]
Y = [[1] , [2] ,[3] ,[4] ,[5] ,[6] ,[7] ,[8]]

from keras.callbacks import History 
history = History()

from keras import optimizers

inputDim = len(X[0])
print('input dim' , inputDim)
model = Sequential()

model.add(Dense(1, activation='sigmoid', input_dim=inputDim))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.add(Dropout(0.2))

sgd = optimizers.SGD(lr=0.009, decay=1e-10, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd , metrics=['accuracy'])
model.fit(X,Y , validation_split=0.1 , verbose=2 , callbacks=[history] , epochs=5,batch_size=32)

可以访问第一层的权重:

weights = model.get_layer(index=1).get_weights()
weights
[array([[-0.93537247]], dtype=float32), array([ 0.00989669], dtype=float32)]

但不在第 2 层:

weights = model.get_layer(index=2).get_weights()
weights
[]

似乎添加 dropout 导致了这一点,因为删除了 dropout:

model.add(Dense(1, activation='sigmoid', input_dim=inputDim))
model.add(Dense(1, activation='sigmoid'))

访问权重成功

weights = model.get_layer(index=1).get_weights()
weights

[array([[-0.62137389]], dtype=float32), array([-0.05591233], dtype=float32)]

weights = model.get_layer(index=2).get_weights()
weights

[array([[-1.09568715]], dtype=float32), array([ 0.53271592], dtype=float32)]

正在阅读有关 Dropout 的内容:https://keras.io/layers/core/

"Dropout consists in randomly setting a fraction rate of input units to 0 at each update during training time, which helps prevent overfitting."

Dropout 可视化: 来源:http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf

所以神经元的一个子集被丢弃了。但在我的示例中,由于第 2 层中的权重参数是一个空数组,所有神经元似乎都被丢弃了? 为什么添加dropout会导致后续层中的权重参数变得不可访问?

辍学没有权重。密集层(和其他一些类型)有。

您的第一个模型有这些层:

0: input layer - no weights 
1: Dense(...) - weights and biases    
2: Dropout(...) - no weights    
3: Dense(...) - weights and biases
4: Dropout(...) - no weights

权重是属于单个层的可训练参数。

dropouts 只是有一个无法训练的常量参数。此参数仅删除部分结果。


密集层使用它们的权重来改变输入并抛出输出:

#pseudocode
output = Weights x input + biases

丢弃层只会丢弃一些输出:

#pseudocode - suppose the dropout parameter is 0.2
output = input[take 80% of the elements]