Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deeplearning4j Tutorial

Introduction to Deeplearning4j

Deeplearning4j (DL4J) is a powerful, open-source, distributed deep learning library for the Java Virtual Machine (JVM). It is designed for business environments and integrates with Hadoop and Apache Spark. DL4J is particularly suited for building deep neural networks and offers a rich set of functionalities for machine learning tasks.

Setting Up Your Environment

To get started with Deeplearning4j, you need to set up your development environment. Ensure you have the following prerequisites:

  • Java Development Kit (JDK) 8 or newer
  • Maven for dependency management
  • An IDE (like IntelliJ IDEA or Eclipse)

Next, create a new Maven project and add the following dependencies to your pom.xml file:

Example pom.xml Dependency

<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>
                

Your First Neural Network

Let’s create a simple neural network to classify the MNIST dataset, which consists of handwritten digits. The following code outlines the steps to build and train a neural network:

Example Neural Network Code

import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.multilayer.MultiLayerConfiguration;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.learning.config.Adam;
import org.nd4j.linalg.lossfunctions.LossFunctions;

public class MnistExample {
    public static void main(String[] args) throws Exception {
        int numInputs = 784;
        int numOutputs = 10;
        int numHiddenNodes = 100;

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(123)
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                .updater(new Adam(0.001))
                .list()
                .layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes)
                        .activation(Activation.RELU)
                        .build())
                .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .activation(Activation.SOFTMAX)
                        .nIn(numHiddenNodes).nOut(numOutputs).build())
                .build();

        MultiLayerNetwork model = new MultiLayerNetwork(conf);
        model.init();

        DataSetIterator mnistTrain = new MnistDataSetIterator(128, true, 12345);
        model.fit(mnistTrain, 10);
    }
}
                

This code sets up a simple feedforward neural network with one hidden layer. It uses the MNIST dataset to train the model for digit classification.

Evaluating the Model

After training the model, it's crucial to evaluate its performance on unseen data. You can use the following code to evaluate the trained model:

Example Evaluation Code

import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.dataset.api.iterator.MnistDataSetIterator;

public class MnistEvaluation {
    public static void main(String[] args) throws Exception {
        MultiLayerNetwork model = ... // Load your trained model

        DataSetIterator mnistTest = new MnistDataSetIterator(128, false, 12345);
        Evaluation eval = model.evaluate(mnistTest);
        System.out.println(eval.stats());
    }
}
                

This code evaluates the model on the MNIST test dataset and prints out the evaluation statistics, including accuracy, precision, and recall.

Conclusion

In this tutorial, you learned how to set up Deeplearning4j, create a simple neural network, train it on the MNIST dataset, and evaluate its performance. Deeplearning4j offers many advanced features for building complex models, so feel free to explore the official documentation and experiment with different architectures and datasets.