deeplearning4j
Suite of tools for deploying and training deep learning models using the JVM. Highlights include model import for keras, tensorflow, and onnx/pytorch, a modular and tiny c++ library for running math code and a java based math library on top of the core c++ library. Also includes samediff: a pytorch/tensorflow like library for running deep learn...
Top Related Projects
An Open Source Machine Learning Framework for Everyone
Tensors and Dynamic neural networks in Python with strong GPU acceleration
scikit-learn: machine learning in Python
Deep Learning for humans
Apache Spark - A unified analytics engine for large-scale data processing
Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
Quick Overview
Deeplearning4j is an open-source, distributed deep learning library for the JVM. It is designed to be used in business environments on distributed GPUs and CPUs, integrating with Hadoop and Apache Spark. Deeplearning4j provides a comprehensive ecosystem for deep learning, including tools for data preprocessing, neural network construction, and model evaluation.
Pros
- Native integration with Java and Scala ecosystems
- Supports distributed computing for large-scale deep learning tasks
- Comprehensive suite of tools for end-to-end deep learning workflows
- Active community and commercial support available
Cons
- Steeper learning curve compared to Python-based deep learning libraries
- Less extensive documentation and tutorials compared to more popular frameworks
- Performance may not match specialized libraries in certain use cases
- Smaller ecosystem of pre-trained models compared to TensorFlow or PyTorch
Code Examples
- Creating a simple feedforward neural network:
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(123)
.activation(Activation.TANH)
.weightInit(WeightInit.XAVIER)
.updater(new Sgd(0.1))
.l2(1e-4)
.list()
.layer(new DenseLayer.Builder().nIn(3).nOut(3).build())
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.activation(Activation.SOFTMAX)
.nIn(3).nOut(3).build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
- Loading and preprocessing an image dataset:
ImageRecordReader recordReader = new ImageRecordReader(height, width, channels);
recordReader.initialize(new FileSplit(new File(dataPath)));
DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, batchSize, 1, outputNum);
DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
scaler.fit(dataIter);
dataIter.setPreProcessor(scaler);
- Training a convolutional neural network:
ComputationGraph model = new ComputationGraph(new NeuralNetConfiguration.Builder()
.seed(seed)
.updater(new Adam(5e-3))
.l2(1e-5)
.graphBuilder()
.addInputs("input")
.setInputTypes(InputType.convolutional(height, width, channels))
.addLayer("conv1", new ConvolutionLayer.Builder(5, 5)
.stride(1, 1)
.nOut(20)
.activation(Activation.RELU)
.build(), "input")
.addLayer("pool1", new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
.kernelSize(2, 2)
.stride(2, 2)
.build(), "conv1")
.addLayer("output", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.nOut(outputNum)
.activation(Activation.SOFTMAX)
.build(), "pool1")
.setOutputs("output")
.build());
model.init();
model.fit(trainIter, nEpochs);
Getting Started
- Add Deeplearning4j dependencies to your project's build file (Maven example):
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-M2</version>
</dependency>
- Create a simple neural network
Competitor Comparisons
An Open Source Machine Learning Framework for Everyone
Pros of TensorFlow
- Larger ecosystem and community support
- More comprehensive documentation and tutorials
- Better support for distributed computing and GPU acceleration
Cons of TensorFlow
- Steeper learning curve for beginners
- Can be slower for certain operations compared to DeepLearning4J
- Less native integration with Java ecosystems
Code Comparison
TensorFlow (Python):
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
DeepLearning4J (Java):
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.layer(new DenseLayer.Builder().nIn(784).nOut(64).activation("relu").build())
.layer(new DenseLayer.Builder().nOut(10).activation("softmax").build())
.build();
Both TensorFlow and DeepLearning4J are powerful deep learning frameworks, but they cater to different ecosystems and use cases. TensorFlow is more widely adopted and has a larger community, while DeepLearning4J offers better integration with Java-based systems and may be more suitable for enterprise environments.
Tensors and Dynamic neural networks in Python with strong GPU acceleration
Pros of PyTorch
- More extensive ecosystem and community support
- Dynamic computational graphs for easier debugging
- Pythonic syntax and intuitive API
Cons of PyTorch
- Steeper learning curve for beginners
- Less optimized for production deployment
- Limited support for mobile and edge devices
Code Comparison
PyTorch:
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
z = torch.add(x, y)
Deeplearning4j:
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
INDArray x = Nd4j.create(new float[]{1, 2, 3});
INDArray y = Nd4j.create(new float[]{4, 5, 6});
INDArray z = x.add(y);
PyTorch offers a more concise and Pythonic syntax, while Deeplearning4j provides a Java-based approach with explicit typing. PyTorch's dynamic computational graph allows for easier debugging and flexibility in model design. Deeplearning4j, being Java-based, integrates well with existing Java ecosystems and offers better performance for production deployments. PyTorch has a larger community and more extensive documentation, making it easier to find resources and solutions to problems. However, Deeplearning4j may be more suitable for enterprise Java environments and scenarios where Java integration is crucial.
scikit-learn: machine learning in Python
Pros of scikit-learn
- Extensive collection of machine learning algorithms and tools
- Well-documented and easy to use for beginners and experts alike
- Seamless integration with other Python scientific computing libraries
Cons of scikit-learn
- Limited support for deep learning and neural networks
- Not optimized for large-scale distributed computing
Code Comparison
scikit-learn:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=4)
clf = RandomForestClassifier()
clf.fit(X, y)
Deeplearning4j:
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
.layer(1, new OutputLayer.Builder().nIn(3).nOut(2).build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
Both libraries offer powerful machine learning capabilities, but scikit-learn focuses on traditional algorithms and ease of use, while Deeplearning4j specializes in deep learning with Java integration. The choice between them depends on the specific project requirements and the preferred programming language.
Deep Learning for humans
Pros of Keras
- Easier to learn and use, with a more intuitive API
- Better support for rapid prototyping and experimentation
- More extensive documentation and community support
Cons of Keras
- Less optimized for production environments
- Limited support for distributed training on multiple GPUs
- Dependent on backend libraries (TensorFlow, Theano, or CNTK)
Code Comparison
Keras:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential([
Dense(64, activation='relu', input_shape=(784,)),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy')
Deeplearning4j:
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.list()
.layer(0, new DenseLayer.Builder().nIn(784).nOut(64).activation(Activation.RELU).build())
.layer(1, new OutputLayer.Builder().nOut(10).activation(Activation.SOFTMAX).build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
Both Keras and Deeplearning4j are popular deep learning frameworks, but they cater to different audiences and use cases. Keras is more Python-centric and focuses on ease of use, while Deeplearning4j is Java-based and offers better integration with enterprise environments and big data ecosystems.
Apache Spark - A unified analytics engine for large-scale data processing
Pros of Spark
- Broader scope: general-purpose distributed computing framework for big data processing
- More mature ecosystem with extensive libraries and integrations
- Better suited for large-scale data processing and analytics tasks
Cons of Spark
- Steeper learning curve for beginners in big data processing
- Less specialized for deep learning tasks compared to Deeplearning4j
- Can be resource-intensive for smaller datasets or simpler computations
Code Comparison
Spark (Scala):
val data = spark.read.csv("data.csv")
val result = data.groupBy("category").agg(sum("value"))
result.show()
Deeplearning4j (Java):
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.list()
.layer(0, new DenseLayer.Builder().nIn(784).nOut(100).build())
.layer(1, new OutputLayer.Builder().nIn(100).nOut(10).build())
.build();
Summary
Spark is a versatile big data processing framework with a rich ecosystem, while Deeplearning4j focuses specifically on deep learning in Java. Spark excels in large-scale data analytics, whereas Deeplearning4j provides a more specialized toolkit for neural network development. The choice between them depends on the specific requirements of your project and the scale of data processing needed.
Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
Pros of CNTK
- Better performance and scalability for large-scale deep learning models
- Supports both Python and C++ APIs, offering flexibility for different programming preferences
- Efficient memory usage and optimized algorithms for faster training
Cons of CNTK
- Steeper learning curve compared to DeepLearning4j
- Less extensive documentation and community support
- Limited integration with Java ecosystems
Code Comparison
CNTK (Python):
import cntk as C
x = C.input_variable(2)
y = C.layers.Dense(10, activation=C.relu)(x)
z = C.layers.Dense(1, activation=C.sigmoid)(y)
DeepLearning4j (Java):
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.layer(new DenseLayer.Builder().nIn(2).nOut(10).activation(Activation.RELU).build())
.layer(new DenseLayer.Builder().nIn(10).nOut(1).activation(Activation.SIGMOID).build())
.build();
Both frameworks offer powerful deep learning capabilities, but CNTK excels in performance and scalability, while DeepLearning4j provides better integration with Java ecosystems and easier adoption for Java developers. CNTK's Python API offers more concise code, while DeepLearning4j's Java API provides a more verbose but type-safe approach.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The Eclipse Deeplearning4J (DL4J) ecosystem is a set of projects intended to support all the needs of a JVM based deep learning application. This means starting with the raw data, loading and preprocessing it from wherever and whatever format it is in to building and tuning a wide variety of simple and complex deep learning networks.
Because Deeplearning4J runs on the JVM you can use it with a wide variety of JVM based languages other than Java, like Scala, Kotlin, Clojure and many more.
The DL4J stack comprises of:
- DL4J: High level API to build MultiLayerNetworks and ComputationGraphs with a variety of layers, including custom ones. Supports importing Keras models from h5, including tf.keras models (as of 1.0.0-beta7) and also supports distributed training on Apache Spark
- ND4J: General purpose linear algebra library with over 500 mathematical, linear algebra and deep learning operations. ND4J is based on the highly-optimized C++ codebase LibND4J that provides CPU (AVX2/512) and GPU (CUDA) support and acceleration by libraries such as OpenBLAS, OneDNN (MKL-DNN), cuDNN, cuBLAS, etc
- SameDiff : Part of the ND4J library, SameDiff is our automatic differentiation / deep learning framework. SameDiff uses a graph-based (define then run) approach, similar to TensorFlow graph mode. Eager graph (TensorFlow 2.x eager/PyTorch) graph execution is planned. SameDiff supports importing TensorFlow frozen model format .pb (protobuf) models. Import for ONNX, TensorFlow SavedModel and Keras models are planned. Deeplearning4j also has full SameDiff support for easily writing custom layers and loss functions.
- DataVec: ETL for machine learning data in a wide variety of formats and files (HDFS, Spark, Images, Video, Audio, CSV, Excel etc)
- LibND4J : C++ library that underpins everything. For more information on how the JVM acceses native arrays and operations refer to JavaCPP
- Python4J: Bundled cpython execution for the JVM
All projects in the DL4J ecosystem support Windows, Linux and macOS. Hardware support includes CUDA GPUs (10.0, 10.1, 10.2 except OSX), x86 CPU (x86_64, avx2, avx512), ARM CPU (arm, arm64, armhf) and PowerPC (ppc64le).
Community Support
For support for the project, please go over to https://community.konduit.ai/
Using Eclipse Deeplearning4J in your project
Deeplearning4J has quite a few dependencies. For this reason we only support usage with a build tool.
<dependencies>
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-M2.1</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-M2.1</version>
</dependency>
</dependencies>
Add these dependencies to your pom.xml file to use Deeplearning4J with the CPU backend. A full standalone project example is available in the example repository, if you want to start a new Maven project from scratch.
Code samples
Due to DL4J being a multi faceted project with several modules in the mono repo, we recommend looking at the examples for a taste of different usages of the different modules. Below we'll link to examples for each module.
- ND4J: https://github.com/deeplearning4j/deeplearning4j-examples/tree/master/nd4j-ndarray-examples
- DL4J: https://github.com/deeplearning4j/deeplearning4j-examples/tree/master/dl4j-examples
- Samediff: https://github.com/deeplearning4j/deeplearning4j-examples/tree/master/samediff-examples
- Datavec: https://github.com/deeplearning4j/deeplearning4j-examples/tree/master/data-pipeline-examples
- Python4j: https://deeplearning4j.konduit.ai/python4j/tutorials/quickstart
For users looking for being able to run models from other frameworks, see:
- Onnx: https://github.com/deeplearning4j/deeplearning4j-examples/tree/master/onnx-import-examples
- Tensorflow/Keras: https://github.com/deeplearning4j/deeplearning4j-examples/tree/master/tensorflow-keras-import-examples
Documentation, Guides and Tutorials
You can find the official documentation for Deeplearning4J and the other libraries of its ecosystem at http://deeplearning4j.konduit.ai/.
Want some examples?
We have separate repository with various examples available: https://github.com/eclipse/deeplearning4j-examples
Building from source
It is preferred to use the official pre-compiled releases (see above). But if you want to build from source, first take a look at the prerequisites for building from source here: https://deeplearning4j.konduit.ai/multi-project/how-to-guides/build-from-source. Various instructions for cpu and gpu builds can be found there. Please go to our forums for further help.
Running tests
In order to run tests, please see the platform-tests module. This module only runs on jdk 11 (mostly due to spark and bugs with older scala versions + JDK 17)
platform-tests allows you to run dl4j for different backends. There are a few properties you can specify on the command line:
- backend.artifactId: this defaults to nd4j-native and will run tests on cpu,you can specify other backends like nd4j-cuda-11.6
- dl4j.version: You can change the dl4j version that the tests run against. This defaults to 1.0.0-SNAPSHOT.
More parameters can be found here: https://github.com/deeplearning4j/deeplearning4j/blob/c1bf8717e4839c8930e9c43183bf7b94d0cf84dc/platform-tests/pom.xml#L47
Running project in Intellij IDEA:
- Ensure you follow https://stackoverflow.com/questions/45370178/exporting-a-package-from-system-module-is-not-allowed-with-release on jdk 9 or later
- Ignore all nd4j-shade submodules. Right click on each folder and click: Maven -> Ignore project
License
Commercial Support
Deeplearning4J is actively developed by the team at Konduit K.K..
[If you need any commercial support feel free to reach out to us. at support@konduit.ai
Top Related Projects
An Open Source Machine Learning Framework for Everyone
Tensors and Dynamic neural networks in Python with strong GPU acceleration
scikit-learn: machine learning in Python
Deep Learning for humans
Apache Spark - A unified analytics engine for large-scale data processing
Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot