Skip to content

Commit 274189e

Browse files
committed
Add README and comments for MNIST autoencoder example
1 parent c340292 commit 274189e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/feedforward/unsupervised/MNISTAutoencoder.java

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

20+
21+
// MNISTAutoencoder.java
22+
// Demonstrates training an autoencoder on MNIST digit images.
23+
// Autoencoders learn compressed representations (unsupervised learning).
24+
2025
package org.deeplearning4j.examples.quickstart.modeling.feedforward.unsupervised;
2126

2227
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -81,6 +86,8 @@ public static void main(String[] args) throws Exception {
8186
.build())
8287
.build();
8388

89+
// Build a simple autoencoder: Encoder → Bottleneck → Decoder
90+
8491
MultiLayerNetwork net = new MultiLayerNetwork(conf);
8592
net.setListeners(Collections.singletonList(new ScoreIterationListener(10)));
8693

@@ -101,6 +108,8 @@ public static void main(String[] args) throws Exception {
101108
INDArray indexes = Nd4j.argMax(dsTest.getLabels(),1); //Convert from one-hot representation -> index
102109
labelsTest.add(indexes);
103110
}
111+
112+
// Train the autoencoder to minimize reconstruction loss
104113

105114
//Train model:
106115
int nEpochs = 3;
@@ -154,8 +163,10 @@ public int compare(Pair<Double, INDArray> o1, Pair<Double, INDArray> o2) {
154163
worst.add(list.get(list.size()-j-1).getRight());
155164
}
156165
}
166+
167+
//Visualize by default
168+
// Evaluate reconstruction quality or print sample reconstructions
157169

158-
//Visualize by default
159170
if (visualize) {
160171
//Visualize the best and worst digits
161172
MNISTVisualizer bestVisualizer = new MNISTVisualizer(2.0, best, "Best (Low Rec. Error)");
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Unsupervised Learning Examples – Autoencoder (DeepLearning4J)
2+
3+
This folder contains unsupervised learning examples implemented using DeepLearning4J.
4+
The primary example in this directory demonstrates how to train an autoencoder on MNIST digits to perform dimensionality reduction and reconstruction.
5+
6+
---
7+
8+
## 🧠 MNISTAutoencoder.java
9+
10+
A simple autoencoder trained on the MNIST dataset (28×28 grayscale digit images).
11+
Autoencoders learn to compress input data into a lower-dimensional representation and then reconstruct it.
12+
13+
### What this example shows
14+
- How autoencoders work
15+
- How to compress images into a bottleneck latent space
16+
- How to reconstruct input images
17+
- How unsupervised neural networks are trained
18+
19+
### Key Concepts
20+
- **Encoder:** Compresses image → latent representation
21+
- **Decoder:** Reconstructs latent representation → image
22+
- **Loss Function:** Measures reconstruction quality
23+
24+
### Expected Behavior
25+
The autoencoder gradually learns to:
26+
- Rebuild digit outlines
27+
- Capture key features
28+
- Reduce noise
29+
30+
This is not a classifier — it learns **patterns** without labels.
31+
32+
---
33+
34+
## ✔ How to Run
35+
36+
mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.quickstart.modeling.feedforward.unsupervised.MNISTAutoencoder"
37+
38+
39+
---
40+
41+
## 🙌 Why This README Helps
42+
The unsupervised folder previously had no explanation, run instructions, or conceptual overview.
43+
This documentation improves clarity and helps beginners understand autoencoders and unsupervised learning techniques in DL4J.

0 commit comments

Comments
 (0)