tensorflow - 如何增加 Keras 中的数据训练偏差?
问题描述
我目前正在使用大约 15k 图像(50% 好和坏)使用 Keras 进行二进制模型训练。但是,我的坏数据集是有限的。所以我添加了增强。尽管如此,我还是想强制模型将即使是好的也识别为坏的,如果它是轻微的或接近坏的。
Y_train = train_generator.classes
from sklearn.utils import class_weight
class_weight = class_weight.compute_class_weight('balanced'
,np.unique(Y_train)
,Y_train)
class_weight
class_weight = dict(zip(np.unique(Y_train), class_weight))
class_weight
输出 :
{0: 1.0015690376569037, 1: 0.9984358706986444}
我想对坏数据集进行更多的训练,而不是对好的数据集进行更多的训练。是否可以将类(0 - 差)权重增加到 10?
训练 :
print(colored('Training initiaited. please wait........', 'blue',))
model.fit_generator(train_generator,
epochs = epochs,
validation_data = validation_generator,
class_weight = class_weight,
steps_per_epoch=int(train_generator.samples/batch_size),
callbacks=callbacks_list,
validation_steps = int(validation_generator.samples/batch_size)
)
强制模型在不良数据集上进行更多训练的最佳方法是什么?(如果这不是最好的方法(不幸的是我没有任何不好的数据,但我确实有很多好的数据点))
遵循“平衡”有什么办法吗?
class_weight.compute_class_weight('balanced'
,np.unique(Y_train)
,Y_train)
解决方案
对误报进行更严格限制的一种简单方法是在分类期间增加阈值。即,假设您的模型抛出 的输出0.65
,同时为图像类做出决定,这通常类似于
threshold = 0.5
if output<threshold:
print("Class 0")
else:
print("Class 1")
它的输出是class 0
通过增加阈值来表示0.80
您对真正的肯定有更严格的限制。仅当您的模型抛出与类概率相对应的输出时,上述内容才有用。
But the correct way would be to choose the correct metric, for example in your case going with precision is a better option.
推荐阅读
- google-apps-script - Google Apps 脚本年错误
- c# - 找不到 Web API 路由?
- php - 执行 SELECT 语句后统计特定表结果
- django - 使用静态我可以加载一个 css 文件,但在同一目录中加载另一个 css 文件时遇到问题
- javascript - 使用 onClick 事件处理程序时不断调用自身
- javascript - 增加两个索引而不是一个
- sql - 如何在 DB2 中找到两个额外的字符并在一列中列出这些字符?
- pandas - 如何在 pandas 数据框上使用 sagemaker 进行预测
- graph - 关于深度优先搜索和生成森林的问题
- html - 在叠加层上使文本变白