首页 > 解决方案 > 如何增加 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)

标签: tensorflowmachine-learningkeras

解决方案


对误报进行更严格限制的一种简单方法是在分类期间增加阈值。即,假设您的模型抛出 的输出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.


推荐阅读