Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scala wrapping of INDArray class of ND4j
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/resources/ScalaSciExamples/deeplearning/wrappingINDArray.sssci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
|
||
// wrapping with more friendly user interface the class INDArray | ||
// this example runs only with Deeplearning4j version of ScalaLab, i.e. ScalaLabDL4j.jar | ||
class ND4jMat (x: RichDouble2DArray) { | ||
import org.nd4j.linalg.api.ndarray.INDArray | ||
import org.nd4j.linalg.factory.Nd4j | ||
import org.nd4j.linalg.util.ArrayUtil | ||
import java.util.Arrays | ||
|
||
|
||
var x1d = scalaSci.RichDouble2DArray.oneDDoubleArray(x) | ||
|
||
var data = Nd4j.create( x1d, Array( x.Nrows, x.Ncols) ) | ||
|
||
|
||
def this(xp: INDArray) = this(xp.toDoubleMatrix ) | ||
|
||
|
||
def + ( that: INDArray) = new ND4jMat(data.add(that)) | ||
|
||
|
||
def - ( that: INDArray) = new ND4jMat(data.sub(that)) | ||
|
||
|
||
|
||
def * ( that: INDArray) = new ND4jMat(data.mmul(that)) | ||
|
||
|
||
def + ( that: ND4jMat):ND4jMat = this + that.data | ||
|
||
|
||
def - ( that: ND4jMat):ND4jMat = this - that.data | ||
|
||
|
||
|
||
def * ( that: ND4jMat):ND4jMat = this * that.data | ||
|
||
//def - ( that: ND4jMat) = this.sub(that) | ||
// def * ( that: ND4jMat) = this.mmul(that) | ||
|
||
} | ||
|
||
|
||
|
||
var N=4000 | ||
|
||
var x = rand(N,N) | ||
|
||
var nx = new ND4jMat(x) | ||
|
||
|
||
tic | ||
var nxnx = nx * nx // multiply fast using ND4j | ||
var tm=toc |