python - 凯拉斯/ tensorflow : Weird dropout behaviour

标签 python tensorflow machine-learning neural-network keras

我想看看 dropout 是如何工作的,所以我进入了 layers.core 模块并将 dropout 调用从 in_train_phase 更改为 in_test_phase.

我不确定我的更改是否对奇怪的丢失行为负责, 所以请多多包涵。

现在考虑到这些变化,下面的代码片段:

from keras.models import Model
from keras.layers import Dropout, Input
import numpy as np
import tensorflow as tf
from keras import initializers

x=np.ones((2,2,4))
# x[:,1,:] = 1

print(x)

from keras.layers import Dense

input = Input(name='atom_inputs', shape=(2, 4))

x1 = Dense(4, activation='linear',
           kernel_initializer=initializers.Ones(),
           bias_initializer='zeros')(input)
x1 = Dropout(0.5, noise_shape=(tf.shape(input)[0], 1, 4))(x1)


fmodel = Model(input, x1)
fmodel.compile(optimizer='sgd', loss='mse')
print(fmodel.predict(x))

会根据 dropout 率产生不同的预测。

例如:

Dropout(0.2)
[[[5. 5. 5. 5.]
  [5. 5. 5. 5.]]

 [[5. 0. 5. 0.]
  [5. 0. 5. 0.]]]

Dropout(0.5)
[[[0. 0. 8. 8.]
  [0. 0. 8. 8.]]

 [[8. 0. 8. 8.]
  [8. 0. 8. 8.]]]

我哪里错了? dropout 定义在密集输出层上, 所以它应该只影响关闭和打开的神经元,而不是它们的 各自的值(value)。对吧?

最佳答案

发生这种情况是因为在使用 Dropout 时,您不仅可以打开和关闭不同的神经元,还可以缩放数据以补偿由于部分神经元被遮挡而导致下一层可能接收到较少信号的事实神经元。它被称为反向丢失,您可能会读到它 here .

因此,基本上,您网络的每个输出都按 1/(1 - p) 因子重新缩放以进行补偿。这就是您的输出不同的原因。

对于 Dropout(0.2) 补偿是 1/(1 - 0.2) = 1.25 这导致 5 = 4 * 1.25Dropout(0.5) 的补偿是 1/(1 - 0.5) = 2,这导致 8 = 4 * 2

关于python - 凯拉斯/ tensorflow : Weird dropout behaviour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48212629/

相关文章:

python - model.fit vs model.predict - sklearn 中的差异和用法

python - 从 Python 中的输入创建元组

python - 如何在pandas数据框中按特定条件进行分组

python - 将 MATLAB 的 interp1 转换为 Python interp1d

python - 如何让 tf.data.Dataset.from_tensor_slices 接受我的 dtype?

python - 这个 'single' 值在梯度中代表什么?

python - 查找列表中连续增加的元素的数量

python - "tf.train.replica_device_setter"是如何工作的?

python - AWS - Step 函数,在 TuningStep 中使用执行输入

machine-learning - ROC 曲线显示奇怪的模式