我正在Keras中使用DenseNet进行二进制分类。
创建加权类:
# Assign weights
weight_for_0 = num_normal/(num_normal + num_covid)
weight_for_1 = num_covid/(num_normal + num_covid)
class_weight = {0: weight_for_0, 1: weight_for_1}
# Print
print(f"Weight for class 0: {weight_for_0:.2f}")
print(f"Weight for class 1: {weight_for_1:.2f}")
因此,我有
Weight for class 0: 0.74
Weight for class 1: 0.26
我给模型安装了class_weight
history_dense201_weighted = model_dense_201.fit_generator(train_generator, epochs = 20,
validation_data = valid_generator, class_weight = class_weight, callbacks = [# mcp_save,
early_stopping, tensorboard_callback])
但当我想评估模型时,我不确定如何评估加权模型,因为class_weight
是历史的一部分。
如何更新此代码,使用加权模型代替默认的model_dense_201
模型?
# Evaluation
evaluation = model_dense_201.evaluate(valid_generator)
print(f"Validation Accuracy: {evaluation[1] * 100:.2f}%")
evaluation = model_dense_201.evaluate(train_generator)
print(f"Train Accuracy: {evaluation[1] * 100:.2f}%")
找到了这个。
https://github.com/tensorflow/tensorflow/issues/35825
引用Francois(又名Chollet(:
"我们在评估中不支持类权重的原因是,class_weight参数表示根据标签计算的样本权重,但标签不应在评估期间作为模型的输入。在培训期间,这是可以的,但在评估期间,这代表着从标签到指标的数据泄漏。如果你在评估中使用了类别权重,你的分数将无法在真实的测试数据上重现(当你没有标签时(。
所以这在概念上是错误的">