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

Fix relative difference scala #14417

Merged
merged 4 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -21,13 +21,13 @@ object CheckUtils {
def reldiff(a: NDArray, b: NDArray): Float = {
val diff = NDArray.sum(NDArray.abs(a - b)).toScalar
val norm = NDArray.sum(NDArray.abs(a)).toScalar
diff / norm
if (diff < Float.MinPositiveValue) diff else diff / norm
}

def reldiff(a: Array[Float], b: Array[Float]): Float = {
val diff =
(a zip b).map { case (aElem, bElem) => Math.abs(aElem - bElem) }.sum
val norm: Float = a.reduce(Math.abs(_) + Math.abs(_))
diff / norm
if (diff < Float.MinPositiveValue) diff else diff / norm
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ class NDArraySuite extends FunSuite with BeforeAndAfterAll with Matchers {
val result3 = 0f to stop by 1f
val range3 = NDArray.arange(stop)
assert(CheckUtils.reldiff(result3.toArray, range3.toArray) <= 1e-4f)

val stop4 = Math.abs(stop)
val step4 = stop4 + Math.abs(scala.util.Random.nextFloat())
val result4 = (0.0 until stop4.toDouble by step4.toDouble)
.flatMap(x => Array.fill[Float](repeat)(x.toFloat))
val range4 = NDArray.arange(stop4, step = step4, repeat = repeat)
assert(CheckUtils.reldiff(result4.toArray, range4.toArray) <= 1e-4f)
}
}

Expand Down