改变小批量内的学习率 - keras
Change learning rate within minibatch - 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:可选字典映射 class 索引(整数)到权重(浮点)值,用于加权损失函数(仅在训练期间)。
在不平衡数据中使用它的例子:
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}
我遇到标签不平衡的问题,例如 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:可选字典映射 class 索引(整数)到权重(浮点)值,用于加权损失函数(仅在训练期间)。
在不平衡数据中使用它的例子: 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}