-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create source code examples with only one neuron #111
- Loading branch information
1 parent
529479d
commit 76fe56e
Showing
2 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<CONFIG> | ||
<ProjectOptions> | ||
<Version Value="11"/> | ||
<PathDelim Value="\"/> | ||
<General> | ||
<Flags> | ||
<MainUnitHasCreateFormStatements Value="False"/> | ||
</Flags> | ||
<SessionStorage Value="InProjectDir"/> | ||
<MainUnit Value="0"/> | ||
<Title Value="Only One Neuron - OR Operation"/> | ||
<UseAppBundle Value="False"/> | ||
<ResourceType Value="res"/> | ||
</General> | ||
<i18n> | ||
<EnableI18N LFM="False"/> | ||
</i18n> | ||
<BuildModes Count="3"> | ||
<Item1 Name="Default" Default="True"/> | ||
<Item2 Name="Debug"> | ||
<CompilerOptions> | ||
<Version Value="11"/> | ||
<PathDelim Value="\"/> | ||
<Target> | ||
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\OnlyOneNeuronOrOperation"/> | ||
</Target> | ||
<SearchPaths> | ||
<IncludeFiles Value="..\neural;$(ProjOutDir)"/> | ||
<OtherUnitFiles Value="$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS);..\..\neural"/> | ||
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/> | ||
</SearchPaths> | ||
<Parsing> | ||
<SyntaxOptions> | ||
<IncludeAssertionCode Value="True"/> | ||
</SyntaxOptions> | ||
</Parsing> | ||
<CodeGeneration> | ||
<Checks> | ||
<IOChecks Value="True"/> | ||
<RangeChecks Value="True"/> | ||
<OverflowChecks Value="True"/> | ||
<StackChecks Value="True"/> | ||
</Checks> | ||
<VerifyObjMethodCallValidity Value="True"/> | ||
</CodeGeneration> | ||
<Linking> | ||
<Debugging> | ||
<DebugInfoType Value="dsDwarf2Set"/> | ||
<UseValgrind Value="True"/> | ||
<UseExternalDbgSyms Value="True"/> | ||
</Debugging> | ||
</Linking> | ||
<Other> | ||
<CustomOptions Value="-dDebug | ||
-dAVX"/> | ||
<OtherDefines Count="2"> | ||
<Define0 Value="Debug"/> | ||
<Define1 Value="AVX"/> | ||
</OtherDefines> | ||
</Other> | ||
</CompilerOptions> | ||
</Item2> | ||
<Item3 Name="Release"> | ||
<CompilerOptions> | ||
<Version Value="11"/> | ||
<PathDelim Value="\"/> | ||
<Target> | ||
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\OnlyOneNeuronOrOperation"/> | ||
</Target> | ||
<SearchPaths> | ||
<IncludeFiles Value="..\..\neural;$(ProjOutDir)"/> | ||
<OtherUnitFiles Value="$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS);..\..\neural"/> | ||
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/> | ||
</SearchPaths> | ||
<CodeGeneration> | ||
<SmartLinkUnit Value="True"/> | ||
<Optimizations> | ||
<OptimizationLevel Value="3"/> | ||
</Optimizations> | ||
</CodeGeneration> | ||
<Linking> | ||
<Debugging> | ||
<GenerateDebugInfo Value="False"/> | ||
</Debugging> | ||
<LinkSmart Value="True"/> | ||
</Linking> | ||
<Other> | ||
<OtherDefines Count="6"> | ||
<Define0 Value="Release"/> | ||
<Define1 Value="Debug"/> | ||
<Define2 Value="CheckRange"/> | ||
<Define3 Value="AVX2"/> | ||
<Define4 Value="AVX"/> | ||
<Define5 Value="OpenCL"/> | ||
</OtherDefines> | ||
</Other> | ||
</CompilerOptions> | ||
</Item3> | ||
</BuildModes> | ||
<PublishOptions> | ||
<Version Value="2"/> | ||
</PublishOptions> | ||
<RunParams> | ||
<FormatVersion Value="2"/> | ||
<Modes Count="1"> | ||
<Mode0 Name="default"/> | ||
</Modes> | ||
</RunParams> | ||
<RequiredPackages Count="1"> | ||
<Item1> | ||
<PackageName Value="multithreadprocslaz"/> | ||
</Item1> | ||
</RequiredPackages> | ||
<Units Count="1"> | ||
<Unit0> | ||
<Filename Value="OnlyOneNeuronOrOperation.lpr"/> | ||
<IsPartOfProject Value="True"/> | ||
</Unit0> | ||
</Units> | ||
</ProjectOptions> | ||
<CompilerOptions> | ||
<Version Value="11"/> | ||
<PathDelim Value="\"/> | ||
<Target> | ||
<Filename Value="..\..\bin\$(TargetCPU)-$(TargetOS)\bin\OnlyOneNeuronOrOperation"/> | ||
</Target> | ||
<SearchPaths> | ||
<IncludeFiles Value="..\..\experiments\neural;$(ProjOutDir)"/> | ||
<OtherUnitFiles Value="$(LazarusDir)\lcl\units\$(TargetCPU)-$(TargetOS);$(LazarusDir)\components\lazutils\lib\$(TargetCPU)-$(TargetOS);..\..\neural"/> | ||
<UnitOutputDirectory Value="..\..\bin\$(TargetCPU)-$(TargetOS)\units"/> | ||
</SearchPaths> | ||
<CodeGeneration> | ||
<Optimizations> | ||
<OptimizationLevel Value="3"/> | ||
</Optimizations> | ||
</CodeGeneration> | ||
<Linking> | ||
<Debugging> | ||
<GenerateDebugInfo Value="False"/> | ||
</Debugging> | ||
</Linking> | ||
<Other> | ||
<CustomOptions Value="-dAVX | ||
-dRelease"/> | ||
<OtherDefines Count="2"> | ||
<Define0 Value="AVX"/> | ||
<Define1 Value="Release"/> | ||
</OtherDefines> | ||
</Other> | ||
</CompilerOptions> | ||
<Debugging> | ||
<Exceptions Count="3"> | ||
<Item1> | ||
<Name Value="EAbort"/> | ||
</Item1> | ||
<Item2> | ||
<Name Value="ECodetoolError"/> | ||
</Item2> | ||
<Item3> | ||
<Name Value="EFOpenError"/> | ||
</Item3> | ||
</Exceptions> | ||
</Debugging> | ||
</CONFIG> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
program OnlyOneNeuronOrOperation; | ||
(* | ||
OnlyOneNeuronOrOperation: learns how to calculate boolean X OR Y. | ||
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 TBackInput = array[0..3] of array[0..1] of TNeuralFloat; | ||
type TBackOutput = array[0..3] of array[0..0] of TNeuralFloat; | ||
|
||
const | ||
cs_false = 0.1; | ||
cs_true = 0.8; | ||
cs_threshold = (cs_false+cs_true) / 2; | ||
|
||
const cs_inputs : TBackInput = | ||
( // x1, x2 | ||
( cs_false, cs_false), | ||
( cs_false, cs_true), | ||
( cs_true, cs_false), | ||
( cs_true, cs_true) | ||
); | ||
|
||
const cs_outputs : TBackOutput = | ||
( // OR result | ||
(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(); | ||
NN.AddLayer( TNNetInput.Create(2) ); | ||
NN.AddLayer( TNNetFullConnectLinear.Create(1) ); | ||
NN.SetLearningRate(0.01, 0.9); | ||
|
||
vInputs := cs_inputs; | ||
vOutput := cs_outputs; | ||
pOutPut := TNNetVolume.Create(1, 1, 1, 1); | ||
|
||
WriteLn('Value encoding FALSE is: ', cs_false:4:2); | ||
WriteLn('Value encoding TRUE is: ', cs_true:4:2); | ||
WriteLn('Threshold is: ', cs_threshold:4:2); | ||
WriteLn; | ||
|
||
for EpochCnt := 1 to 600 do | ||
begin | ||
for Cnt := Low(cs_inputs) to High(cs_inputs) do | ||
begin | ||
NN.Compute(vInputs[Cnt]); | ||
NN.GetOutput(pOutPut); | ||
NN.Backpropagate(vOutput[Cnt]); | ||
if EpochCnt mod 100 = 0 then | ||
WriteLn | ||
( | ||
EpochCnt:7,'x',Cnt, | ||
' Output:', | ||
pOutPut.Raw[0]:5:2,' ', | ||
' - Training/Desired Output:', | ||
vOutput[cnt][0]:5:2,' ' | ||
); | ||
end; | ||
if EpochCnt mod 100 = 0 then WriteLn(); | ||
end; | ||
NN.DebugWeights(); | ||
pOutPut.Free; | ||
NN.Free; | ||
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. |