Machine Learning in Java: Libraries and Tools
Although Python dominates the field of machine learning, Java remains a popular platform for building industrial ML solutions. High performance, strong typing, a rich ecosystem for working with big data (Hadoop, Spark), and widespread use in corporate environments make Java an excellent choice for deploying machine learning models into production. In this article, we will review the key libraries for machine learning in Java, their features, and usage examples.
1. Deeplearning4j (DL4J)
Deeplearning4j is the most popular library for deep learning in Java. It supports neural networks of various architectures: convolutional (CNN), recurrent (RNN), LSTM, as well as distributed training on clusters with Apache Spark and Hadoop.
Key features:
- Flexible API for building computation graphs (ComputationGraph, MultiLayerNetwork)
- Integration with Apache Spark for distributed training
- GPU support (CUDA) for accelerated computation
- Importing models from Keras and TensorFlow
- Built-in tools for data loading and preprocessing (DataVec)
Example: simple neural network for classification
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;import org.deeplearning4j.nn.conf.NeuralNetConfiguration;import org.deeplearning4j.nn.conf.layers.DenseLayer;import org.deeplearning4j.nn.conf.layers.OutputLayer;import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;import org.deeplearning4j.optimize.listeners.ScoreIterationListener;import org.nd4j.linalg.activations.Activation;import org.nd4j.linalg.dataset.DataSet;import org.nd4j.linalg.factory.Nd4j;import org.nd4j.linalg.lossfunctions.LossFunctions;
public class DL4JExample { public static void main(String[] args) { // Creating network configuration MultiLayerConfiguration config = new NeuralNetConfiguration.Builder() .seed(123) .updater(new org.nd4j.linalg.learning.config.Adam()) .list() .layer(0, new DenseLayer.Builder() .nIn(2).nOut(10) .activation(Activation.RELU) .build()) .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nIn(10).nOut(2) .activation(Activation.SOFTMAX) .build()) .build();
// Creating and training the network MultiLayerNetwork model = new MultiLayerNetwork(config); model.init(); model.setListeners(new ScoreIterationListener(10));
// Synthetic data: 1000 examples with 2 features DataSet data = new DataSet( Nd4j.rand(1000, 2), Nd4j.zeros(1000, 2).putColumn(0, Nd4j.rand(1000, 1).gt(0.5).castTo(org.nd4j.linalg.api.buffer.DataType.FLOAT)) );
for (int epoch = 0; epoch < 10; epoch++) { model.fit(data); }
System.out.println("Training complete!"); }}2. Smile (Statistical Machine Intelligence & Learning Engine)
Smile is a fast and multifunctional library covering a wide range of algorithms: from classical linear regression to modern ensemble methods and deep learning. Smile's distinguishing feature is high performance due to optimized Java implementations and a minimal number of dependencies.
Key features:
- Classification algorithms (SVM, Random Forest, KNN, Naive Bayes)
- Clustering (K-Means, DBSCAN, GMM)
- Dimensionality reduction (PCA, t-SNE, UMAP)
- Time series
Also in library
Caching in TypeScript: Redis, Memcached, and In-Memory Solutions
PDF Generation in Go: Library Overview and Practical Guide
ORM for PHP: Comparison of Doctrine, Eloquent, and Cycle — Code Examples
Timber for Kotlin: Complete Guide with Examples