zachgk commented on a change in pull request #12690: [MXNET-1000] get Ndarray
real value and form it from a NDArray
URL: https://github.com/apache/incubator-mxnet/pull/12690#discussion_r247710541
##########
File path: scala-package/core/src/main/scala/org/apache/mxnet/NDArray.scala
##########
@@ -509,6 +510,57 @@ object NDArray extends NDArrayBase {
array(sourceArr, shape, null)
}
+ /**
+ * Create a new NDArray based on the structure of source Array
+ * @param sourceArr Array[Array...Array[Float]...]
+ * @param ctx context like to pass in
+ * @return an NDArray with the same shape of the input
+ */
+ def toNDArray(sourceArr: Array[_], ctx : Context = null) : NDArray = {
+ val shape = ArrayBuffer[Int]()
+ shapeGetter(sourceArr, shape, 0)
+ val finalArr = new Array[Float](shape.product)
+ arrayCombiner(sourceArr, finalArr, 0, finalArr.length - 1)
+ array(finalArr, Shape(shape), ctx)
+ }
+
+ private def shapeGetter(sourceArr : Any,
+ shape : ArrayBuffer[Int], shapeIdx : Int) : Unit = {
+ sourceArr match {
+ case arrFloat : Array[Float] => {
+ val arrLength = arrFloat.length
+ if (shape.length == shapeIdx) {
+ shape += arrLength
+ }
+ require(shape(shapeIdx) == arrLength, "Each Array should have equal
length")
+ }
+ case arr : Array[Any] => {
+ val arrLength = arr.length
+ if (shape.length == shapeIdx) {
+ shape += arrLength
+ }
+ require(shape(shapeIdx) == arrLength,
+ s"Each Array should have equal length, expected ${shape(shapeIdx)},
get $arrLength")
+ arr.foreach(ele => shapeGetter(ele, shape, shapeIdx + 1))
+ }
+ case _ => throw new IllegalArgumentException(s"Wrong type passed:
${sourceArr.getClass}")
+ }
+ }
+
+ private def arrayCombiner(sourceArr : Any, arr : Array[Float], start : Int,
end : Int) : Unit = {
+ sourceArr match {
+ case arrFloat : Array[Float] => {
Review comment:
Shouldn't this handle other types, specifically double now?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services