Skip to content

Commit 5171fd9

Browse files
committed
Add README and comments for RNN examples (recurrent folder)
1 parent 274189e commit 5171fd9

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/recurrent/MemorizeSequence.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* SPDX-License-Identifier: Apache-2.0
1818
******************************************************************************/
1919

20+
// MemorizeSequence.java
21+
// A simple RNN example where the network learns to memorize and reproduce a short sequence.
22+
// Demonstrates basic RNN training and backpropagation-through-time.
23+
2024
package org.deeplearning4j.examples.quickstart.modeling.recurrent;
2125

2226
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
@@ -87,6 +91,8 @@ public static void main(String[] args) {
8791
hiddenLayerBuilder.activation(Activation.TANH);
8892
listBuilder.layer(i, hiddenLayerBuilder.build());
8993
}
94+
95+
// Build a simple RNN with one recurrent layer to memorize the sequence
9096

9197
// we need to use RnnOutputLayer for our RNN
9298
RnnOutputLayer.Builder outputLayerBuilder = new RnnOutputLayer.Builder(LossFunction.MCXENT);
@@ -96,7 +102,8 @@ public static void main(String[] args) {
96102
outputLayerBuilder.nIn(HIDDEN_LAYER_WIDTH);
97103
outputLayerBuilder.nOut(LEARNSTRING_CHARS.size());
98104
listBuilder.layer(HIDDEN_LAYER_CONT, outputLayerBuilder.build());
99-
105+
106+
100107
// create network
101108
MultiLayerConfiguration conf = listBuilder.build();
102109
MultiLayerNetwork net = new MultiLayerNetwork(conf);
@@ -122,6 +129,9 @@ public static void main(String[] args) {
122129
labels.putScalar(new int[] { 0, LEARNSTRING_CHARS_LIST.indexOf(nextChar), samplePos }, 1);
123130
samplePos++;
124131
}
132+
133+
// Train the RNN to output the same sequence it receives as input
134+
125135
DataSet trainingData = new DataSet(input, labels);
126136

127137
// some epochs
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Recurrent Neural Network (RNN) Examples – DeepLearning4J
2+
3+
This folder contains simple recurrent neural network (RNN) examples using LSTM and embedding layers.
4+
These examples demonstrate how to work with sequential data such as signals, sequences, and text-like inputs.
5+
6+
---
7+
8+
## 🔁 MemorizeSequence.java
9+
A minimal RNN example where the network learns to memorize and reproduce a fixed sequence.
10+
11+
### What this example teaches
12+
- How RNNs store information across time steps
13+
- How backpropagation-through-time works
14+
- How sequence learning differs from feedforward networks
15+
16+
### Expected Behavior
17+
The network eventually outputs the same sequence it was trained on.
18+
19+
---
20+
21+
## 🔡 RNNEmbedding.java
22+
Demonstrates the use of an **EmbeddingLayer** followed by RNN layers.
23+
24+
### Key Concepts
25+
- Turning integer-encoded inputs into dense vectors
26+
- Word/token embedding
27+
- Passing embedded sequences into RNN layers
28+
29+
This is a useful template for NLP-style models.
30+
31+
---
32+
33+
## 📊 UCISequenceClassification.java
34+
Sequence classification on a dataset from the UCI machine learning repository.
35+
36+
### What this example shows
37+
- Loading sequential datasets
38+
- Recurrent classification (predict a label for a whole sequence)
39+
- Time-series preprocessing and normalization
40+
41+
---
42+
43+
## ✔ How to Run Any Example
44+
45+
Use the following command:
46+
47+
mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.quickstart.modeling.recurrent.<ClassName>"
48+
49+
50+
Example:
51+
52+
53+
54+
mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.quickstart.modeling.recurrent.MemorizeSequence"
55+
56+
57+
---
58+
59+
## 🙌 Why This README Helps
60+
61+
This folder previously had no documentation.
62+
This README explains:
63+
- What each RNN example does
64+
- What concepts it teaches
65+
- How to run each file
66+
67+
This improves clarity for beginners working with sequential models in DL4J.

dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/recurrent/RNNEmbedding.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
* SPDX-License-Identifier: Apache-2.0
1818
******************************************************************************/
1919

20+
21+
// RNNEmbedding.java
22+
// Demonstrates how to use an EmbeddingLayer + RNN layers for sequence modeling.
23+
// Useful for NLP-style integer token inputs.
24+
2025
package org.deeplearning4j.examples.quickstart.modeling.recurrent;
2126

2227
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
@@ -45,6 +50,9 @@
4550
*
4651
* @author Alex Black
4752
*/
53+
54+
// Convert integer token IDs into dense embedding vectors
55+
4856
public class RNNEmbedding {
4957
public static void main(String[] args) {
5058

@@ -64,6 +72,10 @@ public static void main(String[] args) {
6472
}
6573
}
6674

75+
76+
// Feed embedded vectors into an LSTM/RNN to capture sequence structure
77+
78+
6779
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
6880
.activation(Activation.RELU)
6981
.list()

dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/recurrent/UCISequenceClassification.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* SPDX-License-Identifier: Apache-2.0
1818
******************************************************************************/
1919

20+
21+
// UCISequenceClassification.java
22+
// Demonstrates sequence classification using an RNN on UCI dataset sequences.
23+
2024
package org.deeplearning4j.examples.quickstart.modeling.recurrent;
2125

2226
import org.apache.commons.io.FileUtils;

0 commit comments

Comments
 (0)