Machine Learning in Java: Overview of Key Libraries
Although Python dominates the world of machine learning, Java remains a strong player in corporate development. High performance, strong typing, and a vast ecosystem make Java indispensable for production systems where speed, scalability, and integration with existing services are important. In this article, we will review four leading libraries for ML in Java: Deeplearning4j, Smile, Weka, and MOA. Each addresses its own tasks — from deep learning to streaming data analysis.
1. Deeplearning4j (DL4J) — Deep Learning for Enterprise Applications
Deeplearning4j is a Swiss Army knife for neural networks in Java. The library supports all popular architectures: convolutional (CNN), recurrent (RNN), LSTM, transformers, and generative adversarial networks (GAN). DL4J integrates tightly with Apache Spark and Hadoop, allowing model training on clusters of thousands of nodes. For GPU computing, it uses ND4J — its own library for scientific computing built on top of CUDA.
Key features:
- Creating multilayer perceptrons and convolutional networks
- Support for transfer learning with pre-trained models
- Visualization of the training process via a monitoring server
- Working with text (Word2Vec, Doc2Vec) and images
Example: a simple neural network for classification
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;import org.deeplearning4j.nn.conf.NeuralNetConfiguration;import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;import org.nd4j.linalg.activations.Activation;import org.nd4j.linalg.lossfunctions.LossFunctions;
MultiLayerConfiguration config = new NeuralNetConfiguration.Builder() .seed(42) .activation(Activation.RELU) .list() .layer(0, new DenseLayer.Builder().nIn(784).nOut(128).build()) .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .activation(Activation.SOFTMAX) .nIn(128).nOut(10) .build()) .build();
MultiLayerNetwork model = new MultiLayerNetwork(config);model.init();// model.fit(trainingData);2. Smile — Statistical Machine Learning and Visualization
The Smile library (Statistical Machine Intelligence and Learning Engine) offers over 200 algorithms for classification, regression, clustering, and dimensionality reduction. Its main advantage is ease of use and a rich set of methods without the need to configure complex settings. Smile includes its own visualization system (Swing components) and supports model export to PMML.
Main algorithms:
- Random forest, gradient boosting, SVM
- K-means, DBSCAN, hierarchical clustering
- Principal Component Analysis (PCA), t-SNE, UMAP
- Linear and logistic regression
Example: classification using random forest
import smile.classification.RandomForest;import smile.data.DataFrame;import smile.data.vector.DoubleVector;import smile.io.Read;
// Loading data (CSV)DataFrame df = Read.csv("iris.csv");double[][] x = df.select(0, 1, 2, 3).toArray();int[] y = df.column("species").toIntArray();
// Training the modelRandomForest model = RandomForest.fit(x, y);
// Predictiondouble[] sample = {5.1, 3.5, 1.4, 0.2};int prediction = model.predict(sample);3. Weka — A Classic for Rapid Prototyping
Weka (Waikato Environment for Knowledge Analysis) is the oldest Java library for ML, developed at the University of Waikato. It is ideal for learning and experimentation: it has a graphical