Repository: UKPLab/sentence-transformers
Issue category: Bug Report

Issue:
BinaryClassificationEvaluator returns np.float32() and np.float64()
I came across the problem where

```python
output_scores[similarity_fn_name] = {
  "accuracy": acc,
  "accuracy_threshold": acc_threshold,
  "f1": f1,
  "f1_threshold": f1_threshold,
  "precision": precision,
  "recall": recall,
  "ap": ap,
}
```

is returning outputs in `np.float32()` and `np.float64()` which cause problems during model saving, as the `json/encoder.py` sees those types as `not JSON serializable`.

By changing the code snippet to

```python
output_scores[similarity_fn_name] = {
  "accuracy": acc.item(),
  "accuracy_threshold": acc_threshold.item(),
  "f1": f1.item(),
  "f1_threshold": f1_threshold.item(),
  "precision": precision,
  "recall": recall.item(),
  "ap": ap.item(),
}
```

the elements get copied to a standard Python scalar each (note: `precision` already is) and then behave like expected during saving.


Hints:
Hello!

Well spotted, this is suboptimal. I'd rather have all of these converted to floats as expected. I'm tackling some other PRs right now, but I'll pick this up in a few days if someone else hasn't beat me to it by then.
Thanks for reporting!

- Tom Aarsen
ill be happy to work on this :)

Patch:
diff --git a/sentence_transformers/evaluation/BinaryClassificationEvaluator.py b/sentence_transformers/evaluation/BinaryClassificationEvaluator.py
index e0e00ebc9..4a701279d 100644
--- a/sentence_transformers/evaluation/BinaryClassificationEvaluator.py
+++ b/sentence_transformers/evaluation/BinaryClassificationEvaluator.py
@@ -288,14 +288,14 @@ def compute_metrices(self, model: SentenceTransformer) -> dict[str, dict[str, fl
             logger.info(f"Matthews Correlation with {name}: {mcc * 100:.2f}\n")
 
             output_scores[similarity_fn_name] = {
-                "accuracy": acc,
-                "accuracy_threshold": acc_threshold,
-                "f1": f1,
-                "f1_threshold": f1_threshold,
-                "precision": precision,
-                "recall": recall,
-                "ap": ap,
-                "mcc": mcc,
+                "accuracy": float(acc),
+                "accuracy_threshold": float(acc_threshold),
+                "f1": float(f1),
+                "f1_threshold": float(f1_threshold),
+                "precision": float(precision),
+                "recall": float(recall),
+                "ap": float(ap),
+                "mcc": float(mcc),
             }
 
         return output_scores


Test patch: