ND4J and DL4J exploration

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:-

Creating NDArrays

Before we begin with creating an NDArray, let's see some concepts you should be familiar with:

  • The rank of an NDArray is the number of dimensions. 2d NDArrays have a rank of 2, 3d arrays have a rank of 3, and so on. You can create NDArrays with any arbitrary rank.
  • The shape of an NDArray defines the size of each of the dimensions. Suppose we have a 2d array with 3 rows and 5 columns. This NDArray would have shape [3,5].
  • The length of an NDArray defines the total number of elements in the array. The length is always equal to the product of the values that make up the shape.
  • The stride of an NDArray is defined as the separation (in the underlying data buffer) of contiguous elements in each dimension. Stride is defined per dimension, so a rank N NDArray has N stride values, one for each dimension. Note that most of the time, you don’t need to know (or concern yourself with) the stride - just be aware that this is how ND4J operates internally.

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);


Nd4j.zeros(nRows, nColumns)
[[0.00,  0.00,  0.00,  0.00,  0.00],  
 [0.00,  0.00,  0.00,  0.00,  0.00],  
 [0.00,  0.00,  0.00,  0.00,  0.00]]


Nd4j.ones(nRows, nColumns)
[[1.00,  1.00,  1.00,  1.00,  1.00],  
 [1.00,  1.00,  1.00,  1.00,  1.00],  
 [1.00,  1.00,  1.00,  1.00,  1.00]]


Nd4j.valueArrayOf(nRows, nColumns, 10.0)
[[10.00,  10.00,  10.00,  10.00,  10.00],  
 [10.00,  10.00,  10.00,  10.00,  10.00],  
 [10.00,  10.00,  10.00,  10.00,  10.00]]


INDArray defined from double[][]:
[[1.00,  2.00,  3.00],  
 [4.00,  5.00,  6.00]]


Uniform random array:
[[0.62,  0.39,  0.59,  0.29,  0.59],  
 [0.35,  0.13,  0.05,  0.67,  0.71],  
 [0.70,  0.48,  0.59,  0.90,  0.23]]


Creating INDArrays from other INDArrays, using hstack and vstack:
vStack:
[[0.00,  0.00,  0.00,  0.00,  0.00],  
 [0.00,  0.00,  0.00,  0.00,  0.00],  
 [0.00,  0.00,  0.00,  0.00,  0.00],  
 [1.00,  1.00,  1.00,  1.00,  1.00],  
 [1.00,  1.00,  1.00,  1.00,  1.00],  
 [1.00,  1.00,  1.00,  1.00,  1.00]]
hStack:
[[0.00,  0.00,  0.00,  0.00,  0.00,  1.00,  1.00,  1.00,  1.00,  1.00],  
 [0.00,  0.00,  0.00,  0.00,  0.00,  1.00,  1.00,  1.00,  1.00,  1.00],  
 [0.00,  0.00,  0.00,  0.00,  0.00,  1.00,  1.00,  1.00,  1.00,  1.00]]


Nd4j.eye(3):
[[1.00,  0.00,  0.00],  
 [0.00,  1.00,  0.00],  
 [0.00,  0.00,  1.00]]
Nd4j.linspace(1,10,10):
[1.00,  2.00,  3.00,  4.00,  5.00,  6.00,  7.00,  8.00,  9.00,  10.00]
Out[5]:
null

In [ ]: