Quickstart for Java using Maven
This is everything you need to run DL4J examples and begin your own projects.
We are currently reworking the Getting Started Guide.
Deeplearning4j started as a domain-specific language to configure deep neural networks, and evolved in to a suite of tools developers use to do everything from train models in java to deploy models to production.
You should have these installed to use this QuickStart guide. DL4J targets professional Java developers who are familiar with production deployments, IDEs and automated build tools. Working with DL4J will be easiest if you already have experience with these.
If you don't have Java 11 or later, download the current Java Development Kit (JDK) here. We recommend eclipse temurin or alternatives to oracle jdk. JDKs from other vendors such as Micorosft, Amazon, or Eclipse come prebuilt free of charge. To check if you have a compatible version of Java installed, use the following command:
Please make sure you have a 64-Bit version of java installed, as you will see an error telling you
no jnind4j in java.library.pathif you decide to try to use a 32-Bit version instead. Make sure the JAVA_HOME environment variable is set. Please use jdk 11 or 17. 8 is not not officially supported anymore but will work.
Maven is a dependency management and automated build tool for Java projects. It works well with IDEs such as IntelliJ and lets you install DL4J project libraries easily. Install or update Maven to the latest release following their instructions for your system. To check if you have the most recent version of Maven installed, enter the following:
If you are working on a Mac, you can simply enter the following into the command line:
brew install maven
Maven is widely used among Java developers and it's pretty much mandatory for working with DL4J. If you come from a different background, and Maven is new to you, check out Apache's Maven overview and our introduction to Maven for non-Java programmers, which includes some additional troubleshooting tips. Other build tools such as Ivy and Gradle can also work, but we support Maven best.
$ git clone git://git.kernel.org/pub/scm/git/git.git
The latest version of Mac's Mojave OS breaks git, producing the following error message:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
This can be fixed by running:
- 1.Use the command line to enter the following:git clone https://github.com/eclipse/deeplearning4j-examples.git
- 2.Open IntelliJ and choose Import Project. Then select the
- 3.Choose 'Import project from external model' and ensure that Maven is selected.
- 4.Continue through the wizard's options. Select the SDK that begins with
jdk. (You may need to click on a plus sign to see your options...) Then click Finish. Wait a moment for IntelliJ to download all the dependencies. You'll see the horizontal bar working on the lower right.
- 5.Pick an example from the file tree on the left. Right-click the file to run.
The example repository contains multiple example projects that are grouped by different levels of functionality. The
dl4j-examplesproject you just opened has the simplest examples, but feel free to explore the other projects too!
To run DL4J in your own projects, we highly recommend using Maven for Java users, or a tool such as SBT for Scala. The basic set of dependencies and their versions are shown below. This includes:
deeplearning4j-core, which contains the neural network implementations
nd4j-native-platform, the CPU version of the ND4J library that powers DL4J
datavec-api- Datavec is our library vectorizing and loading data
Within IntelliJ, you will need to choose the first Deeplearning4j example you're going to run. We suggest
MLPClassifierLinear, as you will almost immediately see the network classify two groups of data in our UI. The file on Github can be found here.
To run the example, right click on it and select the green button in the drop-down menu. You will see, in IntelliJ's bottom window, a series of scores. The rightmost number is the error score for the network's classifications. If your network is learning, then that number will decrease over time with each batch it processes. At the end, this window will tell you how accurate your neural-network model has become:
In another window, a graph will appear, showing you how the multilayer perceptron (MLP) has classified the data in the example. It will look like this:
Congratulations! You just trained your first neural network with Deeplearning4j.
Python folks: If you plan to run benchmarks on Deeplearning4j comparing it to well-known Python framework [x], please read these instructions on how to optimize heap space, garbage collection and ETL on the JVM. By following them, you will see at least a 10x speedup in training time.
Q: I'm using a 64-Bit Java on Windows and still get the
no jnind4j in java.library.patherror
A: You may have incompatible DLLs on your PATH. To tell DL4J to ignore those, you have to add the following as a VM parameter (Run -> Edit Configurations -> VM Options in IntelliJ):
Q: SPARK ISSUES I am running the examples and having issues with the Spark based examples such as distributed training or datavec transform options.
Windows users might be seeing something like:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException: Please ensure that you have an nd4j backend on your classpath. Please see: http://nd4j.org/getstarted.html
... 2 more
Caused by: org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException: Please ensure that you have an nd4j backend on your classpath. Please see: http://nd4j.org/getstarted.html
... 3 more
Now that you've learned how to run the different examples, we've made a template available for you that has a basic MNIST trainer with simple evaluation code.
To use the template:
- 1.Copy the
standalone-sample-projectfrom the examples and give it the name of your project.
- 2.Import the folder into IntelliJ.
- 3.Start coding!
Deeplearning4j is a framework that lets you pick and choose with everything available from the beginning. We're not Tensorflow (a low-level numerical computing library with automatic differentiation) or Pytorch. Deeplearning4j has several subprojects that make it easy-ish to build end-to-end applications.
If you want distributed training on Spark, you can see our Spark page. Keep in mind that we cannot setup Spark for you. If you want to set up distributed Spark and GPUs, that is largely up to you. Deeplearning4j simply deploys as a JAR file on an existing Spark cluster.
Deeplearning4j has two other notable components:
Deeplearning4j is meant to be an end-to-end platform for building real applications, not just a tensor library with automatic differentiation. If you want a tensor library with autodiff, please see ND4J and Samediff. Samediff is still in beta, but if you want to contribute, please join our community forum.