Skip to content

Commit

Permalink
Merge pull request #7 from kamalsaleh/master
Browse files Browse the repository at this point in the history
change names of some attributes and include two examples in the readme file
  • Loading branch information
kamalsaleh authored Jul 23, 2024
2 parents c7c363c + fdc7b73 commit 631dfec
Show file tree
Hide file tree
Showing 14 changed files with 832 additions and 61 deletions.
2 changes: 1 addition & 1 deletion PackageInfo.g
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SetPackageInfo( rec(

PackageName := "MachineLearningForCAP",
Subtitle := "Exploring categorical machine learning in CAP",
Version := "2024.07-17",
Version := "2024.07-20",
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)( ),
License := "GPL-2.0-or-later",

Expand Down
499 changes: 499 additions & 0 deletions README.md

Large diffs are not rendered by default.

78 changes: 39 additions & 39 deletions examples/CategoryOfParametrisedMorphisms.g
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ f := Smooth.Softmax( 3 );
f := MorphismConstructor( Para, R1 / Para, [ R2, f ], R3 / Para );
#! ℝ^1 -> ℝ^3 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^2
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^3 -> ℝ^3
Display( f );
#! ℝ^1 -> ℝ^3 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^2
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^3 -> ℝ^3
#!
#! ‣ Exp( x1 ) / (Exp( x1 ) + Exp( x2 ) + Exp( x3 ))
Expand All @@ -59,22 +59,22 @@ Display( r );
g := ReparametriseMorphism( f, r );
#! ℝ^1 -> ℝ^3 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^2
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^3 -> ℝ^3
Display( g );
#! ℝ^1 -> ℝ^3 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^2
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^3 -> ℝ^3
#!
#! ‣ Exp( Sqrt( x1 ) ) / (Exp( Sqrt( x1 ) ) + Exp( Cos( x2 ) ) + Exp( x3 ))
Expand All @@ -83,32 +83,32 @@ Display( g );
l := Para.AffineTransformation( 3, 2 );
#! ℝ^3 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^8
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^11 -> ℝ^2
h := PreCompose( g, l );
#! ℝ^1 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^10
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^11 -> ℝ^2
Display( h );
#! ℝ^1 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^10
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^11 -> ℝ^2
#!
#! ‣ x1 * (Exp( Sqrt( x9 ) ) / (Exp( Sqrt( x9 ) ) + Exp( Cos( x10 ) ) + Exp( x11 )))
Expand All @@ -123,22 +123,22 @@ r := Smooth.Constant( constants );
t := ReparametriseMorphism( h, r );
#! ℝ^1 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^0
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^1 -> ℝ^2
Display( t );
#! ℝ^1 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^0
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^1 -> ℝ^2
#!
#! ‣ 0.91 * (2.39116 / (5.10727 + Exp( x1 ))) + 0.24 * (2.71611 / (5.10727 + Exp( x1 )))
Expand All @@ -148,22 +148,22 @@ Display( t );
s := SimplifyMorphism( t, infinity );
#! ℝ^1 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^0
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^1 -> ℝ^2
Display( s );
#! ℝ^1 -> ℝ^2 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^0
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^1 -> ℝ^2
#!
#! ‣ (1.47 * Exp( x1 ) + 5.84111) / (Exp( x1 ) + 5.10727)
Expand All @@ -175,22 +175,22 @@ ApplyFunctor( iota, Smooth.( 1 ) );
psi := ApplyFunctor( iota, Smooth.Sum( 2 ) );
#! ℝ^2 -> ℝ^1 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^0
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^2 -> ℝ^1
Print( DisplayString( psi ) );
#! ℝ^2 -> ℝ^1 defined by:
#!
#! Parameter Object:
#! Underlying Object:
#! -----------------
#! ℝ^0
#!
#! Parametrised Morphism:
#! ----------------------
#! Underlying Morphism:
#! -------------------
#! ℝ^2 -> ℝ^1
#!
#! ‣ x1 + x2
Expand Down
12 changes: 6 additions & 6 deletions gap/CategoryOfParametrisedMorphisms.gi
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,9 @@ InstallMethod( ViewString,
" -> ",
ViewString( Target( f ) ),
" defined by:",
"\n\nParameter Object:\n-----------------\n",
"\n\nUnderlying Object:\n-----------------\n",
ViewString( UnderlyingObject( f ) ),
"\n\nParametrised Morphism:\n----------------------\n",
"\n\nUnderlying Morphism:\n-------------------\n",
ViewString( UnderlyingMorphism( f ) ) );

end );
Expand All @@ -565,9 +565,9 @@ InstallMethod( DisplayString,
" -> ",
ViewString( Target( f ) ),
" defined by:",
"\n\nParameter Object:\n-----------------\n",
"\n\nUnderlying Object:\n-----------------\n",
ViewString( UnderlyingObject( f ) ),
"\n\nParametrised Morphism:\n----------------------\n",
"\n\nUnderlying Morphism:\n-------------------\n",
DisplayString( UnderlyingMorphism( f ) ) );

end );
Expand All @@ -584,9 +584,9 @@ InstallMethod( Display,
" -> ",
ViewString( Target( f ) ),
" defined by:",
"\n\nParameter Object:\n-----------------\n",
"\n\nUnderlying Object:\n-----------------\n",
ViewString( UnderlyingObject( f ) ),
"\n\nParametrised Morphism:\n----------------------\n" ) );
"\n\nUnderlying Morphism:\n-------------------\n" ) );

Display( UnderlyingMorphism( f ) );

Expand Down
Binary file added pictures/linear_regression.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/logistic_regression.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/network-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/network-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/training_data_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/training_data_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions tst/categorical_vs_direct_constructions.tst
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ gap> Assert( 0, Para.AffineTransformation_( 3, 4 ) = Para.AffineTransformation(
gap> l := Para.AffineTransformation( 3, 4 );
^3 ->^4 defined by:

Parameter Object:
Underlying Object:
-----------------
^16

Parametrised Morphism:
----------------------
Underlying Morphism:
-------------------
^19 ->^4
gap> dummy_input := DummyInputForAffineTransformation( 3, 4, "w", "b", "x" );;
gap> Display( l : dummy_input := dummy_input );
^3 ->^4 defined by:

Parameter Object:
Underlying Object:
-----------------
^16

Parametrised Morphism:
----------------------
Underlying Morphism:
-------------------
^19 ->^4

‣ w1_1 * x1 + w2_1 * x2 + w3_1 * x3 + b_1
Expand Down
113 changes: 113 additions & 0 deletions tst/example-in-readme-1.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
gap> LoadPackage( "MachineLearningForCAP" );
true

gap> Para := CategoryOfParametrisedMorphisms( SkeletalSmoothMaps );
CategoryOfParametrisedMorphisms( SkeletalSmoothMaps )

gap> training_set := [ [ 1, 2.9 ], [ 2, 5.1 ], [ 3, 7.05 ] ];
[ [ 1, 2.9 ], [ 2, 5.1 ], [ 3, 7.05 ] ]

gap> input_dim := 1;; output_dim := 1;; hidden_dims := [ ];;

gap> f := PredictionMorphismOfNeuralNetwork( Para, input_dim, hidden_dims, output_dim, "IdFunc" );;

gap> Display( f );
^1 ->^1 defined by:

Underlying Object:
-----------------
^2

Underlying Morphism:
-------------------
^3 ->^1

‣ x1 * x3 + x2

gap> parameters := [ 2, 1 ];; x := [ 2 ];;

gap> Eval( f, [ parameters, x ] );
[ 5 ]

gap> ell := LossMorphismOfNeuralNetwork( Para, input_dim, hidden_dims, output_dim, "IdFunc" );;

gap> Display( ell );
^2 ->^1 defined by:

Underlying Object:
-----------------
^2

Underlying Morphism:
-------------------
^4 ->^1

‣ (x1 * x3 + x2 - x4) ^ 2 / 1

gap> Lenses := CategoryOfLenses( SkeletalSmoothMaps );
CategoryOfLenses( SkeletalSmoothMaps )

gap> optimizer := Lenses.GradientDescentOptimizer( : learning_rate := 0.01 );;

gap> batch_size := 1;;

gap> one_epoch_update := OneEpochUpdateLens( ell, optimizer, training_set, batch_size );;

gap> parameters := [ 0.1, -0.1 ];;

gap> nr_epochs := 15;;

gap> parameters := Fit( one_epoch_update, nr_epochs, parameters );
Epoch 0/15 - loss = 26.777499999999993
Epoch 1/15 - loss = 13.002145872163197
Epoch 2/15 - loss = 6.3171942035316935
Epoch 3/15 - loss = 3.0722513061917534
Epoch 4/15 - loss = 1.4965356389126505
Epoch 5/15 - loss = 0.73097379078374358
Epoch 6/15 - loss = 0.35874171019291579
Epoch 7/15 - loss = 0.1775574969062125
Epoch 8/15 - loss = 0.089228700384937534
Epoch 9/15 - loss = 0.046072054531129378
Epoch 10/15 - loss = 0.024919378473509772
Epoch 11/15 - loss = 0.014504998499450883
Epoch 12/15 - loss = 0.0093448161379050161
Epoch 13/15 - loss = 0.0067649700132868147
Epoch 14/15 - loss = 0.0054588596501628835
Epoch 15/15 - loss = 0.0047859930295160499
[ 2.08995, 0.802632 ]

gap> theta := SkeletalSmoothMaps.Constant( parameters );
^0 ->^2

gap> Display( theta );
^0 ->^2

2.08995
0.802632

gap> f_theta := ReparametriseMorphism( f, theta );;

gap> Display( f_theta );
^1 ->^1 defined by:

Underlying Object:
-----------------
^0

Underlying Morphism:
-------------------
^1 ->^1

2.08995 * x1 + 0.802632

gap> f_theta := UnderlyingMorphism( f_theta );
^1 ->^1

gap> Eval( f_theta, [ 1 ] );
[ 2.89259 ]

gap> Eval( f_theta, [ 2 ] );
[ 4.98254 ]

gap> Eval( f_theta, [ 3 ] );
[ 7.07249 ]
Loading

0 comments on commit 631dfec

Please sign in to comment.