Getting Started with Machine Learning in Java using Deeplearning4j
Introduction
Getting Started with Machine Learning in Java using Deeplearning4j

Image Source
Introduction
In recent years, machine learning has become increasingly popular as it provides solutions to various real-world problems. Java, a versatile programming language, can also be used to develop machine learning applications using the Deeplearning4j (DL4J) library. In this article, we will explore the basics of machine learning in Java using DL4J and walk through a simple example to demonstrate its capabilities.
Setting Up Deeplearning4j
To get started with DL4J, you need to set up your Java project. We recommend using Maven or Gradle as your build tool. In this tutorial, we will use Maven.
First, create a new Maven project and add the following dependencies in your pom.xml file:
<dependencies>
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-beta7</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-beta7</version>
</dependency>
</dependencies>
Preparing the Dataset
For this example, we will use the popular Iris dataset, which contains 150 samples of iris flowers with four features: sepal length, sepal width, petal length, and petal width. The dataset has three classes, each representing a type of iris flower: Iris Setosa, Iris Versicolor, and Iris Virginica.
We will use DL4J’s built-in dataset iterator to load the Iris dataset. Add the following code to your main method:
import org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
public static void main(String[] args) {
int batchSize = 10;
int numClasses = 3;
DataSetIterator iterator = new IrisDataSetIterator(batchSize, numClasses);
}
Creating a Neural Network Model
Now that we have our dataset, let’s create a simple neural network model. We will use a feedforward multilayer perceptron (MLP) with two hidden layers. Add the following code to your main method:
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
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.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.learning.config.Nesterovs;
import org.nd4j.linalg.lossfunctions.LossFunctions;
MultiLayerConfiguration configuration = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new Nesterovs(0.1, 0.9))
.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(10).activation(Activation.RELU).build())
.layer(1, new DenseLayer.Builder().nIn(10).nOut(10).activation(Activation.RELU).build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.activation(Activation.SOFTMAX)
.nIn(10)
.nOut(numClasses)
.build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(configuration);
model.init();
Training the Model
Now that we have our neural network model set up, we can train it using our Iris dataset. We will train the model for 100 epochs. Add the following code to your main method:
int numEpochs = 100;
for (int i = 0; i < numEpochs; i++) {
iterator.reset();
model.fit(iterator);
}
Evaluating the Model
After training the model, we need to evaluate its performance on the test data. For this, we will use DL4J’s built-in Evaluation class. Add the following code to your main method:
import org.deeplearning4j.eval.Evaluation;
import org.nd4j.linalg.dataset.DataSet;
iterator.reset();
Evaluation evaluation = new Evaluation(numClasses);
while (iterator.hasNext()) {
DataSet batch = iterator.next();
model.rnnClearPreviousState();
evaluation.eval(batch.getLabels(), model.output(batch.getFeatures()));
}
System.out.println(evaluation.stats());
Running the Application
You can now run your Java application. The output should display the evaluation statistics for the trained model, including accuracy, precision, recall, and F1 score.
Conclusion
We have introduced machine learning in Java using the Deeplearning4j library. We have demonstrated how to set up a project, load a dataset, create a simple neural network model, train the model, and evaluate its performance. With this foundation, you can further explore the capabilities of DL4J and apply it to more complex machine learning tasks.
메타데이터
- post_id
- 3a5dc47dbbf4
- slug
- getting-started-with-machine-learning-in-java-using-deeplearning4j-3a5dc47dbbf4
- url
- https://medium.com/@AlexanderObregon/getting-started-with-machine-learning-in-java-using-deeplearning4j-3a5dc47dbbf4
- canonical_url
- https://medium.com/@AlexanderObregon/getting-started-with-machine-learning-in-java-using-deeplearning4j-3a5dc47dbbf4
- author_url
- https://medium.com/@AlexanderObregon
- status
- ok
- fetched_at
- 2026-08-02 14:36:00