ソースを参照

Explicitly return NaN when there is no prediction or gold

Summary: When the `Meter` class didn't see any prediction (resp. gold) of a specific label, `precision` (resp. `recall`) function's behaviour was undefined. This commit makes it explicitely return a NaN.

Reviewed By: EdouardGrave

Differential Revision: D14912163

fbshipit-source-id: fa6a8b48b931df7074f4d20bc2cf9beed99a008c
Onur Çelebi 6 年 前
コミット
157e80e577
1 ファイル変更9 行追加0 行削除
  1. 9 0
      src/meter.h

+ 9 - 0
src/meter.h

@@ -26,12 +26,21 @@ class Meter {
     Metrics() : gold(0), predicted(0), predictedGold(0) {}
 
     double precision() const {
+      if (predicted == 0) {
+        return std::numeric_limits<double>::quiet_NaN();
+      }
       return predictedGold / double(predicted);
     }
     double recall() const {
+      if (gold == 0) {
+        return std::numeric_limits<double>::quiet_NaN();
+      }
       return predictedGold / double(gold);
     }
     double f1Score() const {
+      if (predicted + gold == 0) {
+        return std::numeric_limits<double>::quiet_NaN();
+      }
       return 2 * predictedGold / double(predicted + gold);
     }
   };