-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathOnlyOneNeuronOrOperation.lpr
126 lines (103 loc) · 4.24 KB
/
OnlyOneNeuronOrOperation.lpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
program OnlyOneNeuronOrOperation;
(*
OnlyOneNeuronOrOperation: this free pascal source code trains a neural network
that contains only one neuron to learn the OR boolean operation.
Copyright (C) 2023 Joao Paulo Schwarz Schuler
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)
{$mode objfpc}{$H+}
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
cthreads, {$ENDIF} {$ENDIF}
Classes,
neuralnetwork,
neuralvolume,
neuralfit;
type
// Define the input and output types for training data
TBackInput = array[0..3] of array[0..1] of TNeuralFloat; // Input data for OR operation
TBackOutput = array[0..3] of array[0..0] of TNeuralFloat; // Expected output for OR operation
const
cs_false = 0.1; // Encoding for "false" value
cs_true = 0.8; // Encoding for "true" value
cs_threshold = (cs_false + cs_true) / 2; // Threshold for False/True values
const
cs_inputs : TBackInput =
(
// Input data for OR operation
(cs_false, cs_false),
(cs_false, cs_true),
(cs_true, cs_false),
(cs_true, cs_true)
);
const
cs_outputs : TBackOutput =
(
// Expected outputs for OR operation
(cs_false),
(cs_true),
(cs_true),
(cs_true)
);
procedure RunAlgo();
var
NN: TNNet;
EpochCnt: integer;
Cnt: integer;
pOutPut: TNNetVolume;
vInputs: TBackInput;
vOutput: TBackOutput;
begin
NN := TNNet.Create();
// Create the neural network layers
NN.AddLayer(TNNetInput.Create(2)); // Input layer with 2 inputs
NN.AddLayer(TNNetFullConnectLinear.Create(1)); // Single neuron layer connected to both inputs from the previous layer.
NN.SetLearningRate(0.01, 0.9); // Set the learning rate and momentum
vInputs := cs_inputs; // Assign the input data
vOutput := cs_outputs; // Assign the expected output data
pOutPut := TNNetVolume.Create(1, 1, 1, 1); // Create a volume to hold the computed output
WriteLn('The value encoding FALSE is: ', cs_false:4:2); // Display the encoding for "false"
WriteLn('The value encoding TRUE is: ', cs_true:4:2); // Display the encoding for "true"
WriteLn('The threshold is: ', cs_threshold:4:2); // Display the threshold value
WriteLn;
for EpochCnt := 1 to 600 do
begin
for Cnt := Low(cs_inputs) to High(cs_inputs) do
begin
// Feed forward and backpropagation
NN.Compute(vInputs[Cnt]); // Perform feedforward computation
NN.GetOutput(pOutPut); // Get the output of the network
NN.Backpropagate(vOutput[Cnt]); // Perform backpropagation to adjust weights
if EpochCnt mod 100 = 0 then
WriteLn(
EpochCnt:7, 'x', Cnt,
' Inputs:', vInputs[Cnt][0]:5:3,' ',vInputs[Cnt][1]:5:3,
' Computed Output:', pOutPut.Raw[0]:5:2,' ',
' Desired Output:', vOutput[cnt][0]:5:2
);
end;
if EpochCnt mod 100 = 0 then
WriteLn();
end;
NN.DebugWeights(); // Display the final weights of the network
pOutPut.Free; // Free the memory allocated for output
NN.Free; // Free the memory allocated for the network
Write('Press ENTER to exit.');
ReadLn;
end;
var
// Stops Lazarus errors
Application: record Title:string; end;
begin
Application.Title:='Only One Neuron - OR Operation';
RunAlgo();
end.