Skip to content

Commit c7c363c

Browse files
authored
Merge pull request #6 from kamalsaleh/master
Update methods for creating and fitting neural networks
2 parents e14e98f + 40a581d commit c7c363c

File tree

7 files changed

+20
-25
lines changed

7 files changed

+20
-25
lines changed

PackageInfo.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SetPackageInfo( rec(
1010

1111
PackageName := "MachineLearningForCAP",
1212
Subtitle := "Exploring categorical machine learning in CAP",
13-
Version := "2024.07-15",
13+
Version := "2024.07-17",
1414
Date := (function ( ) if IsBound( GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE ) then return GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE; else return Concatenation( ~.Version{[ 1 .. 4 ]}, "-", ~.Version{[ 6, 7 ]}, "-01" ); fi; end)( ),
1515
License := "GPL-2.0-or-later",
1616

gap/CategoryOfParametrisedMorphisms.gi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ InstallMethod( ReparametriseMorphism,
304304

305305
end );
306306

307+
##
308+
InstallOtherMethod( Eval,
309+
[ IsMorphismInCategoryOfParametrisedMorphisms, IsDenseList ],
310+
311+
function( f, pair )
312+
313+
return Eval( UnderlyingMorphism( f ), Concatenation( pair ) );
314+
315+
end );
316+
307317
##
308318
InstallMethod( NaturalEmbeddingIntoCategoryOfParametrisedMorphisms,
309319
[ IsCapCategory, IsCategoryOfParametrisedMorphisms ],

gap/FitParameters.gi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,22 @@ InstallMethod( Fit,
139139

140140
l_n := Length( String( n ) );
141141

142-
for i in [ 0 .. n ] do
142+
Print( "Epoch ", JoinStringsWithSeparator( ListWithIdenticalEntries( l_n - 1, " " ), "" ), "0/", String( n ), " - loss = ", String( get( w )[1] ), "\n" );
143+
144+
for i in [ 1 .. n ] do
143145

144146
str_i := String( i );
145147

146148
l_i := Length( str_i );
147149

148150
spaces := JoinStringsWithSeparator( ListWithIdenticalEntries( l_n - l_i, " " ), "" );
149151

152+
w := put( w );
153+
150154
loss := get( w );
151155

152156
Print( "Epoch ", spaces, String( i ), "/", String( n ), " - loss = ", String( loss[1] ), "\n" );
153157

154-
w := put( w );
155-
156158
#Display( w );
157159

158160
od;

gap/NeuralNetworks.gi

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ InstallMethod( LogitsMorphismOfNeuralNetwork,
1515

1616
N := Length( dims );
1717

18-
Print( "The total number of layers is ", String( N ), "\n\n" );
19-
2018
L := [ ];
2119

2220
for i in [ 1 .. N - 1 ] do
@@ -25,8 +23,6 @@ InstallMethod( LogitsMorphismOfNeuralNetwork,
2523

2624
P_i := UnderlyingObject( l_i );
2725

28-
Print( "Creating a morphism from layer ", String( i ), " to ", String( i + 1 ), " with ", String( RankOfObject( P_i ) ), " parameters\n" );
29-
3026
Add( L, l_i );
3127

3228
if i <> N - 1 then
@@ -37,8 +33,6 @@ InstallMethod( LogitsMorphismOfNeuralNetwork,
3733

3834
od;
3935

40-
Print( "\n" );
41-
4236
return PreComposeList( Para, L );
4337

4438
end );

tst/functor.tst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ gap> Smooth := SkeletalSmoothMaps;;
44
gap> Lenses := CategoryOfLenses( Smooth );;
55
gap> Para := CategoryOfParametrisedMorphisms( Smooth );;
66
gap> Para_Lenses := CategoryOfParametrisedMorphisms( Lenses );;
7-
gap> f := LossMorphismOfNeuralNetwork( Para, 2, [], 1, "IdFunc" );;
8-
The total number of layers is 2
9-
10-
Creating a morphism from layer 1 to 2 with 3 parameters
7+
gap> ell := LossMorphismOfNeuralNetwork( Para, 2, [], 1, "IdFunc" );;
118
gap> dummy_input := ConvertToExpressions( [ "w1", "w2", "b1", "x1", "x2", "y" ] );
129
[ w1, w2, b1, x1, x2, y ]
13-
gap> Display( f : dummy_input := dummy_input );
10+
gap> Display( ell : dummy_input := dummy_input );
1411
^3 ->^1 defined by:
1512

1613
Parameter Object:
@@ -24,7 +21,7 @@ Parametrised Morphism:
2421
‣ (w1 * x1 + w2 * x2 + b1 - y) ^ 2 / 1
2522
gap> R := EmbeddingIntoCategoryOfParametrisedMorphisms( Para, Para_Lenses );
2623
Embedding into category of parametrised morphisms
27-
gap> Rf := ApplyFunctor( R, f );
24+
gap> Rf := ApplyFunctor( R, ell );
2825
(ℝ^3, ℝ^3) -> (ℝ^1, ℝ^1) defined by:
2926

3027
Parameter Object:

tst/neural-network-1.tst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ gap> Smooth := SkeletalSmoothMaps;;
22
gap> Lenses := CategoryOfLenses( Smooth );;
33
gap> Para := CategoryOfParametrisedMorphisms( Smooth );;
44
gap> f := LossMorphismOfNeuralNetwork( Para, 2, [], 1, "IdFunc" );;
5-
The total number of layers is 2
6-
7-
Creating a morphism from layer 1 to 2 with 3 parameters
85
gap> optimizer := Lenses.AdamOptimizer( );;
96
gap> training_examples_path := SelectBasedOnCondition( IsExistingFile( "data-1.txt" ), "data-1.txt", "tst/data-1.txt" );;
107
gap> batch_size := 5;;
@@ -63,4 +60,4 @@ Epoch 47/50 - loss = 0.00092586526390837627
6360
Epoch 48/50 - loss = 0.00091733056032563621
6461
Epoch 49/50 - loss = 0.00091003319324738866
6562
Epoch 50/50 - loss = 0.00090379645850528724
66-
[ 1021, -0.00236067, -0.00633157, 0.000258869, 0.0135747, 0.0500079, 0.0310695, 2.00197, -2.99162, 0.997524 ]
63+
[ 1001, -0.0024867, -0.00684601, 0.000293851, 0.0138455, 0.051012, 0.0316896, 2.00229, -2.99088, 0.997374 ]

tst/neural-network-2.tst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ gap> Smooth := SkeletalSmoothMaps;;
22
gap> Lenses := CategoryOfLenses( Smooth );;
33
gap> Para := CategoryOfParametrisedMorphisms( Smooth );;
44
gap> f := LossMorphismOfNeuralNetwork( Para, 2, [ 5, 5 ], 4, "Softmax" );;
5-
The total number of layers is 4
6-
7-
Creating a morphism from layer 1 to 2 with 15 parameters
8-
Creating a morphism from layer 2 to 3 with 30 parameters
9-
Creating a morphism from layer 3 to 4 with 24 parameters
105
gap> optimizer := Lenses.GradientDescentOptimizer( : learning_rate := 0.01 );;
116
gap> training_examples_path := SelectBasedOnCondition( IsExistingFile( "data-2.txt" ), "data-2.txt", "tst/data-2.txt" );;
127
gap> batch_size := 1;;

0 commit comments

Comments
 (0)