Skip to content

Commit a342ff5

Browse files
authored
Merge pull request #1 from kamalsaleh/master
Bug fix & add example for the category of lenses
2 parents cc0c9bf + cf11053 commit a342ff5

File tree

6 files changed

+365
-36
lines changed

6 files changed

+365
-36
lines changed

.github/workflows/Tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ jobs:
7777
CUR_SHA=$(git rev-parse --verify HEAD)
7878
git fetch origin gh-pages
7979
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)
80-
if [ "${{ matrix.image }}" = "ghcr.io/homalg-project/gap-docker:latest" ] && [ "$CUR_SHA" = "$(git rev-parse origin/master)" ] && [ $(dirname "$GITHUB_REPOSITORY") = "homalg-project" ]; then \
80+
if [ "${{ matrix.image }}" = "ghcr.io/kamalsaleh/gap-sympy-docker:latest" ] && [ "$CUR_SHA" = "$(git rev-parse origin/master)" ] && [ $(dirname "$GITHUB_REPOSITORY") = "homalg-project" ]; then \
8181
git checkout master; \
8282
TOKEN="${{ secrets.GITHUB_TOKEN }}" SUBSPLIT_PUSH_SECRET="${{ secrets.SUBSPLIT_PUSH_SECRET }}" ./dev/make_dist.sh; \
8383
else \
8484
TOKEN="${{ secrets.GITHUB_TOKEN }}" ./dev/simulate_dist.sh; \
8585
fi
8686
- name: Upload code coverage
87-
if: github.event_name != 'schedule' && matrix.image == 'ghcr.io/kamalsaleh/gap-sympy-docker:latest'
87+
if: github.event_name != 'schedule' && github.event_name != 'pull_request' && matrix.image == 'ghcr.io/kamalsaleh/gap-sympy-docker:latest'
8888
env:
8989
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
9090
run: |

PackageInfo.g

+1-1
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-02",
13+
Version := "2024.07-04",
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

examples/CategoryOfLenses.g

+330
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
#! @Chapter Examples and Tests
2+
3+
#! @Section Category of Lenses
4+
5+
LoadPackage( "MachineLearningForCAP" );
6+
7+
#! @Example
8+
Smooth := CategoryOfSkeletalSmoothMaps( );
9+
#! SkeletalSmoothMaps
10+
Lenses := CategoryOfLenses( Smooth );
11+
#! CategoryOfLenses( SkeletalSmoothMaps )
12+
A := ObjectConstructor( Lenses, [ Smooth.( 1 ), Smooth.( 2 ) ] );
13+
#! (ℝ^1, ℝ^2)
14+
IsWellDefined( A );
15+
#! true
16+
CapCategory( A );
17+
#! CategoryOfLenses( SkeletalSmoothMaps )
18+
A_datum := ObjectDatum( A );
19+
#! [ ℝ^1, ℝ^2 ]
20+
CapCategory( A_datum[1] );
21+
#! SkeletalSmoothMaps
22+
B := ObjectConstructor( Lenses, [ Smooth.( 3 ), Smooth.( 4 ) ] );
23+
#! (ℝ^3, ℝ^4)
24+
get := RandomMorphism( Smooth.( 1 ), Smooth.( 3 ), 5 );
25+
#! ℝ^1 -> ℝ^3
26+
put := RandomMorphism( Smooth.( 1 + 4 ), Smooth.( 2 ), 5 );
27+
#! ℝ^5 -> ℝ^2
28+
f := MorphismConstructor( Lenses, A, [ get, put ], B );
29+
#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:
30+
#!
31+
#! Get Morphism:
32+
#! ----------
33+
#! ℝ^1 -> ℝ^3
34+
#!
35+
#! Put Morphism:
36+
#! ----------
37+
#! ℝ^5 -> ℝ^2
38+
MorphismDatum( f );
39+
#! [ ℝ^1 -> ℝ^3, ℝ^5 -> ℝ^2 ]
40+
IsWellDefined( f );
41+
#! true
42+
Display( f );
43+
#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:
44+
#!
45+
#! Get Morphism:
46+
#! ------------
47+
#! ℝ^1 -> ℝ^3
48+
#!
49+
#! ‣ 0.766 * x1 ^ 4 + 0.234
50+
#! ‣ 1. * x1 ^ 4 + 0.388
51+
#! ‣ 0.459 * x1 ^ 4 + 0.278
52+
#!
53+
#! Put Morphism:
54+
#! ------------
55+
#! ℝ^5 -> ℝ^2
56+
#!
57+
#! ‣ 0.677 * x1 ^ 5 + 0.19 * x2 ^ 4 + 0.659 * x3 ^ 4
58+
#! + 0.859 * x4 ^ 5 + 0.28 * x5 ^ 1 + 0.216
59+
#! ‣ 0.37 * x1 ^ 5 + 0.571 * x2 ^ 4 + 0.835 * x3 ^ 4
60+
#! + 0.773 * x4 ^ 5 + 0.469 * x5 ^ 1 + 0.159
61+
id_A := IdentityMorphism( Lenses, A );
62+
#! (ℝ^1, ℝ^2) -> (ℝ^1, ℝ^2) defined by:
63+
#!
64+
#! Get Morphism:
65+
#! ----------
66+
#! ℝ^1 -> ℝ^1
67+
#!
68+
#! Put Morphism:
69+
#! ----------
70+
#! ℝ^3 -> ℝ^2
71+
Display( id_A );
72+
#! (ℝ^1, ℝ^2) -> (ℝ^1, ℝ^2) defined by:
73+
#!
74+
#! Get Morphism:
75+
#! ------------
76+
#! ℝ^1 -> ℝ^1
77+
#!
78+
#! ‣ x1
79+
#!
80+
#! Put Morphism:
81+
#! ------------
82+
#! ℝ^3 -> ℝ^2
83+
#!
84+
#! ‣ x2
85+
#! ‣ x3
86+
IsCongruentForMorphisms( PreCompose( id_A, f ), f );
87+
#! true
88+
TensorUnit( Lenses );
89+
#! (ℝ^0, ℝ^0)
90+
TensorProductOnObjects( A, B );
91+
#! (ℝ^4, ℝ^6)
92+
f1 := RandomMorphism( A, B, 5 );
93+
#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:
94+
#!
95+
#! Get Morphism:
96+
#! ----------
97+
#! ℝ^1 -> ℝ^3
98+
#!
99+
#! Put Morphism:
100+
#! ----------
101+
#! ℝ^5 -> ℝ^2
102+
f2 := RandomMorphism( A, B, 5 );
103+
#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:
104+
#!
105+
#! Get Morphism:
106+
#! ----------
107+
#! ℝ^1 -> ℝ^3
108+
#!
109+
#! Put Morphism:
110+
#! ----------
111+
#! ℝ^5 -> ℝ^2
112+
f3 := RandomMorphism( A, B, 5 );
113+
#! (ℝ^1, ℝ^2) -> (ℝ^3, ℝ^4) defined by:
114+
#!
115+
#! Get Morphism:
116+
#! ----------
117+
#! ℝ^1 -> ℝ^3
118+
#!
119+
#! Put Morphism:
120+
#! ----------
121+
#! ℝ^5 -> ℝ^2
122+
f1_f2 := TensorProductOnMorphisms( Lenses, f1, f2 );
123+
#! (ℝ^2, ℝ^4) -> (ℝ^6, ℝ^8) defined by:
124+
#!
125+
#! Get Morphism:
126+
#! ----------
127+
#! ℝ^2 -> ℝ^6
128+
#!
129+
#! Put Morphism:
130+
#! ----------
131+
#! ℝ^10 -> ℝ^4
132+
f2_f3 := TensorProductOnMorphisms( Lenses, f2, f3 );
133+
#! (ℝ^2, ℝ^4) -> (ℝ^6, ℝ^8) defined by:
134+
#!
135+
#! Get Morphism:
136+
#! ----------
137+
#! ℝ^2 -> ℝ^6
138+
#!
139+
#! Put Morphism:
140+
#! ----------
141+
#! ℝ^10 -> ℝ^4
142+
t1 := TensorProductOnMorphisms( Lenses, f1_f2, f3 );
143+
#! (ℝ^3, ℝ^6) -> (ℝ^9, ℝ^12) defined by:
144+
#!
145+
#! Get Morphism:
146+
#! ----------
147+
#! ℝ^3 -> ℝ^9
148+
#!
149+
#! Put Morphism:
150+
#! ----------
151+
#! ℝ^15 -> ℝ^6
152+
t2 := TensorProductOnMorphisms( Lenses, f1, f2_f3 );
153+
#! (ℝ^3, ℝ^6) -> (ℝ^9, ℝ^12) defined by:
154+
#!
155+
#! Get Morphism:
156+
#! ----------
157+
#! ℝ^3 -> ℝ^9
158+
#!
159+
#! Put Morphism:
160+
#! ----------
161+
#! ℝ^15 -> ℝ^6
162+
IsCongruentForMorphisms( t1, t2 );
163+
#! true
164+
Display( Braiding( A, B ) );
165+
#! (ℝ^4, ℝ^6) -> (ℝ^4, ℝ^6) defined by:
166+
#!
167+
#! Get Morphism:
168+
#! ------------
169+
#! ℝ^4 -> ℝ^4
170+
#!
171+
#! ‣ x2
172+
#! ‣ x3
173+
#! ‣ x4
174+
#! ‣ x1
175+
#!
176+
#! Put Morphism:
177+
#! ------------
178+
#! ℝ^10 -> ℝ^6
179+
#!
180+
#! ‣ x9
181+
#! ‣ x10
182+
#! ‣ x5
183+
#! ‣ x6
184+
#! ‣ x7
185+
#! ‣ x8
186+
Display( PreCompose( Braiding( A, B ), BraidingInverse( A, B ) ) );
187+
#! (ℝ^4, ℝ^6) -> (ℝ^4, ℝ^6) defined by:
188+
#!
189+
#! Get Morphism:
190+
#! ------------
191+
#! ℝ^4 -> ℝ^4
192+
#!
193+
#! ‣ x1
194+
#! ‣ x2
195+
#! ‣ x3
196+
#! ‣ x4
197+
#!
198+
#! Put Morphism:
199+
#! ------------
200+
#! ℝ^10 -> ℝ^6
201+
#!
202+
#! ‣ x5
203+
#! ‣ x6
204+
#! ‣ x7
205+
#! ‣ x8
206+
#! ‣ x9
207+
#! ‣ x10
208+
R := EmbeddingIntoCategoryOfLenses( Smooth, Lenses );
209+
#! Embedding functor into category of lenses
210+
SourceOfFunctor( R );
211+
#! SkeletalSmoothMaps
212+
RangeOfFunctor( R );
213+
#! CategoryOfLenses( SkeletalSmoothMaps )
214+
f := Smooth.Softmax( 2 );
215+
#! ℝ^2 -> ℝ^2
216+
Display( f );
217+
#! ℝ^2 -> ℝ^2
218+
#!
219+
#! ‣ Exp( x1 ) / (Exp( x1 ) + Exp( x2 ))
220+
#! ‣ Exp( x2 ) / (Exp( x1 ) + Exp( x2 ))
221+
Rf := ApplyFunctor( R, f );
222+
#! (ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by:
223+
#!
224+
#! Get Morphism:
225+
#! ----------
226+
#! ℝ^2 -> ℝ^2
227+
#!
228+
#! Put Morphism:
229+
#! ----------
230+
#! ℝ^4 -> ℝ^2
231+
Display( Rf );
232+
#! (ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by:
233+
#!
234+
#! Get Morphism:
235+
#! ------------
236+
#! ℝ^2 -> ℝ^2
237+
#!
238+
#! ‣ Exp( x1 ) / (Exp( x1 ) + Exp( x2 ))
239+
#! ‣ Exp( x2 ) / (Exp( x1 ) + Exp( x2 ))
240+
#!
241+
#! Put Morphism:
242+
#! ------------
243+
#! ℝ^4 -> ℝ^2
244+
#!
245+
#! ‣ x3 *
246+
#! ((Exp( x1 ) + Exp( x2 ) - Exp( x1 )) * (Exp( x1 ) / (Exp( x1 ) + Exp( x2 )) ^ 2))
247+
#! + x4 * ((- Exp( x1 )) * (Exp( x2 ) / (Exp( x1 ) + Exp( x2 )) ^ 2))
248+
#! ‣ x3 * ((- Exp( x2 )) * (Exp( x1 ) / (Exp( x1 ) + Exp( x2 )) ^ 2)) +
249+
#! x4 *
250+
#! ((Exp( x1 ) + Exp( x2 ) - Exp( x2 )) * (Exp( x2 ) / (Exp( x1 ) + Exp( x2 )) ^ 2))
251+
Display( Lenses.GradientDescentOptimizer( :learning_rate := 0.01 )( 2 ) );
252+
#! (ℝ^2, ℝ^2) -> (ℝ^2, ℝ^2) defined by:
253+
#!
254+
#! Get Morphism:
255+
#! ------------
256+
#! ℝ^2 -> ℝ^2
257+
#!
258+
#! ‣ x1
259+
#! ‣ x2
260+
#!
261+
#! Put Morphism:
262+
#! ------------
263+
#! ℝ^4 -> ℝ^2
264+
#!
265+
#! ‣ x1 + 0.01 * x3
266+
#! ‣ x2 + 0.01 * x4
267+
Display( Lenses.GradientDescentWithMomentumOptimizer(
268+
:learning_rate := 0.01, momentum := 0.9 )( 2 ) );
269+
#! (ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by:
270+
#!
271+
#! Get Morphism:
272+
#! ------------
273+
#! ℝ^4 -> ℝ^2
274+
#!
275+
#! ‣ x3
276+
#! ‣ x4
277+
#!
278+
#! Put Morphism:
279+
#! ------------
280+
#! ℝ^6 -> ℝ^4
281+
#!
282+
#! ‣ 0.9 * x1 + 0.01 * x5
283+
#! ‣ 0.9 * x2 + 0.01 * x6
284+
#! ‣ x3 + (0.9 * x1 + 0.01 * x5)
285+
#! ‣ x4 + (0.9 * x2 + 0.01 * x6)
286+
Display( Lenses.AdagradOptimizer( :learning_rate := 0.01 )( 2 ) );
287+
#! (ℝ^4, ℝ^4) -> (ℝ^2, ℝ^2) defined by:
288+
#!
289+
#! Get Morphism:
290+
#! ------------
291+
#! ℝ^4 -> ℝ^2
292+
#!
293+
#! ‣ x3
294+
#! ‣ x4
295+
#!
296+
#! Put Morphism:
297+
#! ------------
298+
#! ℝ^6 -> ℝ^4
299+
#!
300+
#! ‣ x1 + x5 ^ 2
301+
#! ‣ x2 + x6 ^ 2
302+
#! ‣ x3 + 0.01 * x5 / (1.e-07 + Sqrt( (x1 + x5 ^ 2) ))
303+
#! ‣ x4 + 0.01 * x6 / (1.e-07 + Sqrt( (x2 + x6 ^ 2) ))
304+
Display( Lenses.AdamOptimizer(
305+
:learning_rate := 0.01, beta_1 := 0.9, beta_2 := 0.999 )( 2 ) );
306+
#! (ℝ^7, ℝ^7) -> (ℝ^2, ℝ^2) defined by:
307+
#!
308+
#! Get Morphism:
309+
#! ------------
310+
#! ℝ^7 -> ℝ^2
311+
#!
312+
#! ‣ x6
313+
#! ‣ x7
314+
#!
315+
#! Put Morphism:
316+
#! ------------
317+
#! ℝ^9 -> ℝ^7
318+
#!
319+
#! ‣ x1 + 1
320+
#! ‣ 0.9 * x2 + 0.1 * x8
321+
#! ‣ 0.9 * x3 + 0.1 * x9
322+
#! ‣ 0.999 * x4 + 0.001 * x8 ^ 2
323+
#! ‣ 0.999 * x5 + 0.001 * x9 ^ 2
324+
#! ‣ x6 + 0.01 / (1 - 0.999 ^ x1)
325+
#! * ((0.9 * x2 + 0.1 * x8) /
326+
#! (1.e-07 + Sqrt( (0.999 * x4 + 0.001 * x8 ^ 2) / (1 - 0.999 ^ x1) )))
327+
#! ‣ x7 + 0.01 / (1 - 0.999 ^ x1)
328+
#! * ((0.9 * x3 + 0.1 * x9) /
329+
#! (1.e-07 + Sqrt( (0.999 * x5 + 0.001 * x9 ^ 2) / (1 - 0.999 ^ x1) )))
330+
#! @EndExample

0 commit comments

Comments
 (0)