【问题标题】:How to modify a neural network with Keras during training?如何在训练期间使用 Keras 修改神经网络?
【发布时间】:2018-03-16 19:25:24
【问题描述】:

假设我的网络中有以下内容:

x = Conv2D(
            filters=256,
            kernel_size=5,
            strides=2,
            padding="same"
        )(x)
x = Dropout(0.5)(x)
x = BatchNormalization(momentum=0.8)(x)
x = LeakyReLU(alpha=0.2)(x)

顺便说一句,我正在使用 Tensorflow 后端。

在训练期间,我想修改或降低 Dropout 层的值。最终,有什么方法可以停用它?

【问题讨论】:

  • 您到底想什么时候停用?
  • 为了举例,我想每 2 个 epoch 一次将辍学率除以 2。在 10 个 epoch 之后,我想完全禁用它
  • 如果可以的话,我可以告诉你如何在 tensorflow 中做到这一点?
  • 我至少可以试试,谢谢
  • 您可以在 tensorflow 中定义一个占位符,比如说 keep_prob,它具有 dropout 的概率值。现在,当您训练模型时,您可以根据您的时代更改变量 keep_prob 并在这行代码中更新 - sess.run(xx ,feed_dict= {x: batchx, ypred :batchy, keep_prob:dropout_rate}

标签: tensorflow neural-network deep-learning keras keras-layer


【解决方案1】:

我终于知道了:

class MyModel():
    def __init__(self, init_dropout, dropout_decay):

        self.init_dropout  = init_dropout
        self.dropout_decay = dropout_decay

        input_layer = Input((64, 64, 1))
        x = Conv2D(
            filters=256,
            kernel_size=5,
            strides=2,
            padding="same"
        )(input_layer)
        x = Dropout(rate=init_dropout)(x)
        x = BatchNormalization(momentum=0.8)(x)
        x = LeakyReLU(alpha=0.2)(x)

        self.model = Model(input_layer, x)

    def decay_dropout(self, epoch, verbose=0):

        rate = max(0, self.init_dropout * (1 / np.exp(self.dropout_decay * epoch))) #define a formula for dropout decay

        for layer in self.model.layers:
            if isinstance(layer, Dropout):

                if (verbose >= 1):
                    print("Decaying Dropout from %.3f to %.3f" % (layer.rate, rate))

                layer.rate = rate

然后当然需要在每个 epoch 之后调用函数 decay_dropout。

【讨论】:

  • 你确定这行得通吗?我也尝试过使用layer.rate,但在我的实验中它根本不起作用。为了对此进行测试,我只是尝试用少量样本过度拟合 nn。如果在创建模型(函数式 API)期间将 dropout rate 设置为 0.0,那么过拟合小数据样本显然会起作用。但是,如果您创建它时的辍学率为例如0.2 然后尝试使用layer.rate 再次将其设置为 0.0,网络永远不会过拟合。所以对我来说,layer.rate 似乎将速率写入配置,但从未使用过(在设置速率后编译也不起作用)。
猜你喜欢
  • 2019-12-17
  • 1970-01-01
  • 2017-07-14
  • 2017-05-18
  • 2017-11-27
  • 2017-02-20
  • 2011-04-07
  • 1970-01-01
  • 2017-12-05
相关资源
最近更新 更多