我有一个包含15个不平衡类的数据集,并试图用keras进行多标签分类
我试着用微F-1分数作为衡量标准
我的模型:
# Create a VGG instance
model_vgg = tf.keras.applications.VGG19(weights = 'imagenet', pooling = 'max', include_top = False,
input_shape = (512, 512, 3))
# Freeze the layers which you don't want to train.
for layer in model_vgg.layers[:-5]:
layer.trainable = False
# Adding custom Layers
x = model_vgg.output
x = Flatten()(x)
x = Dense(1024, activation = "relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation = "relu")(x)
predictions = Dense(15, activation = "sigmoid")(x)
# creating the final model
model_vgg_final = Model(model_vgg.input, predictions)
# Print the summary
model_vgg_final.summary()
对于F1分数,我使用来自this question的自定义指标
from keras import backend as K
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
在编译模型时,我使用了二进制交叉熵和自定义F-1
# Compile a model
model_vgg_final.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = [f1])
我监控F-1是否提前停止
# Early stopping
early_stopping = EarlyStopping(monitor = 'f1', patience = 5)
# Training the model
history_vgg = model_vgg_final.fit(train_generator, steps_per_epoch = 10, epochs = 30, verbose = 1,
callbacks = [early_stopping], validation_data = valid_generator)
如何更新此自定义函数并获得micro F-1作为度量?我也很感激关于我的方法的建议
在scikit-learn documentation中有信息,但不确定如何将其合并到keras中
好问题
您在那里提供的链接指向在旧版本的Keras中如何计算指标(请注意,简短的解释)。问题是,在旧的Keras(1.X)中,度量是按批计算的,这当然会导致不正确的全局结果。在keras2.X中,内置的it度量被删除
但是,您的问题有解决方案
2.x
:How to get other metrics in Tensorflow 2.0 (not only accuracy)?中工作tensorflow-addons
>pip install tensorflow-addons
TensorFlow addons是一个非常好的包,它包含了基本TensorFlow包中不可用的多种功能和特性。这里,F1Score
是一个内置的度量,因此您可以直接使用它李>例如:
请注意“
micro
”参数的用法,它实际上正好代表您想要的内容,即microf1-score
相关问题 更多 >
编程相关推荐