Quickstart
ND4J Key features and brief samples.
Introduction
ND4J is a scientific computing library for the JVM. It is meant to be used in production environments rather than as a research tool, which means routines are designed to run fast with minimum RAM requirements. The main features are:
A versatile n-dimensional array object.
Linear algebra and signal processing functions.
Multiplatform functionality including GPUs.
all major operating systems: win/linux/osx/android.
architectures: x86, arm, ppc.
This quickstart follows the same layout and approach of the Numpy quickstart. This should help people familiar with Python and Numpy get started quickly with Nd4J.
Prerequisites
You can use Nd4J from any JVM Language. (For example: Scala, Kotlin). You can use Nd4J with any build tool. The sample code in this quick start uses the following:
Java (developer version) 1.7 or later (Only 64-Bit versions supported)
Apache Maven (automated build and dependency manager)
Git (distributed version control system)
To improve readability we show you the output of System.out.println(...). But we have not show the print statement in the sample code. If you are confident you know how to use maven and git, please feel free to skip to the Basics. In the remainder of this section we will build a small 'hello ND4J' application to verify the prequisites are set up correctly.
Execute the following commands to get the project from github.
When everything is set up correctly you should see the following output:
Basics
The main feature of Nd4j is the versatile n-dimensional array interface called INDArray. To improve performance Nd4j uses off-heap memory to store data. The INDArray is different from standard Java arrays.
Some of the key properties and methods for an INDArray x are as follows:
Array Creation
To create INDArrays you use the static factory methods of the Nd4j class.
The Nd4j.createFromArray function is overloaded to make it easy to create INDArrays from regular Java arrays. The example below uses Java double arrays. Similar create methods are overloaded for float, int and long. The Nd4j.createFromArray function has overloads up to 4d for all types.
Nd4j can create arrays initialized with zeros and ones using the functions zeros and ones. The rand function allows you to create an array initialized with random values. The default datatype of the INDArray created is float. Some overloads allow you to set the datatype.
Use the arange functions to create an array of evenly spaces values:
The linspace function allows you to specify the number of points generated:
Printing Arrays
The INDArray supports Java's toString() method. The current implementation has limited precision and a limited number of elements. The output is similar to printing NumPy arrays:
Basic Operations
You will have to use INDArray methods to perform operations on your arrays. There are in-place and copy overloads and scalar and element wise overloaded versions. The in-place operators return a reference to the array so you can conveniently chain operations together. Use in-place operators where possible to improve performance. Copy operators have new array creation overhead.
addition: arr.add(...), arr.addi(...) substraction: arr.sub(...), arr.subi(...) multiplication: arr.mul(...), arr.muli(...) division: arr.div(...), arr.divi(...)
When you perform the basic operations you must make sure the underlying data types are the same.
The INDArray has methods implementing reduction/accumulation operations such as sum, min, max.
Provide a dimension argument to apply the operation across the specified dimension:
Transform operation
Nd4j provides familiar mathematical functions such as sin, cos, and exp. These are called transform operations. The result is returned as an INDArray.
You can check out a complete list of transform operations in the Javadoc
Matrix multiplication
We have already seen the element wise multiplcation in the basic operations. The other Matrix operations have their own methods:
Indexing, Slicing and Iterating
Indexing, Slicing and Iterating is harder in Java than in Python. To retreive individual values from an INDArray you can use the getDouble, getFloat or getInt methods. INDArrays cannot be indexed like Java arrays. You can get a Java array from an INDArray using toDoubleVector(), toDoubleMatrix(), toFloatVector() and toFloatMatrix()
For multidimensional arrays you should use INDArray.get(NDArrayIndex...). The example below shows how to iterate over the rows and columns of a 2D array. Note that for 2D arrays we could have used the getColumn and getRow convenience methods.
Shape Manipulation
Changing the shape of an array
The number of elements along each axis is in the shape. The shape can be changed with various methods,.
Stacking together different arrays
Arrays can be stacked together using the vstack and hstack methods.
Copies and View
When working with INDArrays the data is not always copied. Here are three cases you should be aware of.
No Copy at All
Simple assignments make no copy of the data. Java passes objects by reference. No copies are made on a method call.
View or Shallow Copy
Some functions will return a view of an array.
Deep Copy
To make a copy of the array use the dup method. This will give you a new array with new data.
Functions and Methods Overview
Array Creation
arange, create, copy, empty, empty_like, eye, linspace, meshgrid, ones, ones_like, rand, readTxt, zeros, zeros_like
Conversions
convertToDoubles, convertToFloats, convertToHalfs
Manipulations
concatenate, hstack, ravel, repeat, reshape, squeeze, swapaxes, tear, transpose, vstack
Ordering
Operations
choice, cumsum, mmul, prod, put, putWhere, sum
Basic Statistics
covarianceMatrix, mean, std, var
Basic Linear Algebra
Last updated
Was this helpful?