Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[clojure]: add comp-metric based on CompositeEvalMetric #14553

Merged
merged 2 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(ns org.apache.clojure-mxnet.eval-metric
(:refer-clojure :exclude [get update])
(:require [org.apache.clojure-mxnet.util :as util])
(:import (org.apache.mxnet Accuracy TopKAccuracy F1 Perplexity MAE MSE RMSE CustomMetric)))
(:import (org.apache.mxnet Accuracy TopKAccuracy F1 Perplexity MAE MSE RMSE CustomMetric CompositeEvalMetric)))

(defn accuracy
"Basic Accuracy Metric"
Expand Down Expand Up @@ -74,11 +74,21 @@
[f-eval mname]
`(new CustomMetric (util/scala-fn ~f-eval) ~mname))

(defn comp-metric
"Create a metric instance composed out of several metrics"
[metrics]
(let [cm (CompositeEvalMetric.)]
(doseq [m metrics] (.add cm m))
cm))

(defn get
"Get the values of the metric in a vector form (name and value)"
"Get the values of the metric in as a map of {name value} pairs"
[metric]
(let [[[mname] [mvalue]] (util/tuple->vec (.get metric))]
[mname mvalue]))
(let [m (apply zipmap (-> (.get metric)
util/tuple->vec))]
(if-not (instance? CompositeEvalMetric metric)
(first m)
m)))

(defn reset
"clear the internal statistics to an initial state"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@
"my-metric")]
(eval-metric/update metric [(ndarray/ones [2])] [(ndarray/ones [2])])
(is (= ["my-metric" 0.0] (eval-metric/get metric)))))

(deftest test-comp-metric
(let [metric (eval-metric/comp-metric [(eval-metric/accuracy)
(eval-metric/f1)
(eval-metric/top-k-accuracy 2)])]
(eval-metric/update metric [(ndarray/ones [2])] [(ndarray/ones [2 3])])
(is (= {"accuracy" 0.0
"f1" 0.0
"top_k_accuracy" 1.0} (eval-metric/get metric)))))