Examples Tour
Tour of the dl4j-examples repository — classification, regression, CNN, RNN, and advanced examples
Tour of the DL4J Examples Repository
The dl4j-examples repository contains runnable, self-contained Java programs covering most of DL4J's functionality. Each example is designed to be readable and educational: the code is annotated, data loading is handled automatically, and results are printed to the console or displayed visually.
This page walks through the major areas of the repository, describes what each example demonstrates, and links to the source code.
Getting the Examples
Clone and Build
git clone https://github.com/eclipse/deeplearning4j-examples.git
cd deeplearning4j-examples
mvn clean package -DskipTestsOpen in IntelliJ
File → Open → select the
deeplearning4j-examplesdirectory.IntelliJ will detect the Maven project and import it automatically.
Wait for indexing to complete, then right-click any example class and choose Run.
See the Quickstart for detailed IDE setup instructions.
Repository Structure
deeplearning4j-examples/
├── dl4j-examples/ # Core DL4J examples (most of what you want)
├── datavec-examples/ # Data ingestion and transformation examples
├── dl4j-spark-examples/ # Distributed training on Spark
├── nd4j-examples/ # Raw NDArray and linear algebra examples
└── rl4j-examples/ # Reinforcement learning examplesDataVec Examples
DataVec is DL4J's data pipeline library. It handles ingesting raw files (CSV, images, video, audio), applying transformations, and producing DataSet objects ready for training. If your data is not in a standard format, DataVec is where you spend the most time.
IrisAnalysis.java
Loads the canonical Iris flower dataset into a Spark RDD and runs a schema analysis. Good first example of the DataVec analysis API.
BasicDataVecExample.java
Demonstrates the transform process: loading data into a Spark RDD, defining a TransformProcess to filter rows, apply time transformations, and remove columns. The core pattern you will use for any ETL work.
PrintSchemasAtEachStep.java
Shows how to print the schema at each step of a transform pipeline, which is essential for debugging data issues. When a transform is producing unexpected output, this is the first tool to reach for.
JoinExample.java
Demonstrates joining two separate datasets in DataVec before passing the combined result to a network. Useful when your features come from multiple tables or files.
MnistImagePipelineExample.java
Uses ParentPathLabelGenerator and ImagePreProcessingScaler to load MNIST images from disk and normalize them. Shows the standard pattern for any image loading task.
CSVExampleEvaluationMetaData.java
Demonstrates RecordMetaData, which tracks the origin of each example. When a network produces errors on specific inputs, this lets you trace back to the original source records.
Classification Examples
MLPMnistSingleLayerExample.java
A single hidden-layer feedforward network for MNIST digit classification. The simplest possible end-to-end DL4J program: data loading, network configuration, training loop, evaluation. Start here.
MLPMnistTwoLayerExample.java
Extends the single-layer example with a second hidden layer. Demonstrates how adding depth affects training dynamics and accuracy on the same dataset.
Feedforward examples directory
Contains additional classification examples (anomaly detection, multi-class iris classification, linear data classification) and regression examples. Browse the directory for the task closest to yours.
Regression Examples
RegressionMathFunctions.java
Trains a feedforward network to approximate several mathematical functions (sin, cos, x^2, etc.) from a single input value. The cleanest illustration of regression with MultiLayerNetwork: shows how to set up LossFunction.MSE and how to evaluate using RegressionEvaluation.
CNN Examples (Image Classification)
AnimalsClassification.java
Classifies images of four animal categories (bear, deer, duck, turtle) loaded from a local image directory. Can be run with either AlexNet or LeNet. Demonstrates FileSplit, ParentPathLabelGenerator, ImageRecordReader, and RecordReaderDataSetIterator — the standard image loading stack. To use your own image dataset, organize images into one subdirectory per class and point FileSplit at the root.
Transfer Learning with VGG16
The transfer learning examples show how to take a pretrained ImageNet model from the model zoo and adapt it to a new classification task by replacing the output layer:
FeaturizedPreSave.java — runs the pretrained VGG16 up to a specified layer and saves the activations (features) to disk. Only needs to be run once.
FitFromFeaturized.java — loads those saved features and trains a new classification head on them. Very fast because the expensive convolutional forward pass is precomputed.
EditLastLayerOthersFrozen.java — demonstrates modifying VGG16 by freezing all layers except the final output layer and fine-tuning end-to-end.
Transfer learning examples directory
RNN Examples (Sequence Data)
BasicRNNExample.java
A minimal RNN that learns to reproduce a string of characters from a small alphabet. The simplest possible demonstration of a recurrent network in DL4J.
GravesLSTMCharModellingExample.java
Trains an LSTM on the complete works of Shakespeare, character by character, then generates text in the style of Shakespeare. A famous example of character-level language modeling. The generated text quality improves noticeably over training epochs.
UCISequenceClassificationExample.java
Classifies time series into six categories (cyclic, uptrending, downtrending, etc.) using the UCI time series dataset. Shows how to load variable-length sequences, pad them to equal length, and use RnnSequenceClassificationEvaluation.
Word2VecSentimentRNN.java
Sentiment analysis (positive/negative) on movie reviews using Word2Vec embeddings fed into an LSTM. Demonstrates combining pretrained word vectors with a recurrent classifier — a common pattern in NLP.
VideoClassificationExample.java
Combines convolutional layers (to process each frame), max pooling, dense layers, and LSTM layers (to capture temporal dynamics across frames) into a single ComputationGraph for video classification. A good example of multi-modal architectures.
NLP Examples
Word2VecRawTextExample.java
Trains a Word2Vec model from scratch on a raw text file and evaluates the resulting embedding space. Demonstrates SentenceIterator, TokenizerFactory, Word2Vec.Builder, and the wordsNearest() similarity API.
TSNEStandardExample.java
Loads pre-trained word vectors, runs t-SNE to reduce them to 2D, and saves coordinates to a CSV file for plotting. Shows that semantically similar words cluster together in the embedding space.
ParagraphVectorsClassifierExample.java
Uses Paragraph Vectors (Doc2Vec) to classify documents. Each document is embedded as a fixed-length vector before classification.
Saving and Loading Models
SaveLoadMultiLayerNetwork.java / SaveLoadComputationGraph.java
These examples demonstrate serializing a trained network to a zip file and reloading it for inference or continued training using ModelSerializer. Essential for any production workflow.
Advanced Examples: Custom Layers and Loss Functions
CustomLayerExample.java
Shows the minimum required to implement a custom layer: defining forward pass, backward pass, parameter initialization, and integrating with the DL4J builder API. Start here if you need a layer type that DL4J does not provide.
CustomLossExample.java / CustomLossL1L2.java
Demonstrates implementing a custom loss function by extending ILossFunction. Includes both the loss computation and its gradient.
Distributed Training on Spark
MnistMLPExample.java
Trains a feedforward network on the MNIST dataset across a Spark cluster using SparkDl4jMultiLayer. Shows how to set up a ParameterAveragingTrainingMaster and adapt a single-node training loop for distributed use.
SparkLSTMCharacterExample.java
The Shakespeare LSTM text generation example, adapted for Spark. Useful for seeing how the Spark integration handles recurrent networks and time series data distribution.
ND4J Examples
ND4J is the tensor library DL4J is built on. The ND4J examples demonstrate creating and manipulating INDArray objects directly — useful if you need to implement custom preprocessing or mathematical operations outside the DL4J layer abstraction.
Reinforcement Learning Examples (RL4J)
RL4J is DL4J's reinforcement learning library. Examples include agents learning to play Atari-style games and custom environments. The examples are in the rl4j-examples sub-module.
Running Examples from the Command Line
After building with mvn package, you can run any example from the command line:
Replace the class name with the example you want to run. The -bin.jar artifact produced by the Maven Shade plugin includes all dependencies.
Last updated
Was this helpful?