From cf11053e8eb527dc4f249800cc310ef742b046bf Mon Sep 17 00:00:00 2001 From: Kamal Saleh Date: Tue, 9 Jul 2024 11:54:00 +0200 Subject: [PATCH] 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