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

Commit

Permalink
[clojure]: add comp-metric based on CompositeEvalMetric (#14553)
Browse files Browse the repository at this point in the history
* [clojure]: add comp-metric based on CompositeEvalMetric

* [clojure]: add comp-metric test
  • Loading branch information
tolitius authored and nswamy committed Apr 5, 2019
1 parent a3c11da commit 0f0b2dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
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)))))

0 comments on commit 0f0b2dd

Please sign in to comment.