ND4J is a tensor & N-dimensional array scientific computing library built for the JVM and is part of the Deeplearning4j suite of software. This notebook serves as the user guide to understand the main functionality of ND4J. In this notebook, we'll explore its use-cases through numerous examples and then we'll discuss its usage in the project. So, let's begin with loading some essential JARs.
In [1]:
%classpath add mvn org.nd4j nd4j-native-platform 0.9.0
In [2]:
%classpath add mvn org.bytedeco javacpp 1.5
ND4J(N-Dimensions For Java) or – as its creators present it – the “numpy” for the JVM, allows the user to create and perform various mathematical operations on N-Dimensional arrays. Throughout this notebook, we’ll use the term NDArray to refer to the general concept of an n-dimensional array; the term INDArray refers specifically to the Java interface that ND4J defines. In practice, these two terms can be used interchangeably.
First, let's see how to create an NDArray:-
Before we begin with creating an NDArray, let's see some concepts you should be familiar with:
Now, let's see the different ways to create an NDArray:-
In [5]:
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import java.util.Arrays;
//First, we'll see how to create INDArrays with different scalar value initializations
int nRows = 3;
int nColumns = 5;
INDArray allZeros = Nd4j.zeros(nRows, nColumns);
System.out.println("Nd4j.zeros(nRows, nColumns)");
System.out.println(allZeros);
INDArray allOnes = Nd4j.ones(nRows, nColumns);
System.out.println("\n\nNd4j.ones(nRows, nColumns)");
System.out.println(allOnes);
INDArray allTens = Nd4j.valueArrayOf(nRows, nColumns, 10.0);
System.out.println("\n\nNd4j.valueArrayOf(nRows, nColumns, 10.0)");
System.out.println(allTens);
//We can also create INDArrays from double[] and double[][] (or, float/int etc Java arrays)
double[][] matrixDouble = new double[][]{
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0}};
INDArray matrix = Nd4j.create(matrixDouble);
System.out.println("\n\nINDArray defined from double[][]:");
System.out.println(matrix);
//It is also possible to create random INDArrays:
int[] shape = new int[]{nRows, nColumns};
INDArray uniformRandom = Nd4j.rand(shape);
System.out.println("\n\nUniform random array:");
System.out.println(uniformRandom);
//We can create INDArrays by combining other INDArrays, too:
INDArray vStack = Nd4j.vstack(allZeros, allOnes);//Vertical stack: [1,3]+[1,3] to [2,3]
INDArray hStack = Nd4j.hstack(allZeros, allOnes);//Horizontal stack: [1,3]+[1,3] to [1,6]
System.out.println("\n\nCreating INDArrays from other INDArrays, using hstack and vstack:");
System.out.println("vStack:\n" + vStack);
System.out.println("hStack:\n" + hStack);
//There's some other miscellaneous methods, too:
INDArray identityMatrix = Nd4j.eye(3);
System.out.println("\n\nNd4j.eye(3):\n" + identityMatrix);
INDArray linspace = Nd4j.linspace(1,10,10); //Values 1 to 10, in 10 steps
System.out.println("\n\nNd4j.linspace(1,10,10):\n" + linspace);
Out[5]:
In [ ]: