2
2
(*
3
3
Coded by Joao Paulo Schwarz Schuler.
4
4
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.
5
11
*)
6
12
{ $mode objfpc}{ $H+}
7
13
@@ -15,34 +21,42 @@ TTestCNNAlgo = class(TCustomApplication)
15
21
procedure DoRun ; override;
16
22
end ;
17
23
24
+ // Implementation of the TTestCNNAlgo class
18
25
procedure TTestCNNAlgo.DoRun ;
19
26
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
23
30
begin
31
+ // Checking if the MNIST files exist and loading the data
24
32
if Not (CheckMNISTFile(' train' , { IsFashion=} true)) or
25
33
Not (CheckMNISTFile(' t10k' , { IsFashion=} true)) then
26
34
begin
27
35
Terminate;
28
36
exit;
29
37
end ;
38
+
30
39
WriteLn(' Creating Neural Network...' );
31
- NN := THistoricalNets.Create();
40
+
41
+ // Creating the neural network with specific layers and configurations
42
+ NN := TNNet.Create();
32
43
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
42
53
]);
54
+
55
+ // Creating the training, validation, and test image volumes from the fashion MNIST files
43
56
CreateMNISTVolumes(ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes,
44
57
' train' , ' t10k' , { Verbose=} true, { IsFashion=} true);
45
58
59
+ // Creating and configuring the NeuralFit object for training the neural network
46
60
NeuralFit := TNeuralImageFit.Create;
47
61
NeuralFit.FileNameBase := ' SimpleFashionMNIST' ;
48
62
NeuralFit.InitialLearningRate := 0.001 ;
@@ -53,21 +67,25 @@ TTestCNNAlgo = class(TCustomApplication)
53
67
NeuralFit.HasFlipX := true;
54
68
NeuralFit.HasFlipY := false;
55
69
NeuralFit.MaxCropSize := 4 ;
70
+
71
+ // Training and fitting the neural network using the provided data
56
72
NeuralFit.Fit(NN, ImgTrainingVolumes, ImgValidationVolumes, ImgTestVolumes, { NumClasses=} 10 , { batchsize=} 128 , { epochs=} 50 );
57
- NeuralFit.Free;
58
73
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
63
80
Terminate;
64
81
end ;
65
82
66
83
var
67
84
Application: TTestCNNAlgo;
68
85
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
73
90
end .
91
+
0 commit comments