Skip to content

Commit 631dfec

Browse files
authored
Merge pull request #7 from kamalsaleh/master
change names of some attributes and include two examples in the readme file
2 parents c7c363c + fdc7b73 commit 631dfec

14 files changed

+832
-61
lines changed

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-17",
13+
Version := "2024.07-20",
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

README.md

+499
Large diffs are not rendered by default.

examples/CategoryOfParametrisedMorphisms.g

+39-39
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ f := Smooth.Softmax( 3 );
2626
f := MorphismConstructor( Para, R1 / Para, [ R2, f ], R3 / Para );
2727
#! ℝ^1 -> ℝ^3 defined by:
2828
#!
29-
#! Parameter Object:
29+
#! Underlying Object:
3030
#! -----------------
3131
#! ℝ^2
3232
#!
33-
#! Parametrised Morphism:
34-
#! ----------------------
33+
#! Underlying Morphism:
34+
#! -------------------
3535
#! ℝ^3 -> ℝ^3
3636
Display( f );
3737
#! ℝ^1 -> ℝ^3 defined by:
3838
#!
39-
#! Parameter Object:
39+
#! Underlying Object:
4040
#! -----------------
4141
#! ℝ^2
4242
#!
43-
#! Parametrised Morphism:
44-
#! ----------------------
43+
#! Underlying Morphism:
44+
#! -------------------
4545
#! ℝ^3 -> ℝ^3
4646
#!
4747
#! ‣ Exp( x1 ) / (Exp( x1 ) + Exp( x2 ) + Exp( x3 ))
@@ -59,22 +59,22 @@ Display( r );
5959
g := ReparametriseMorphism( f, r );
6060
#! ℝ^1 -> ℝ^3 defined by:
6161
#!
62-
#! Parameter Object:
62+
#! Underlying Object:
6363
#! -----------------
6464
#! ℝ^2
6565
#!
66-
#! Parametrised Morphism:
67-
#! ----------------------
66+
#! Underlying Morphism:
67+
#! -------------------
6868
#! ℝ^3 -> ℝ^3
6969
Display( g );
7070
#! ℝ^1 -> ℝ^3 defined by:
7171
#!
72-
#! Parameter Object:
72+
#! Underlying Object:
7373
#! -----------------
7474
#! ℝ^2
7575
#!
76-
#! Parametrised Morphism:
77-
#! ----------------------
76+
#! Underlying Morphism:
77+
#! -------------------
7878
#! ℝ^3 -> ℝ^3
7979
#!
8080
#! ‣ Exp( Sqrt( x1 ) ) / (Exp( Sqrt( x1 ) ) + Exp( Cos( x2 ) ) + Exp( x3 ))
@@ -83,32 +83,32 @@ Display( g );
8383
l := Para.AffineTransformation( 3, 2 );
8484
#! ℝ^3 -> ℝ^2 defined by:
8585
#!
86-
#! Parameter Object:
86+
#! Underlying Object:
8787
#! -----------------
8888
#! ℝ^8
8989
#!
90-
#! Parametrised Morphism:
91-
#! ----------------------
90+
#! Underlying Morphism:
91+
#! -------------------
9292
#! ℝ^11 -> ℝ^2
9393
h := PreCompose( g, l );
9494
#! ℝ^1 -> ℝ^2 defined by:
9595
#!
96-
#! Parameter Object:
96+
#! Underlying Object:
9797
#! -----------------
9898
#! ℝ^10
9999
#!
100-
#! Parametrised Morphism:
101-
#! ----------------------
100+
#! Underlying Morphism:
101+
#! -------------------
102102
#! ℝ^11 -> ℝ^2
103103
Display( h );
104104
#! ℝ^1 -> ℝ^2 defined by:
105105
#!
106-
#! Parameter Object:
106+
#! Underlying Object:
107107
#! -----------------
108108
#! ℝ^10
109109
#!
110-
#! Parametrised Morphism:
111-
#! ----------------------
110+
#! Underlying Morphism:
111+
#! -------------------
112112
#! ℝ^11 -> ℝ^2
113113
#!
114114
#! ‣ x1 * (Exp( Sqrt( x9 ) ) / (Exp( Sqrt( x9 ) ) + Exp( Cos( x10 ) ) + Exp( x11 )))
@@ -123,22 +123,22 @@ r := Smooth.Constant( constants );
123123
t := ReparametriseMorphism( h, r );
124124
#! ℝ^1 -> ℝ^2 defined by:
125125
#!
126-
#! Parameter Object:
126+
#! Underlying Object:
127127
#! -----------------
128128
#! ℝ^0
129129
#!
130-
#! Parametrised Morphism:
131-
#! ----------------------
130+
#! Underlying Morphism:
131+
#! -------------------
132132
#! ℝ^1 -> ℝ^2
133133
Display( t );
134134
#! ℝ^1 -> ℝ^2 defined by:
135135
#!
136-
#! Parameter Object:
136+
#! Underlying Object:
137137
#! -----------------
138138
#! ℝ^0
139139
#!
140-
#! Parametrised Morphism:
141-
#! ----------------------
140+
#! Underlying Morphism:
141+
#! -------------------
142142
#! ℝ^1 -> ℝ^2
143143
#!
144144
#! ‣ 0.91 * (2.39116 / (5.10727 + Exp( x1 ))) + 0.24 * (2.71611 / (5.10727 + Exp( x1 )))
@@ -148,22 +148,22 @@ Display( t );
148148
s := SimplifyMorphism( t, infinity );
149149
#! ℝ^1 -> ℝ^2 defined by:
150150
#!
151-
#! Parameter Object:
151+
#! Underlying Object:
152152
#! -----------------
153153
#! ℝ^0
154154
#!
155-
#! Parametrised Morphism:
156-
#! ----------------------
155+
#! Underlying Morphism:
156+
#! -------------------
157157
#! ℝ^1 -> ℝ^2
158158
Display( s );
159159
#! ℝ^1 -> ℝ^2 defined by:
160160
#!
161-
#! Parameter Object:
161+
#! Underlying Object:
162162
#! -----------------
163163
#! ℝ^0
164164
#!
165-
#! Parametrised Morphism:
166-
#! ----------------------
165+
#! Underlying Morphism:
166+
#! -------------------
167167
#! ℝ^1 -> ℝ^2
168168
#!
169169
#! ‣ (1.47 * Exp( x1 ) + 5.84111) / (Exp( x1 ) + 5.10727)
@@ -175,22 +175,22 @@ ApplyFunctor( iota, Smooth.( 1 ) );
175175
psi := ApplyFunctor( iota, Smooth.Sum( 2 ) );
176176
#! ℝ^2 -> ℝ^1 defined by:
177177
#!
178-
#! Parameter Object:
178+
#! Underlying Object:
179179
#! -----------------
180180
#! ℝ^0
181181
#!
182-
#! Parametrised Morphism:
183-
#! ----------------------
182+
#! Underlying Morphism:
183+
#! -------------------
184184
#! ℝ^2 -> ℝ^1
185185
Print( DisplayString( psi ) );
186186
#! ℝ^2 -> ℝ^1 defined by:
187187
#!
188-
#! Parameter Object:
188+
#! Underlying Object:
189189
#! -----------------
190190
#! ℝ^0
191191
#!
192-
#! Parametrised Morphism:
193-
#! ----------------------
192+
#! Underlying Morphism:
193+
#! -------------------
194194
#! ℝ^2 -> ℝ^1
195195
#!
196196
#! ‣ x1 + x2

gap/CategoryOfParametrisedMorphisms.gi

+6-6
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ InstallMethod( ViewString,
546546
" -> ",
547547
ViewString( Target( f ) ),
548548
" defined by:",
549-
"\n\nParameter Object:\n-----------------\n",
549+
"\n\nUnderlying Object:\n-----------------\n",
550550
ViewString( UnderlyingObject( f ) ),
551-
"\n\nParametrised Morphism:\n----------------------\n",
551+
"\n\nUnderlying Morphism:\n-------------------\n",
552552
ViewString( UnderlyingMorphism( f ) ) );
553553

554554
end );
@@ -565,9 +565,9 @@ InstallMethod( DisplayString,
565565
" -> ",
566566
ViewString( Target( f ) ),
567567
" defined by:",
568-
"\n\nParameter Object:\n-----------------\n",
568+
"\n\nUnderlying Object:\n-----------------\n",
569569
ViewString( UnderlyingObject( f ) ),
570-
"\n\nParametrised Morphism:\n----------------------\n",
570+
"\n\nUnderlying Morphism:\n-------------------\n",
571571
DisplayString( UnderlyingMorphism( f ) ) );
572572

573573
end );
@@ -584,9 +584,9 @@ InstallMethod( Display,
584584
" -> ",
585585
ViewString( Target( f ) ),
586586
" defined by:",
587-
"\n\nParameter Object:\n-----------------\n",
587+
"\n\nUnderlying Object:\n-----------------\n",
588588
ViewString( UnderlyingObject( f ) ),
589-
"\n\nParametrised Morphism:\n----------------------\n" ) );
589+
"\n\nUnderlying Morphism:\n-------------------\n" ) );
590590

591591
Display( UnderlyingMorphism( f ) );
592592

pictures/linear_regression.gif

1.34 MB
Loading

pictures/logistic_regression.gif

596 KB
Loading

pictures/network-1.png

6.06 KB
Loading

pictures/network-2.png

53.2 KB
Loading

pictures/training_data_1.png

214 KB
Loading

pictures/training_data_2.png

270 KB
Loading

tst/categorical_vs_direct_constructions.tst

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ gap> Assert( 0, Para.AffineTransformation_( 3, 4 ) = Para.AffineTransformation(
1212
gap> l := Para.AffineTransformation( 3, 4 );
1313
^3 ->^4 defined by:
1414

15-
Parameter Object:
15+
Underlying Object:
1616
-----------------
1717
^16
1818

19-
Parametrised Morphism:
20-
----------------------
19+
Underlying Morphism:
20+
-------------------
2121
^19 ->^4
2222
gap> dummy_input := DummyInputForAffineTransformation( 3, 4, "w", "b", "x" );;
2323
gap> Display( l : dummy_input := dummy_input );
2424
^3 ->^4 defined by:
2525

26-
Parameter Object:
26+
Underlying Object:
2727
-----------------
2828
^16
2929

30-
Parametrised Morphism:
31-
----------------------
30+
Underlying Morphism:
31+
-------------------
3232
^19 ->^4
3333

3434
‣ w1_1 * x1 + w2_1 * x2 + w3_1 * x3 + b_1

tst/example-in-readme-1.tst

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
gap> LoadPackage( "MachineLearningForCAP" );
2+
true
3+
4+
gap> Para := CategoryOfParametrisedMorphisms( SkeletalSmoothMaps );
5+
CategoryOfParametrisedMorphisms( SkeletalSmoothMaps )
6+
7+
gap> training_set := [ [ 1, 2.9 ], [ 2, 5.1 ], [ 3, 7.05 ] ];
8+
[ [ 1, 2.9 ], [ 2, 5.1 ], [ 3, 7.05 ] ]
9+
10+
gap> input_dim := 1;; output_dim := 1;; hidden_dims := [ ];;
11+
12+
gap> f := PredictionMorphismOfNeuralNetwork( Para, input_dim, hidden_dims, output_dim, "IdFunc" );;
13+
14+
gap> Display( f );
15+
^1 ->^1 defined by:
16+
17+
Underlying Object:
18+
-----------------
19+
^2
20+
21+
Underlying Morphism:
22+
-------------------
23+
^3 ->^1
24+
25+
‣ x1 * x3 + x2
26+
27+
gap> parameters := [ 2, 1 ];; x := [ 2 ];;
28+
29+
gap> Eval( f, [ parameters, x ] );
30+
[ 5 ]
31+
32+
gap> ell := LossMorphismOfNeuralNetwork( Para, input_dim, hidden_dims, output_dim, "IdFunc" );;
33+
34+
gap> Display( ell );
35+
^2 ->^1 defined by:
36+
37+
Underlying Object:
38+
-----------------
39+
^2
40+
41+
Underlying Morphism:
42+
-------------------
43+
^4 ->^1
44+
45+
‣ (x1 * x3 + x2 - x4) ^ 2 / 1
46+
47+
gap> Lenses := CategoryOfLenses( SkeletalSmoothMaps );
48+
CategoryOfLenses( SkeletalSmoothMaps )
49+
50+
gap> optimizer := Lenses.GradientDescentOptimizer( : learning_rate := 0.01 );;
51+
52+
gap> batch_size := 1;;
53+
54+
gap> one_epoch_update := OneEpochUpdateLens( ell, optimizer, training_set, batch_size );;
55+
56+
gap> parameters := [ 0.1, -0.1 ];;
57+
58+
gap> nr_epochs := 15;;
59+
60+
gap> parameters := Fit( one_epoch_update, nr_epochs, parameters );
61+
Epoch 0/15 - loss = 26.777499999999993
62+
Epoch 1/15 - loss = 13.002145872163197
63+
Epoch 2/15 - loss = 6.3171942035316935
64+
Epoch 3/15 - loss = 3.0722513061917534
65+
Epoch 4/15 - loss = 1.4965356389126505
66+
Epoch 5/15 - loss = 0.73097379078374358
67+
Epoch 6/15 - loss = 0.35874171019291579
68+
Epoch 7/15 - loss = 0.1775574969062125
69+
Epoch 8/15 - loss = 0.089228700384937534
70+
Epoch 9/15 - loss = 0.046072054531129378
71+
Epoch 10/15 - loss = 0.024919378473509772
72+
Epoch 11/15 - loss = 0.014504998499450883
73+
Epoch 12/15 - loss = 0.0093448161379050161
74+
Epoch 13/15 - loss = 0.0067649700132868147
75+
Epoch 14/15 - loss = 0.0054588596501628835
76+
Epoch 15/15 - loss = 0.0047859930295160499
77+
[ 2.08995, 0.802632 ]
78+
79+
gap> theta := SkeletalSmoothMaps.Constant( parameters );
80+
^0 ->^2
81+
82+
gap> Display( theta );
83+
^0 ->^2
84+
85+
2.08995
86+
0.802632
87+
88+
gap> f_theta := ReparametriseMorphism( f, theta );;
89+
90+
gap> Display( f_theta );
91+
^1 ->^1 defined by:
92+
93+
Underlying Object:
94+
-----------------
95+
^0
96+
97+
Underlying Morphism:
98+
-------------------
99+
^1 ->^1
100+
101+
2.08995 * x1 + 0.802632
102+
103+
gap> f_theta := UnderlyingMorphism( f_theta );
104+
^1 ->^1
105+
106+
gap> Eval( f_theta, [ 1 ] );
107+
[ 2.89259 ]
108+
109+
gap> Eval( f_theta, [ 2 ] );
110+
[ 4.98254 ]
111+
112+
gap> Eval( f_theta, [ 3 ] );
113+
[ 7.07249 ]

0 commit comments

Comments
 (0)