From 44c131835021d1467abe3dbbf2fc4952579b0cd3 Mon Sep 17 00:00:00 2001 From: Kamal Saleh Date: Tue, 9 Jul 2024 12:17:34 +0200 Subject: [PATCH 1/4] disable uploading codecoverage on PR's (takes too long) --- .github/workflows/Tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 031f65e..e6dcd76 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -77,14 +77,14 @@ jobs: CUR_SHA=$(git rev-parse --verify HEAD) git fetch origin gh-pages git worktree add gh-pages/ gh-pages || (echo "There was an error. Make sure there is a branch named 'gh-pages'. See https://github.com/homalg-project/PackageJanitor#error-there-was-an-error-make-sure-there-is-a-branch-named-gh-pages"; exit 1) - if [ "${{ matrix.image }}" = "ghcr.io/homalg-project/gap-docker:latest" ] && [ "$CUR_SHA" = "$(git rev-parse origin/master)" ] && [ $(dirname "$GITHUB_REPOSITORY") = "homalg-project" ]; then \ + if [ "${{ matrix.image }}" = "ghcr.io/kamalsaleh/gap-sympy-docker:latest" ] && [ "$CUR_SHA" = "$(git rev-parse origin/master)" ] && [ $(dirname "$GITHUB_REPOSITORY") = "homalg-project" ]; then \ git checkout master; \ TOKEN="${{ secrets.GITHUB_TOKEN }}" SUBSPLIT_PUSH_SECRET="${{ secrets.SUBSPLIT_PUSH_SECRET }}" ./dev/make_dist.sh; \ else \ TOKEN="${{ secrets.GITHUB_TOKEN }}" ./dev/simulate_dist.sh; \ fi - name: Upload code coverage - if: github.event_name != 'schedule' && matrix.image == 'ghcr.io/kamalsaleh/gap-sympy-docker:latest' + if: github.event_name != 'schedule' && github.event_name != 'pull_request' && matrix.image == 'ghcr.io/kamalsaleh/gap-sympy-docker:latest' env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} run: | From 991d7ec8e84d5351f66e8a570f9f2f560647f107 Mon Sep 17 00:00:00 2001 From: Kamal Saleh Date: Tue, 9 Jul 2024 11:52:16 +0200 Subject: [PATCH 2/4] bug fix in TensorProductOnMorphisms --- PackageInfo.g | 2 +- gap/CategoryOfLenses.gi | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/PackageInfo.g b/PackageInfo.g index 8ba13f8..7415c2f 100644 --- a/PackageInfo.g +++ b/PackageInfo.g @@ -10,7 +10,7 @@ SetPackageInfo( rec( PackageName := "MachineLearningForCAP", Subtitle := "Exploring categorical machine learning in CAP", -Version := "2024.07-02", +Version := "2024.07-03", 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", diff --git a/gap/CategoryOfLenses.gi b/gap/CategoryOfLenses.gi index 216cfe2..28cc569 100644 --- a/gap/CategoryOfLenses.gi +++ b/gap/CategoryOfLenses.gi @@ -236,15 +236,16 @@ InstallMethod( CategoryOfLenses, C := UnderlyingCategory( Lenses ); - f := UnderlyingPairOfMorphisms( f ); - g := UnderlyingPairOfMorphisms( g ); - A := UnderlyingPairOfObjects( Source( f ) ); B := UnderlyingPairOfObjects( Target( f ) ); + f := UnderlyingPairOfMorphisms( f ); + U := UnderlyingPairOfObjects( Source( g ) ); V := UnderlyingPairOfObjects( Target( g ) ); + g := UnderlyingPairOfMorphisms( g ); + get := DirectProductFunctorial( C, [ f[1], g[1] ] ); put := PreCompose( C, From a1b2fbee5431160efed5f18af14aeaf4a075f81f Mon Sep 17 00:00:00 2001 From: Kamal Saleh Date: Tue, 9 Jul 2024 11:52:53 +0200 Subject: [PATCH 3/4] set the default value of momentum to 0.9 --- PackageInfo.g | 2 +- gap/CategoryOfLenses.gi | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/PackageInfo.g b/PackageInfo.g index 7415c2f..c05bb33 100644 --- a/PackageInfo.g +++ b/PackageInfo.g @@ -10,7 +10,7 @@ SetPackageInfo( rec( PackageName := "MachineLearningForCAP", Subtitle := "Exploring categorical machine learning in CAP", -Version := "2024.07-03", +Version := "2024.07-04", 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", diff --git a/gap/CategoryOfLenses.gi b/gap/CategoryOfLenses.gi index 28cc569..4d46ad8 100644 --- a/gap/CategoryOfLenses.gi +++ b/gap/CategoryOfLenses.gi @@ -482,7 +482,7 @@ InstallOtherMethod( \., local learning_rate, momentum; learning_rate := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "learning_rate", 0.01 ); - momentum := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "momentum", 0 ); + momentum := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "momentum", 0.9 ); return # both are non-negative @@ -529,7 +529,7 @@ InstallOtherMethod( \., local learning_rate, momentum; learning_rate := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "learning_rate", 0.01 ); - momentum := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "momentum", 0 ); + momentum := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "momentum", 0.9 ); return function ( n ) From cf11053e8eb527dc4f249800cc310ef742b046bf Mon Sep 17 00:00:00 2001 From: Kamal Saleh Date: Tue, 9 Jul 2024 11:54:00 +0200 Subject: [PATCH 4/4] add more examples --- examples/CategoryOfLenses.g | 330 +++++++++++++++++++++ examples/CategoryOfParametrisedMorphisms.g | 46 +-- tst/neural-network-2.tst | 8 +- 3 files changed, 356 insertions(+), 28 deletions(-) create mode 100644 examples/CategoryOfLenses.g diff --git a/examples/CategoryOfLenses.g b/examples/CategoryOfLenses.g new file mode 100644 index 0000000..9a946ab --- /dev/null +++ b/examples/CategoryOfLenses.g @@ -0,0 +1,330 @@ +#! @Chapter Examples and Tests + +#! @Section Category of Lenses + +LoadPackage( "MachineLearningForCAP" ); + +#! @Example +Smooth := CategoryOfSkeletalSmoothMaps( ); +#! SkeletalSmoothMaps +Lenses := CategoryOfLenses( Smooth ); +#! CategoryOfLenses( SkeletalSmoothMaps ) +A := ObjectConstructor( Lenses, [ Smooth.( 1 ), Smooth.( 2 ) ] ); +#! (ℝ^1, ℝ^2) +IsWellDefined( A ); +#! true +CapCategory( A ); +#! CategoryOfLenses( SkeletalSmoothMaps ) +A_datum := ObjectDatum( A ); +#! [ ℝ^1, ℝ^2 ] +CapCategory( A_datum[1] ); +#! SkeletalSmoothMaps +B := ObjectConstructor( Lenses, [ Smooth.( 3 ), Smooth.( 4 ) ] ); +#! (ℝ^3, ℝ^4) +get := RandomMorphism( Smooth.( 1 ), Smooth.( 3 ), 5 ); +#! ℝ^1 -> ℝ^3 +put := RandomMorphism( Smooth.( 1 + 4 ), Smooth.( 2 ), 5 ); +#! ℝ^5 -> ℝ^2 +f := MorphismConstructor( Lenses, A, [ get, put ], B ); +#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^1 -> ℝ^3 +#! +#! Put Morphism: +#! ---------- +#! ℝ^5 -> ℝ^2 +MorphismDatum( f ); +#! [ ℝ^1 -> ℝ^3, ℝ^5 -> ℝ^2 ] +IsWellDefined( f ); +#! true +Display( f ); +#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^1 -> ℝ^3 +#! +#! ‣ 0.766 * x1 ^ 4 + 0.234 +#! ‣ 1. * x1 ^ 4 + 0.388 +#! ‣ 0.459 * x1 ^ 4 + 0.278 +#! +#! Put Morphism: +#! ------------ +#! ℝ^5 -> ℝ^2 +#! +#! ‣ 0.677 * x1 ^ 5 + 0.19 * x2 ^ 4 + 0.659 * x3 ^ 4 +#! + 0.859 * x4 ^ 5 + 0.28 * x5 ^ 1 + 0.216 +#! ‣ 0.37 * x1 ^ 5 + 0.571 * x2 ^ 4 + 0.835 * x3 ^ 4 +#! + 0.773 * x4 ^ 5 + 0.469 * x5 ^ 1 + 0.159 +id_A := IdentityMorphism( Lenses, A ); +#! (ℝ^1, ℝ^2) -> (ℝ^1, ℝ^2) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^1 -> ℝ^1 +#! +#! Put Morphism: +#! ---------- +#! ℝ^3 -> ℝ^2 +Display( id_A ); +#! (ℝ^1, ℝ^2) -> (ℝ^1, ℝ^2) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^1 -> ℝ^1 +#! +#! ‣ x1 +#! +#! Put Morphism: +#! ------------ +#! ℝ^3 -> ℝ^2 +#! +#! ‣ x2 +#! ‣ x3 +IsCongruentForMorphisms( PreCompose( id_A, f ), f ); +#! true +TensorUnit( Lenses ); +#! (ℝ^0, ℝ^0) +TensorProductOnObjects( A, B ); +#! (ℝ^4, ℝ^6) +f1 := RandomMorphism( A, B, 5 ); +#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^1 -> ℝ^3 +#! +#! Put Morphism: +#! ---------- +#! ℝ^5 -> ℝ^2 +f2 := RandomMorphism( A, B, 5 ); +#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^1 -> ℝ^3 +#! +#! Put Morphism: +#! ---------- +#! ℝ^5 -> ℝ^2 +f3 := RandomMorphism( A, B, 5 ); +#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^1 -> ℝ^3 +#! +#! Put Morphism: +#! ---------- +#! ℝ^5 -> ℝ^2 +f1_f2 := TensorProductOnMorphisms( Lenses, f1, f2 ); +#! (ℝ^2, ℝ^4) -> (ℝ^6, ℝ^8) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^2 -> ℝ^6 +#! +#! Put Morphism: +#! ---------- +#! ℝ^10 -> ℝ^4 +f2_f3 := TensorProductOnMorphisms( Lenses, f2, f3 ); +#! (ℝ^2, ℝ^4) -> (ℝ^6, ℝ^8) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^2 -> ℝ^6 +#! +#! Put Morphism: +#! ---------- +#! ℝ^10 -> ℝ^4 +t1 := TensorProductOnMorphisms( Lenses, f1_f2, f3 ); +#! (ℝ^3, ℝ^6) -> (ℝ^9, ℝ^12) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^3 -> ℝ^9 +#! +#! Put Morphism: +#! ---------- +#! ℝ^15 -> ℝ^6 +t2 := TensorProductOnMorphisms( Lenses, f1, f2_f3 ); +#! (ℝ^3, ℝ^6) -> (ℝ^9, ℝ^12) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^3 -> ℝ^9 +#! +#! Put Morphism: +#! ---------- +#! ℝ^15 -> ℝ^6 +IsCongruentForMorphisms( t1, t2 ); +#! true +Display( Braiding( A, B ) ); +#! (ℝ^4, ℝ^6) -> (ℝ^4, ℝ^6) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^4 -> ℝ^4 +#! +#! ‣ x2 +#! ‣ x3 +#! ‣ x4 +#! ‣ x1 +#! +#! Put Morphism: +#! ------------ +#! ℝ^10 -> ℝ^6 +#! +#! ‣ x9 +#! ‣ x10 +#! ‣ x5 +#! ‣ x6 +#! ‣ x7 +#! ‣ x8 +Display( PreCompose( Braiding( A, B ), BraidingInverse( A, B ) ) ); +#! (ℝ^4, ℝ^6) -> (ℝ^4, ℝ^6) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^4 -> ℝ^4 +#! +#! ‣ x1 +#! ‣ x2 +#! ‣ x3 +#! ‣ x4 +#! +#! Put Morphism: +#! ------------ +#! ℝ^10 -> ℝ^6 +#! +#! ‣ x5 +#! ‣ x6 +#! ‣ x7 +#! ‣ x8 +#! ‣ x9 +#! ‣ x10 +R := EmbeddingIntoCategoryOfLenses( Smooth, Lenses ); +#! Embedding functor into category of lenses +SourceOfFunctor( R ); +#! SkeletalSmoothMaps +RangeOfFunctor( R ); +#! CategoryOfLenses( SkeletalSmoothMaps ) +f := Smooth.Softmax( 2 ); +#! ℝ^2 -> ℝ^2 +Display( f ); +#! ℝ^2 -> ℝ^2 +#! +#! ‣ Exp( x1 ) / (Exp( x1 ) + Exp( x2 )) +#! ‣ Exp( x2 ) / (Exp( x1 ) + Exp( x2 )) +Rf := ApplyFunctor( R, f ); +#! (ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by: +#! +#! Get Morphism: +#! ---------- +#! ℝ^2 -> ℝ^2 +#! +#! Put Morphism: +#! ---------- +#! ℝ^4 -> ℝ^2 +Display( Rf ); +#! (ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^2 -> ℝ^2 +#! +#! ‣ Exp( x1 ) / (Exp( x1 ) + Exp( x2 )) +#! ‣ Exp( x2 ) / (Exp( x1 ) + Exp( x2 )) +#! +#! Put Morphism: +#! ------------ +#! ℝ^4 -> ℝ^2 +#! +#! ‣ x3 * +#! ((Exp( x1 ) + Exp( x2 ) - Exp( x1 )) * (Exp( x1 ) / (Exp( x1 ) + Exp( x2 )) ^ 2)) +#! + x4 * ((- Exp( x1 )) * (Exp( x2 ) / (Exp( x1 ) + Exp( x2 )) ^ 2)) +#! ‣ x3 * ((- Exp( x2 )) * (Exp( x1 ) / (Exp( x1 ) + Exp( x2 )) ^ 2)) + +#! x4 * +#! ((Exp( x1 ) + Exp( x2 ) - Exp( x2 )) * (Exp( x2 ) / (Exp( x1 ) + Exp( x2 )) ^ 2)) +Display( Lenses.GradientDescentOptimizer( :learning_rate := 0.01 )( 2 ) ); +#! (ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^2 -> ℝ^2 +#! +#! ‣ x1 +#! ‣ x2 +#! +#! Put Morphism: +#! ------------ +#! ℝ^4 -> ℝ^2 +#! +#! ‣ x1 + 0.01 * x3 +#! ‣ x2 + 0.01 * x4 +Display( Lenses.GradientDescentWithMomentumOptimizer( + :learning_rate := 0.01, momentum := 0.9 )( 2 ) ); +#! (ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^4 -> ℝ^2 +#! +#! ‣ x3 +#! ‣ x4 +#! +#! Put Morphism: +#! ------------ +#! ℝ^6 -> ℝ^4 +#! +#! ‣ 0.9 * x1 + 0.01 * x5 +#! ‣ 0.9 * x2 + 0.01 * x6 +#! ‣ x3 + (0.9 * x1 + 0.01 * x5) +#! ‣ x4 + (0.9 * x2 + 0.01 * x6) +Display( Lenses.AdagradOptimizer( :learning_rate := 0.01 )( 2 ) ); +#! (ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^4 -> ℝ^2 +#! +#! ‣ x3 +#! ‣ x4 +#! +#! Put Morphism: +#! ------------ +#! ℝ^6 -> ℝ^4 +#! +#! ‣ x1 + x5 ^ 2 +#! ‣ x2 + x6 ^ 2 +#! ‣ x3 + 0.01 * x5 / (1.e-07 + Sqrt( (x1 + x5 ^ 2) )) +#! ‣ x4 + 0.01 * x6 / (1.e-07 + Sqrt( (x2 + x6 ^ 2) )) +Display( Lenses.AdamOptimizer( + :learning_rate := 0.01, beta_1 := 0.9, beta_2 := 0.999 )( 2 ) ); +#! (ℝ^7, ℝ^7) -> (ℝ^2, ℝ^2) defined by: +#! +#! Get Morphism: +#! ------------ +#! ℝ^7 -> ℝ^2 +#! +#! ‣ x6 +#! ‣ x7 +#! +#! Put Morphism: +#! ------------ +#! ℝ^9 -> ℝ^7 +#! +#! ‣ x1 + 1 +#! ‣ 0.9 * x2 + 0.1 * x8 +#! ‣ 0.9 * x3 + 0.1 * x9 +#! ‣ 0.999 * x4 + 0.001 * x8 ^ 2 +#! ‣ 0.999 * x5 + 0.001 * x9 ^ 2 +#! ‣ x6 + 0.01 / (1 - 0.999 ^ x1) +#! * ((0.9 * x2 + 0.1 * x8) / +#! (1.e-07 + Sqrt( (0.999 * x4 + 0.001 * x8 ^ 2) / (1 - 0.999 ^ x1) ))) +#! ‣ x7 + 0.01 / (1 - 0.999 ^ x1) +#! * ((0.9 * x3 + 0.1 * x9) / +#! (1.e-07 + Sqrt( (0.999 * x5 + 0.001 * x9 ^ 2) / (1 - 0.999 ^ x1) ))) +#! @EndExample diff --git a/examples/CategoryOfParametrisedMorphisms.g b/examples/CategoryOfParametrisedMorphisms.g index 1cebd2f..88e7fa2 100644 --- a/examples/CategoryOfParametrisedMorphisms.g +++ b/examples/CategoryOfParametrisedMorphisms.g @@ -137,27 +137,27 @@ Display( t ); #! + 0.88 * (Exp( x1 ) / (5.10727 + Exp( x1 ))) + 0.59 #! ‣ 0.67 * (2.39116 / (5.10727 + Exp( x1 ))) + 0.05 * (2.71611 / (5.10727 + Exp( x1 ))) #! + 0.85 * (Exp( x1 ) / (5.10727 + Exp( x1 ))) + 0.31 -# gap> s := SimplifyMorphism( t, infinity ); -# ℝ^1 -> ℝ^2 defined by: -# -# Parameter Object: -# ----------------- -# ℝ^0 -# -# Parametrised Morphism: -# ---------------------- -# ℝ^1 -> ℝ^2 -# gap> Display( s ); -# ℝ^1 -> ℝ^2 defined by: -# -# Parameter Object: -# ----------------- -# ℝ^0 -# -# Parametrised Morphism: -# ---------------------- -# ℝ^1 -> ℝ^2 -# -# ‣ (1.47 * Exp( x1 ) + 5.84111) / (Exp( x1 ) + 5.10727) -# ‣ (1.16 * Exp( x1 ) + 3.32114) / (Exp( x1 ) + 5.10727) +s := SimplifyMorphism( t, infinity ); +#! ℝ^1 -> ℝ^2 defined by: +#! +#! Parameter Object: +#! ----------------- +#! ℝ^0 +#! +#! Parametrised Morphism: +#! ---------------------- +#! ℝ^1 -> ℝ^2 +#! gap> Display( s ); +#! ℝ^1 -> ℝ^2 defined by: +#! +#! Parameter Object: +#! ----------------- +#! ℝ^0 +#! +#! Parametrised Morphism: +#! ---------------------- +#! ℝ^1 -> ℝ^2 +#! +#! ‣ (1.47 * Exp( x1 ) + 5.84111) / (Exp( x1 ) + 5.10727) +#! ‣ (1.16 * Exp( x1 ) + 3.32114) / (Exp( x1 ) + 5.10727) #! @EndExample diff --git a/tst/neural-network-2.tst b/tst/neural-network-2.tst index 1bdf16d..4af8750 100644 --- a/tst/neural-network-2.tst +++ b/tst/neural-network-2.tst @@ -18,9 +18,7 @@ gap> w2 := Concatenation( TransposedMat( w2 ) );; gap> w3 := [[-0.05885905, -0.81396204, 0.00370395, -0.42547446], [-0.39928403, 0.56314194, 0.6614479 , 0.5060446 ], [ 0.6662301, -0.2800727 , 0.1187852 , -0.27065504], [ 0.15874296, -0.6039741 , -0.7533438 , -0.33242884], [ 0.26578736, -0.45036432, -0.61879224, 0.8060001 ], [ 0., 0., 0., 0., ] ];; gap> w3 := Concatenation( TransposedMat( w3 ) );; gap> w := Concatenation( [ w3, w2, w1 ] );; -gap> nr_epochs := 3;; +gap> nr_epochs := 1;; gap> w := Fit( one_epoch_update, nr_epochs, w );; -Epoch 0/3 - loss = 0.36090265698232782 -Epoch 1/3 - loss = 0.21968044680244986 -Epoch 2/3 - loss = 0.17883151112398918 -Epoch 3/3 - loss = 0.15935373922266871 +Epoch 0/1 - loss = 0.36090265698232782 +Epoch 1/1 - loss = 0.21968044680244986