【问题标题】:Change learning rate within minibatch - keras在小批量内改变学习率 - keras
【发布时间】:2021-08-23 13:41:21
【问题描述】:

我遇到了标签不平衡的问题,例如 90% 的数据具有标签 0,其余 10% 的数据具有标签 1。

我想用 minibatch 教授网络。因此,我希望优化器为标记为 1 的示例提供比标记为 0 的示例的学习率(或以某种方式将梯度更改为)大 9。

有什么办法吗?

问题是整个训练过程都是在这一行完成的:

history = model.fit(trainX, trainY, epochs=1, batch_size=minibatch_size, validation_data=(valX, valY), verbose=0)

有没有办法在低层改变fit方法?

【问题讨论】:

    标签: keras imbalanced-data


    【解决方案1】:

    你可以尝试使用keras的class_weight参数。

    来自 keras 文档:

    class_weight:将类索引(整数)映射到权重(浮点)值的可选字典,用于加权损失函数(仅在训练期间)。

    在不平衡数据中使用它的示例: https://www.tensorflow.org/tutorials/structured_data/imbalanced_data#class_weights

    class_weights={"class_1": 1, "class_2": 10}
    history = model.fit(trainX, trainY, epochs=1, batch_size=minibatch_size, validation_data=(valX, valY), verbose=0, class_weight=class_weights)
    

    完整示例:

    # Examine the class label imbalance
    # you can use your_df['label_class_column'] or just the trainY values.
    neg, pos = np.bincount(your_df['label_class_column'])
    total = neg + pos
    print('Examples:\n    Total: {}\n    Positive: {} ({:.2f}% of total)\n'.format(
        total, pos, 100 * pos / total))
    
    # Scaling by total/2 helps keep the loss to a similar magnitude.
    # The sum of the weights of all examples stays the same.
    weight_for_0 = (1 / neg)*(total)/2.0 
    weight_for_1 = (1 / pos)*(total)/2.0
    
    class_weight = {0: weight_for_0, 1: weight_for_1}
    

    【讨论】:

    • 谢谢尼夫,我仍然有一些问题,因为我试图教的网络只有一个输出,不同的类是二进制的。我已尝试按原样添加此内容,但没有成功。
    • 它在二进制情况下工作正常。我添加了一个完整的例子。
    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 2020-10-22
    • 2019-06-11
    • 2021-09-26
    • 2019-04-01
    • 2021-08-25
    • 2020-11-08
    • 2021-08-29
    相关资源
    最近更新 更多