Skip to content

Commit f39b9ac

Browse files
authored
Merge pull request #1090 from NA-V10/improve-lenet-docs
Add documentation and comments for LeNetMNIST CNN example
2 parents 051c59b + c29d1c0 commit f39b9ac

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/convolution/LeNetMNIST.java

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

20+
// LeNetMNIST.java
21+
// Classic LeNet-style CNN for MNIST digit classification.
22+
// Expected accuracy: 98–99%.
23+
// MNIST images: 28x28 grayscale (1 channel).
24+
25+
2026
package org.deeplearning4j.examples.quickstart.modeling.convolution;
2127

2228
import org.apache.commons.io.FilenameUtils;
@@ -64,6 +70,10 @@ public static void main(String[] args) throws Exception {
6470
*/
6571
log.info("Build model....");
6672

73+
// Build LeNet CNN architecture:
74+
// Conv → Pool → Conv → Pool → Dense → Output
75+
76+
6777
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
6878
.seed(seed)
6979
.l2(0.0005)
@@ -116,7 +126,9 @@ row vector format (i.e., 1x784 vectors), hence the "convolutionalFlat" input typ
116126

117127
MultiLayerNetwork model = new MultiLayerNetwork(conf);
118128
model.init();
119-
129+
130+
// Evaluate the model at the end of each epoch using EvaluativeListener
131+
120132
log.info("Train model...");
121133
model.setListeners(new ScoreIterationListener(10), new EvaluativeListener(mnistTest, 1, InvocationType.EPOCH_END)); //Print score every 10 iterations and evaluate on test set every epoch
122134
model.fit(mnistTrain, nEpochs);

dl4j-examples/src/main/java/org/deeplearning4j/examples/quickstart/modeling/convolution/LeNetMNISTReLu.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+
// LeNetMNISTReLu.java
21+
// Same as LeNetMNIST but uses ReLU activation instead of Tanh.
22+
23+
2024
package org.deeplearning4j.examples.quickstart.modeling.convolution;
2125

2226
import org.datavec.api.io.labels.ParentPathLabelGenerator;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Convolutional Neural Network (CNN) Examples – DeepLearning4J
2+
3+
This folder contains convolutional neural network (CNN) examples implemented using DeepLearning4J.
4+
5+
## 🧠 LeNetMNIST.java
6+
Trains a LeNet-style CNN on the MNIST handwritten digit dataset.
7+
8+
### ✔ What this example demonstrates
9+
- Loading the MNIST dataset
10+
- Building a classic LeNet CNN architecture
11+
- Training the network
12+
- Evaluating accuracy
13+
14+
### ✔ Expected Accuracy
15+
**98%–99%** after 1–2 epochs.
16+
17+
---
18+
19+
## 📦 How to Run
20+
21+
Build the project:
22+
23+
mvn clean package
24+
25+
Run the example:
26+
27+
mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.quickstart.modeling.convolution.LeNetMNIST"
28+
29+
Run the ReLU variant:
30+
31+
mvn -q exec:java -Dexec.mainClass="org.deeplearning4j.examples.quickstart.modeling.convolution.LeNetMNISTReLu"
32+
33+
---
34+
35+
## 🧱 LeNet Architecture Breakdown
36+
- Convolution layer (20 filters)
37+
- Subsampling (2×2)
38+
- Convolution layer (50 filters)
39+
- Subsampling
40+
- Dense layer
41+
- Output layer (Softmax, 10 classes)
42+
43+
---
44+
45+
## 📂 Other Files
46+
| File | Description |
47+
|------|-------------|
48+
| **LeNetMNISTReLu.java** | LeNet variant with ReLU activation |
49+
| **CenterLossLeNetMNIST.java** | LeNet with center loss |
50+
| **CIFARClassifier.java** | CIFAR-10 image classifier |
51+
| **Conv1DUCISequenceClassifier.java** | 1D CNN example for sequences |
52+
53+
---
54+
55+
## 🙌 Why This Documentation Helps
56+
These CNN examples previously had no explanation, run instructions, or architecture summary.
57+
This README improves clarity for new users and first-time contributors.

0 commit comments

Comments
 (0)