Keras自定义F1分数和手动计算的F1分数给出了完全不同的结果

时间:2018-09-17 04:57:29

标签: python python-3.x machine-learning keras deep-learning

我按照此链接How to calculate F1 Macro in Keras?计算了我的二进制数据的F1分数。

在评估和预测完成后,我还编写了自己的代码来计算F1分数。 我自己的代码是

model = VGG.build(80, 80, 6)
model.load_weights('/home/matlabclient01/Documents/Warfana/Final/models_6/m13.h5')
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=[f1])

predY = model.predict(data, batch_size=1000, verbose=1)
y_classes = predY.argmax(axis=-1)    #y_classes contain predictions

#data1 contain actual labels
temp = data1
temp = np.array(temp)
results = y_classes == temp

TP = TN = FP = FN = 0

if len(y_classes) == len(temp):
    for i in range(len(y_classes)):
        if y_classes[i] == temp[i] and temp[i] == 1:
            TP=TP+1
        elif y_classes[i] == temp[i] and temp[i] == 0:
            TN=TN+1
        elif y_classes[i] != temp[i] and temp[i] == 1:
            FN=FN+1
        elif y_classes[i] != temp[i] and temp[i] == 0:
            FP=FP+1

print("TP = ",TP)
print("FP = ",FP)
print("TN = ",TN)
print("FN = ",FN)

precision = TP/(len(y_classes[y_classes==1]))
recall = TP/(len(temp[temp==1]))
fmeasure = (2*precision*recall)/(precision+recall)

print("FMeasure = ",fmeasure)

这些是不同的结果。 Keras Custom Formula and my own code's Results

结果:

第一个F度量 = 0.822(Keras自定义公式的结果)

最后一次测量 = 0.31(来自我自己的代码的结果)

0 个答案:

没有答案