首页 > 解决方案 > 在小批量内改变学习率 - keras

问题描述

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

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

有什么办法吗?

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

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

有没有办法在低级别更改拟合方法?

标签: kerasimbalanced-data

解决方案


您可以尝试使用 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}

推荐阅读