keras - 在小批量内改变学习率 - 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)
有没有办法在低级别更改拟合方法?
解决方案
您可以尝试使用 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}
推荐阅读
- java - 我有一个形状不同的框架,我想移动它,但到目前为止只有一个移动
- firebase - Chrome 扩展程序不允许来自 firebase 的外部脚本
- node.js - DynamoDB 异常 - 提供的 AttributeValue 设置了多个数据类型
- prolog - 序言。为变量赋值后,程序输出从 `yes` 变为 `no`
- anaconda - 将 Python 解释器设置为新环境后无法启动 spyder 内核
- c++ - 什么是类的静态对象,在类定义中,调用类的静态成员函数有哪些不同的方法?
- python - 我无法使用 vscode 运行 jupyter
- javascript - 使用给定用户 ID 的 Gmail API 身份验证
- ruby - 哪个更有效:respond_to?还是什么都不做的方法?
- vba - 每个循环的 VBA