Skip to content

Commit adb764e

Browse files
Better comments on examples.
1 parent 38fe7c9 commit adb764e

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

examples/SimpleFashionMNIST/SimpleFashionMNIST.lpr

+40-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
(*
33
Coded by Joao Paulo Schwarz Schuler.
44
https://github.com/joaopauloschuler/neural-api
5+
-----------------------------------------------
6+
The code shows an example of training and fitting a convolutional
7+
neural network (CNN) using the Fashion MNIST dataset. It creates a neural
8+
network with specific layers and configurations, loads the fashion MNIST data,
9+
and then trains the network using the provided data. The code also sets
10+
various parameters for training, such as learning rate, decay, and batch size.
511
*)
612
{$mode objfpc}{$H+}
713

@@ -15,34 +21,42 @@ TTestCNNAlgo = class(TCustomApplication)
1521
procedure DoRun; override;
1622
end;
1723

24+
// Implementation of the TTestCNNAlgo class
1825
procedure TTestCNNAlgo.DoRun;
1926
var
20-
NN: TNNet;
21-
NeuralFit: TNeuralImageFit;
22-
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList;
27+
NN: TNNet; // Neural network object
28+
NeuralFit: TNeuralImageFit; // Object for training and fitting the neural network
29+
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList; // Lists of training, validation, and test image volumes
2330
begin
31+
// Checking if the MNIST files exist and loading the data
2432
if Not(CheckMNISTFile('train', {IsFashion=}true)) or
2533
Not(CheckMNISTFile('t10k', {IsFashion=}true)) then
2634
begin
2735
Terminate;
2836
exit;
2937
end;
38+
3039
WriteLn('Creating Neural Network...');
31-
NN := THistoricalNets.Create();
40+
41+
// Creating the neural network with specific layers and configurations
42+
NN := TNNet.Create();
3243
NN.AddLayer([
33-
TNNetInput.Create(28, 28, 1),
34-
TNNetConvolutionLinear.Create(64, 5, 2, 1, 1),
35-
TNNetMaxPool.Create(4),
36-
TNNetConvolutionReLU.Create(64, 3, 1, 1, 1),
37-
TNNetConvolutionReLU.Create(64, 3, 1, 1, 1),
38-
TNNetFullConnectReLU.Create(32),
39-
TNNetFullConnectReLU.Create(32),
40-
TNNetFullConnectLinear.Create(10),
41-
TNNetSoftMax.Create()
44+
TNNetInput.Create(28, 28, 1), // Input layer for 28x28 grayscale images
45+
TNNetConvolutionLinear.Create(64, 5, 2, 1, 1), // Convolutional layer with linear activation
46+
TNNetMaxPool.Create(4), // Max pooling layer
47+
TNNetConvolutionReLU.Create(64, 3, 1, 1, 1), // Convolutional layer with ReLU activation
48+
TNNetConvolutionReLU.Create(64, 3, 1, 1, 1), // Convolutional layer with ReLU activation
49+
TNNetFullConnectReLU.Create(32), // Fully connected layer with ReLU activation
50+
TNNetFullConnectReLU.Create(32), // Fully connected layer with ReLU activation
51+
TNNetFullConnectLinear.Create(10), // Fully connected layer with linear activation
52+
TNNetSoftMax.Create() // Softmax layer for classification
4253
]);
54+
55+
// Creating the training, validation, and test image volumes from the fashion MNIST files
4356
CreateMNISTVolumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes,
4457
'train', 't10k', {Verbose=}true, {IsFashion=}true);
4558

59+
// Creating and configuring the NeuralFit object for training the neural network
4660
NeuralFit := TNeuralImageFit.Create;
4761
NeuralFit.FileNameBase := 'SimpleFashionMNIST';
4862
NeuralFit.InitialLearningRate := 0.001;
@@ -53,21 +67,25 @@ TTestCNNAlgo = class(TCustomApplication)
5367
NeuralFit.HasFlipX := true;
5468
NeuralFit.HasFlipY := false;
5569
NeuralFit.MaxCropSize := 4;
70+
71+
// Training and fitting the neural network using the provided data
5672
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, {NumClasses=}10, {batchsize=}128, {epochs=}50);
57-
NeuralFit.Free;
5873

59-
NN.Free;
60-
ImgTestVolumes.Free;
61-
ImgValidationVolumes.Free;
62-
ImgTrainingVolumes.Free;
74+
NeuralFit.Free; // Freeing the NeuralFit object
75+
76+
NN.Free; // Freeing the neural network object
77+
ImgTestVolumes.Free; // Freeing the test data volumes
78+
ImgValidationVolumes.Free; // Freeing the validation data volumes
79+
ImgTrainingVolumes.Free; // Freeing the training data volumes
6380
Terminate;
6481
end;
6582

6683
var
6784
Application: TTestCNNAlgo;
6885
begin
69-
Application := TTestCNNAlgo.Create(nil);
70-
Application.Title:='Simple Fashion MNIST Classification Example';
71-
Application.Run;
72-
Application.Free;
86+
Application := TTestCNNAlgo.Create(nil); // Creating an instance of the TTestCNNAlgo class
87+
Application.Title:='Simple Fashion MNIST Classification Example'; // Setting the application title
88+
Application.Run; // Running the application
89+
Application.Free; // Freeing the application instance
7390
end.
91+

examples/SimpleMNist/SimpleMNist.lpr

+24-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
99
cthreads, {$ENDIF} {$ENDIF}
10-
Classes, SysUtils, CustApp, neuralnetwork, neuralvolume, Math, neuraldatasets, neuralfit;
10+
Classes, SysUtils, CustApp, neuralnetwork, neuralvolume, Math,
11+
neuraldatasets, neuralfit;
1112

1213
type
1314
TTestCNNAlgo = class(TCustomApplication)
@@ -17,17 +18,21 @@ TTestCNNAlgo = class(TCustomApplication)
1718

1819
procedure TTestCNNAlgo.DoRun;
1920
var
20-
NN: THistoricalNets;
21-
NeuralFit: TNeuralImageFit;
22-
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList;
21+
NN: TNNet; // Neural network object
22+
NeuralFit: TNeuralImageFit; // Object for neural network fitting
23+
ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes: TNNetVolumeList; // Volumes for training, validation, and testing
2324
begin
24-
if Not(CheckMNISTFile('train')) or Not(CheckMNISTFile('t10k')) then
25+
// Check if MNIST files exist
26+
if not (CheckMNISTFile('train')) or not (CheckMNISTFile('t10k')) then
2527
begin
2628
Terminate;
27-
exit;
29+
Exit; // Exit the procedure if MNIST files are not found
2830
end;
31+
2932
WriteLn('Creating Neural Network...');
30-
NN := THistoricalNets.Create();
33+
NN := TNNet.Create(); // Create an instance of the neural network
34+
35+
// Define the layers of the neural network
3136
NN.AddLayer([
3237
TNNetInput.Create(28, 28, 1),
3338
TNNetConvolutionLinear.Create(32, 5, 2, 1, 1),
@@ -41,21 +46,27 @@ TTestCNNAlgo = class(TCustomApplication)
4146
TNNetFullConnectLinear.Create(10),
4247
TNNetSoftMax.Create()
4348
]);
49+
50+
// Create MNIST volumes for training, validation, and testing
4451
CreateMNISTVolumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, 'train', 't10k');
4552

53+
// Configure the neural network fitting
4654
NeuralFit := TNeuralImageFit.Create;
4755
NeuralFit.FileNameBase := 'SimpleMNist';
4856
NeuralFit.InitialLearningRate := 0.001;
4957
NeuralFit.LearningRateDecay := 0.01;
5058
NeuralFit.StaircaseEpochs := 10;
5159
NeuralFit.Inertia := 0.9;
5260
NeuralFit.L2Decay := 0.00001;
53-
NeuralFit.HasFlipX := false;
54-
NeuralFit.HasFlipY := false;
61+
NeuralFit.HasFlipX := False;
62+
NeuralFit.HasFlipY := False;
5563
NeuralFit.MaxCropSize := 4;
64+
65+
// Fit the neural network using the training, validation, and testing volumes
5666
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, {NumClasses=}10, {batchsize=}128, {epochs=}20);
5767
NeuralFit.Free;
5868

69+
// Clean up resources
5970
NN.Free;
6071
ImgTestVolumes.Free;
6172
ImgValidationVolumes.Free;
@@ -66,8 +77,8 @@ TTestCNNAlgo = class(TCustomApplication)
6677
var
6778
Application: TTestCNNAlgo;
6879
begin
69-
Application := TTestCNNAlgo.Create(nil);
70-
Application.Title:='MNist Classification Example';
71-
Application.Run;
72-
Application.Free;
80+
Application := TTestCNNAlgo.Create(nil); // Create an instance of TTestCNNAlgo
81+
Application.Title := 'MNist Classification Example'; // Set the application title
82+
Application.Run; // Run the application
83+
Application.Free; // Free the application instance
7384
end.

0 commit comments

Comments
 (0)