Language: Java
ML/AI / Numerical Computing
ND4J was created to bring NumPy-like capabilities to Java. It allows Java developers to perform vectorized operations, matrix computations, and linear algebra efficiently, both on CPU and GPU. ND4J is commonly used for deep learning, data analysis, and scientific computing within the Java ecosystem.
ND4J (N-Dimensional Arrays for Java) is a scientific computing library for the JVM, providing high-performance n-dimensional arrays, linear algebra operations, and GPU acceleration. It serves as the foundation for Deeplearning4j and other Java-based ML frameworks.
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-M2.1</version>
</dependency>implementation 'org.nd4j:nd4j-native-platform:1.0.0-M2.1'ND4J provides Nd4j class for creating and manipulating n-dimensional arrays (INDArray), supports element-wise operations, linear algebra, broadcasting, reductions, and integration with deep learning frameworks like Deeplearning4j.
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.api.ndarray.INDArray;
INDArray array = Nd4j.create(new double[]{1, 2, 3, 4});
System.out.println(array);Creates a 1D n-dimensional array and prints its contents.
INDArray a = Nd4j.create(new double[]{1,2,3});
INDArray b = Nd4j.create(new double[]{4,5,6});
INDArray c = a.add(b);
System.out.println(c);Performs element-wise addition of two NDArrays.
INDArray mat1 = Nd4j.create(new double[][]{{1,2},{3,4}});
INDArray mat2 = Nd4j.create(new double[][]{{5,6},{7,8}});
INDArray result = mat1.mmul(mat2);
System.out.println(result);Performs matrix multiplication using ND4J's `mmul` function.
INDArray mat = Nd4j.create(new double[][]{{1,2,3},{4,5,6}});
INDArray addVec = Nd4j.create(new double[]{10,20,30});
INDArray result = mat.addRowVector(addVec);
System.out.println(result);Demonstrates broadcasting a 1D vector across rows of a 2D matrix.
INDArray arr = Nd4j.create(new double[]{1,2,3,4,5});
System.out.println(arr.meanNumber());
System.out.println(arr.stdNumber());Computes mean and standard deviation of an array.
// Use ND4J with CUDA backend for GPU computations
// Ensure nd4j-cuda-platform dependency is includedND4J can leverage GPU for faster computations on large matrices or deep learning tasks.
Use vectorized operations instead of loops for performance.
Leverage broadcasting for operations on arrays of different shapes.
Prefer Nd4j native platform or CUDA backend for large datasets or GPU acceleration.
Release unused NDArrays if memory usage is high.
Combine ND4J with Deeplearning4j for deep learning workflows.