【问题标题】:Dropout entire input layer丢弃整个输入层
【发布时间】:2019-09-11 03:03:51
【问题描述】:

假设我有两个输入(每个都有许多特征),我想将它们输入Dropout 层。我希望每次迭代都丢弃整个输入及其所有相关特征,并保留整个其他输入。

连接输入后,我想我需要为Dropout 使用noise_shape 参数,但连接层的形状并不能真正让我这样做。对于形状 (15,) 的两个输入,连接的形状是 (None, 30),而不是 (None, 15, 2),因此其中一个轴丢失了,我不能沿着它退出。

对我能做什么有什么建议吗?谢谢。

from keras.layers import Input, concatenate, Dense, Dropout

x = Input((15,))  # 15 features for the 1st input
y = Input((15,))  # 15 features for the 2nd input
xy = concatenate([x, y])
print(xy._keras_shape)
# (None, 30)

layer = Dropout(rate=0.5, noise_shape=[xy.shape[0], 1])(xy)
...

【问题讨论】:

  • 我实现这一点的一种方法是在生成器中。您可以构建一个返回两个输入的生成器,在其中它会随机返回其中一个作为 0,概率为 0.5

标签: python keras neural-network


【解决方案1】:

编辑:

好像我误解了你的问题,这里是根据你的要求更新的答案。

为了实现你想要的,x 和 y 有效地成为时间步长,根据 Keras 文档,noise_shape=(batch_size, 1, features) 如果你的输入形状是 (batch_size, timesteps, features)

x = Input((15,1))  # 15 features for the 1st input
y = Input((15,1))  # 15 features for the 2nd input
xy = concatenate([x, y])

dropout_layer = Dropout(rate=0.5, noise_shape=[None, 1, 2])(xy)
...

要测试您是否获得了正确的行为,您可以使用以下代码 (reference link) 检查中间 xy 层和 dropout_layer

### Define your model ###

from keras.layers import Input, concatenate, Dropout
from keras.models import Model
from keras import backend as K

# Learning phase must be set to 1 for dropout to work
K.set_learning_phase(1)

x = Input((15,1))  # 15 features for the 1st input
y = Input((15,1))  # 15 features for the 2nd input
xy = concatenate([x, y])

dropout_layer = Dropout(rate=0.5, noise_shape=[None, 1, 2])(xy)

model = Model(inputs=[x,y], output=dropout_layer)

# specify inputs and output of the model

x_inp = model.input[0]                                           
y_inp = model.input[1]
outp = [layer.output for layer in model.layers[2:]]        
functor = K.function([x_inp, y_inp], outp)

### Get some random inputs ###

import numpy as np

input_1 = np.random.random((1,15,1))
input_2 = np.random.random((1,15,1))

layer_outs = functor([input_1,input_2])
print('Intermediate xy layer:\n\n',layer_outs[0])
print('Dropout layer:\n\n', layer_outs[1])

您应该看到整个 x 或 y 根据您的要求随机删除(50% 的机会):

Intermediate xy layer:

 [[[0.32093528 0.70682645]
  [0.46162075 0.74063486]
  [0.522718   0.22318116]
  [0.7897043  0.7849486 ]
  [0.49387926 0.13929296]
  [0.5754296  0.6273373 ]
  [0.17157765 0.92996144]
  [0.36210892 0.02305864]
  [0.52637625 0.88259524]
  [0.3184462  0.00197006]
  [0.67196816 0.40147918]
  [0.24782693 0.5766827 ]
  [0.25653633 0.00514544]
  [0.8130438  0.2764429 ]
  [0.25275478 0.44348967]]]

Dropout layer:

 [[[0.         1.4136529 ]
  [0.         1.4812697 ]
  [0.         0.44636232]
  [0.         1.5698972 ]
  [0.         0.2785859 ]
  [0.         1.2546746 ]
  [0.         1.8599229 ]
  [0.         0.04611728]
  [0.         1.7651905 ]
  [0.         0.00394012]
  [0.         0.80295837]
  [0.         1.1533654 ]
  [0.         0.01029088]
  [0.         0.5528858 ]
  [0.         0.88697934]]]

如果你想知道为什么所有元素都乘以 2,看看 tensorflow 如何实现 dropout here

希望这会有所帮助。

【讨论】:

  • 感谢您的建议。关于第二点,dropout 不是让x 的特征与rate=0.5 一起被丢弃,而不是整个x
  • 它随机选择整个x 的50% 被丢弃。根据文档 - Dropout consists in randomly setting a fraction rate of input units to 0 at each update during training time, which helps prevent overfitting.rate: float between 0 and 1. Fraction of the input units to drop.
  • 您的解释和文档在我看来就像x 的个别功能很有可能会被删除rate。我想要的是保留或删除x,概率为rate。我希望有一种简单的方法来测试丢弃的内容。
  • @foxpal 抱歉,我不知道你想保留或删除 xy 完全有可能 rate。更新了我的答案以适合您的应用程序,并包含有关如何检查输出的代码 sn-p。干杯。
  • 很高兴它有帮助。关于 xy 的两倍,当两者都没有被丢弃时,我不会说这是错误的,而是在 Tensorflow 中如何实现 dropout 的限制。当p = 0.5 时将它们加倍的目的是近似原始输出(没有丢失),这是在大多数更新中实现的(从概率上讲,尤其是当有更多时间步长时)。
猜你喜欢
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 2013-10-12
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多