【问题标题】:Keras Dropout with noise_shape带有noise_shape的Keras Dropout
【发布时间】:2017-10-05 11:59:24
【问题描述】:

我有一个关于Keras函数Dropout的问题,参数为noise_shape。

问题 1:

如果您的输入具有形状(batch_size, timesteps, features)并且您希望所有时间步的 dropout 掩码相同,则可以使用 noise_shape=(batch_size, 1, features) 是什么意思?添加这个参数有什么好处?

这是否意味着将被丢弃的神经元数量在时间步长上是相同的?这意味着在每个时间步 t,都会有 n 个神经元被丢弃?

问题 2: 创建模型时是否必须在noise_shape 中包含“batch_size”? --> 看下面的例子。

假设我有一个形状为 (10000, 1, 100, 2) -->(数据数、通道、时间步长、特征数)的多元时间序列数据。

然后我创建批次大小为 64 的批次 --> (64, 1, 100, 2)

如果我想创建一个带有 dropout 的 CNN 模型,我会使用 Keras 函数式 API:

inp = Input([1, 100, 2])
conv1 = Conv2D(64, kernel_size=(11,2), strides(1,1),data_format='channels_first')(inp)
max1 = MaxPooling2D((2,1))(conv1)
max1_shape = max1._keras_shape
drop1 = Dropout((0.1, noise_shape=[**?**, max1._keras_shape[1], 1, 1]))

因为max1层的输出形状应该是(None, 64, 50, 1),我不能给问号赋值None(对应batch_size)

我想知道我应该如何应对?我应该只使用 (64, 1, 1) 作为噪声形状吗?或者我应该定义一个名为“batch_size”的变量,然后将它传递给这个参数,如 (batch_size, 64, 1, 1)?

【问题讨论】:

    标签: python deep-learning keras dropout


    【解决方案1】:

    问题 1:

    我认为这有点像一个 numpy 广播。

    假设您有 2 个批次,有 3 个时间步长和 4 个特征(这是一个更容易展示的小示例): (2, 3, 4)

    如果您使用 (2, 1, 4) 的噪声形状,每个批次都会有自己的 将应用于所有时间步的 dropout 掩码。

    假设这些是形状 (2, 3, 4) 的权重:

    array([[[  1,   2,   3,   4],
            [  5,   6,   7,   8],
            [ 10,  11,  12,  13]],
    
           [[ 14,  15,  16,  17],
            [ 18,  19,  20,  21],
            [ 22,  23,  24,  25]]])
    

    这将是随机的 noise_shape (2, 1, 4) (1 表示保留,0 表示关闭):

    array([[[ 1,  1,  1,  0]],
    
           [[ 1,  0,  0,  1]]])
    

    所以你有这两种噪声形状(对于每一批)。 然后它将沿时间步长轴进行广播。

    array([[[ 1,  1,  1,  0],
            [ 1,  1,  1,  0],
            [ 1,  1,  1,  0]],
    
           [[ 1,  0,  0,  1],
            [ 1,  0,  0,  1],
            [ 1,  0,  0,  1]]])
    

    并应用于权重:

    array([[[  1,   2,   3,   0],
            [  5,   6,   7,   0],
            [ 10,  11,  12,   0]],
    
           [[ 14,   0,   0,  17],
            [ 18,   0,   0,  21],
            [ 22,   0,   0,  25]]])
    

    问题 2:

    说实话,我不确定你的第二个问题。

    编辑: 您可以做的是获取输入形状的第一维, 这应该是batch_size,正如github issue中所建议的那样:

    import tensorflow as tf
    
    ...
    
    batch_size = tf.shape(inp)[0]
    drop1 = Dropout((0.1, noise_shape=[batch_size, max1._keras_shape[1], 1, 1]))
    

    如您所见,我使用的是 tensorflow 后端。不知道如果theano也 有这些问题,如果有,你也许可以用 theano 形状等价物。

    【讨论】:

      【解决方案2】:

      下面是示例代码,可以查看到底发生了什么。 输出日志一目了然。

      如果您对动态batch_size 感到困扰,只需将noise_shape 的第一个元素设置为None,如下所示,即

      dl1 = tk.layers.Dropout(0.2, noise_shape=[_batch_size, 1, _num_features])

      dl1 = tk.layers.Dropout(0.2, noise_shape=[None, 1, _num_features])

      import tensorflow as tf
      import tensorflow.keras as tk
      import numpy as np
      
      _batch_size = 5
      _time_steps = 2
      _num_features = 3
      input = np.random.random((_batch_size, _time_steps, _num_features))
      dl = tk.layers.Dropout(0.2)
      dl1 = tk.layers.Dropout(0.2, noise_shape=[_batch_size, 1, _num_features])
      
      out = dl(input, training=True).numpy()
      out1 = dl1(input, training=True).numpy()
      
      
      for i in range(_batch_size):
          print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>", i)
          print("input")
          print(input[i])
          print("out")
          print(out[i])
          print("out1")
          print(out1[i])
      

      输出是:

      >>>>>>>>>>>>>>>>>>>>>>>>>>>> 0
      input
      [[0.53853024 0.80089701 0.64374258]
       [0.06481775 0.31187039 0.5029061 ]]
      out
      [[0.6731628  1.0011213  0.        ]
       [0.08102219 0.38983798 0.6286326 ]]
      out1
      [[0.6731628  0.         0.8046782 ]
       [0.08102219 0.         0.6286326 ]]
      >>>>>>>>>>>>>>>>>>>>>>>>>>>> 1
      input
      [[0.70746014 0.08990712 0.58195288]
       [0.75798534 0.50140453 0.04914242]]
      out
      [[0.8843252  0.11238389 0.        ]
       [0.9474817  0.62675565 0.        ]]
      out1
      [[0.         0.11238389 0.        ]
       [0.         0.62675565 0.        ]]
      >>>>>>>>>>>>>>>>>>>>>>>>>>>> 2
      input
      [[0.85253707 0.55813084 0.70741476]
       [0.98812977 0.21565134 0.67909392]]
      out
      [[1.0656713  0.69766355 0.8842684 ]
       [0.         0.26956415 0.        ]]
      out1
      [[1.0656713  0.69766355 0.8842684 ]
       [1.2351623  0.26956415 0.84886736]]
      >>>>>>>>>>>>>>>>>>>>>>>>>>>> 3
      input
      [[0.9837272  0.3504008  0.37425778]
       [0.67648931 0.74456052 0.6229444 ]]
      out
      [[1.2296591  0.438001   0.        ]
       [0.84561163 0.93070066 0.7786805 ]]
      out1
      [[0.         0.438001   0.46782222]
       [0.         0.93070066 0.7786805 ]]
      >>>>>>>>>>>>>>>>>>>>>>>>>>>> 4
      input
      [[0.45599217 0.80992091 0.04458478]
       [0.12214568 0.09821599 0.51525869]]
      out
      [[0.5699902  1.0124011  0.        ]
       [0.1526821  0.         0.64407337]]
      out1
      [[0.5699902  1.0124011  0.05573097]
       [0.1526821  0.12276999 0.64407337]]
      

      【讨论】:

        猜你喜欢
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        • 2019-08-09
        • 2021-04-06
        • 2019-11-24
        • 2021-03-27
        • 2019-05-22
        相关资源
        最近更新 更多