Skip to content

Commit

Permalink
add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalsaleh committed Jul 9, 2024
1 parent a1b2fbe commit cf11053
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 28 deletions.
330 changes: 330 additions & 0 deletions examples/CategoryOfLenses.g
Original file line number Diff line number Diff line change
@@ -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
46 changes: 23 additions & 23 deletions examples/CategoryOfParametrisedMorphisms.g
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions tst/neural-network-2.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cf11053

Please sign in to comment.