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

Commit

Permalink
[scala] EvalMetric sumMetric is now a Double instead of a Float (#8297)
Browse files Browse the repository at this point in the history
When the difference in magnitude between the total
accuracy and 1 becomes too big and accuracy is not updated anymore due
to the low precision of float numbers.
  • Loading branch information
benqua authored and yzhliu committed Nov 22, 2017
1 parent ec6144f commit 8df20a2
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions scala-package/core/src/main/scala/ml/dmlc/mxnet/EvalMetric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import scala.collection.mutable.ArrayBuffer
abstract class EvalMetric(protected val name: String) {

protected var numInst: Int = 0
protected var sumMetric: Float = 0.0f
protected var sumMetric: Double = 0.0d

/**
* Update the internal evaluation.
Expand All @@ -41,7 +41,7 @@ abstract class EvalMetric(protected val name: String) {
*/
def reset(): Unit = {
this.numInst = 0
this.sumMetric = 0.0f
this.sumMetric = 0.0d
}

/**
Expand All @@ -50,7 +50,7 @@ abstract class EvalMetric(protected val name: String) {
* value, Value of the evaluation
*/
def get: (Array[String], Array[Float]) = {
(Array(this.name), Array(this.sumMetric / this.numInst))
(Array(this.name), Array((this.sumMetric / this.numInst).toFloat))
}
}

Expand Down Expand Up @@ -111,11 +111,10 @@ class Accuracy extends EvalMetric("accuracy") {
require(label.shape == predLabel.shape,
s"label ${label.shape} and prediction ${predLabel.shape}" +
s"should have the same length.")
for ((labelElem, predElem) <- label.toArray zip predLabel.toArray) {
if (labelElem == predElem) {
this.sumMetric += 1
}
}

this.sumMetric += label.toArray.zip(predLabel.toArray)
.filter{ case (labelElem: Float, predElem: Float) => labelElem == predElem }
.size
this.numInst += predLabel.shape(0)
predLabel.dispose()
}
Expand Down

0 comments on commit 8df20a2

Please sign in to comment.