keras中模型训练class_weight,sample_weight区别说明keras 中fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0,
steps_per_epoch=None, validation_steps=None)
官⽅⽂档中:
weigh weight 区别class_weight:字典,将不同的类别映射为不同的权值,该参数⽤来在训练过程中调整损失函数(只能⽤于训练)。该参数在处理⾮平衡的训练数据(某些类的训练样本数很少)时,可以使得损失函数对样本数不⾜的数据更加关注。
sample_weight:权值的numpy array,⽤于在训练时调整损失函数(仅⽤于训练)。可以传递⼀个1D的与样本等长的向量⽤于对样本进⾏1对1的加权,或者在⾯对时序数据时,传递⼀个的形式为(samples,sequence_length)的矩阵来为每个时间步上的样本赋不同的权。这种情况下请确定在编译模型时添加了sample_weight_mode='temporal'。
class_weight---主要针对的上数据不均衡问题,⽐如:异常检测的⼆项分类问题,异常数据仅占1%,正常数据占99%; 此时就要设置不同类对loss的影响。
sample_weigh---主要解决的是样本质量不同的问题,⽐如前1000个样本的可信度,那么它的权重就要⾼,后1000个样本可能有错、不可信,那么权重就要调低。
补充知识:Keras 中数据不均衡时,metrics,class_weight的设置⽅法
当数据处理不均衡时,⽐如处理癌症训练问题,有病样本很少,参考:
主要从两个⽅⾯着⼿:
⼀、loss函数的权重问题
训练时,设置的权重:
class_weight={
1: n_non_cancer_samples / n_cancer_samples * t
}
⼆、编译时设置模型的metrics
def sensitivity(y_true, y_pred):
true_positives = K.und(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.und(K.clip(y_true, 0, 1)))
return true_positives / (possible_positives + K.epsilon())
def specificity(y_true, y_pred):
true_negatives = K.und(K.clip((1-y_true) * (1-y_pred), 0, 1)))
possible_negatives = K.und(K.clip(1-y_true, 0, 1)))
return true_negatives / (possible_negatives + K.epsilon())
modelpile(
loss='binary_crossentropy',
optimizer=RMSprop(0.001),
metrics=[sensitivity, specificity]
)
以上这篇keras中模型训练class_weight,sample_weight区别说明就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。