Skip to content

Commit fdc7b73

Browse files
committed
update README.md
1 parent 9818b72 commit fdc7b73

File tree

9 files changed

+771
-0
lines changed

9 files changed

+771
-0
lines changed

README.md

Lines changed: 499 additions & 0 deletions
Large diffs are not rendered by default.

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/example-in-readme-1.tst

Lines changed: 113 additions & 0 deletions
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 ]

tst/example-in-readme-2.tst

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
gap> LoadPackage( "MachineLearningForCAP" );
2+
true
3+
4+
gap> Para := CategoryOfParametrisedMorphisms( SkeletalSmoothMaps );
5+
CategoryOfParametrisedMorphisms( SkeletalSmoothMaps )
6+
7+
gap> class_1 := Concatenation( List( [ -2 .. 3 ], i -> [ [ i, i - 1, 1, 0, 0 ], [ i + 1, i - 1, 1, 0, 0 ] ] ) );;
8+
gap> class_2 := Concatenation( List( [ -3 .. -1 ], i -> List( [ i + 1 .. - i - 1 ], j -> [ i, j, 0, 1, 0 ] ) ) );;
9+
gap> class_3 := Concatenation( List( [ 1 .. 3 ], i -> List( [ - i + 1 .. i - 1 ], j -> [ j, i, 0, 0, 1 ] ) ) );;
10+
11+
gap> training_set := Concatenation( class_1, class_2, class_3 );
12+
[ [ -2, -3, 1, 0, 0 ], [ -1, -3, 1, 0, 0 ], [ -1, -2, 1, 0, 0 ], [ 0, -2, 1, 0, 0 ], [ 0, -1, 1, 0, 0 ],
13+
[ 1, -1, 1, 0, 0 ], [ 1, 0, 1, 0, 0 ], [ 2, 0, 1, 0, 0 ], [ 2, 1, 1, 0, 0 ], [ 3, 1, 1, 0, 0 ],
14+
[ 3, 2, 1, 0, 0 ], [ 4, 2, 1, 0, 0 ], [ -3, -2, 0, 1, 0 ], [ -3, -1, 0, 1, 0 ], [ -3, 0, 0, 1, 0 ],
15+
[ -3, 1, 0, 1, 0 ], [ -3, 2, 0, 1, 0 ], [ -2, -1, 0, 1, 0 ], [ -2, 0, 0, 1, 0 ], [ -2, 1, 0, 1, 0 ],
16+
[ -1, 0, 0, 1, 0 ], [ 0, 1, 0, 0, 1 ], [ -1, 2, 0, 0, 1 ], [ 0, 2, 0, 0, 1 ], [ 1, 2, 0, 0, 1 ],
17+
[ -2, 3, 0, 0, 1 ], [ -1, 3, 0, 0, 1 ], [ 0, 3, 0, 0, 1 ], [ 1, 3, 0, 0, 1 ], [ 2, 3, 0, 0, 1 ] ]
18+
19+
gap> input_dim := 2;; output_dim := 3;; hidden_dims := [ ];;
20+
21+
gap> f := PredictionMorphismOfNeuralNetwork( Para, input_dim, hidden_dims, output_dim, "Softmax" );;
22+
23+
gap> input := ConvertToExpressions( [ "theta_1", "theta_2", "theta_3", "theta_4", "theta_5", "theta_6", "theta_7", "theta_8", "theta_9", "x1", "x2" ] );;
24+
25+
gap> Display( f : dummy_input := input );
26+
^2 ->^3 defined by:
27+
28+
Underlying Object:
29+
-----------------
30+
^9
31+
32+
Underlying Morphism:
33+
-------------------
34+
^11 ->^3
35+
36+
‣ Exp( (theta_1 * x1 + theta_2 * x2 + theta_3) )
37+
/ (Exp( theta_1 * x1 + theta_2 * x2 + theta_3 ) + Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) ) + Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) ))
38+
‣ Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) )
39+
/ (Exp( theta_1 * x1 + theta_2 * x2 + theta_3 ) + Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) ) + Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) ))
40+
‣ Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) )
41+
/ (Exp( theta_1 * x1 + theta_2 * x2 + theta_3 ) + Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) ) + Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) ))
42+
43+
gap> parameters := [ 0.1, -0.1, 0, 0.1, 0.2, 0, -0.2, 0.3, 0 ];;
44+
gap> x := [ 1, 2 ];;
45+
46+
gap> prediction_x := Eval( f, [ parameters, x ] );
47+
[ 0.223672, 0.407556, 0.368772 ]
48+
49+
gap> PositionMaximum( prediction_x );
50+
2
51+
52+
gap> ell := LossMorphismOfNeuralNetwork( Para, input_dim, hidden_dims, output_dim, "Softmax" );;
53+
54+
gap> input := ConvertToExpressions( [ "theta_1", "theta_2", "theta_3", "theta_4", "theta_5", "theta_6", "theta_7", "theta_8", "theta_9", "x1", "x2", "y1", "y2", "y3" ] );;
55+
56+
gap> Display( ell : dummy_input := input );
57+
^5 ->^1 defined by:
58+
59+
Underlying Object:
60+
-----------------
61+
^9
62+
63+
Underlying Morphism:
64+
-------------------
65+
^14 ->^1
66+
67+
‣ ((Log( Exp( theta_1 * x1 + theta_2 * x2 + theta_3 ) + Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) ) + Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) ) ) - (theta_1 * x1 + theta_2 * x2 + theta_3)) * y1
68+
+ (Log( Exp( theta_1 * x1 + theta_2 * x2 + theta_3 ) + Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) ) + Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) ) ) - (theta_4 * x1 + theta_5 * x2 + theta_6)) * y2
69+
+ (Log( Exp( theta_1 * x1 + theta_2 * x2 + theta_3 ) + Exp( (theta_4 * x1 + theta_5 * x2 + theta_6) ) + Exp( (theta_7 * x1 + theta_8 * x2 + theta_9) ) ) - (theta_7 * x1 + theta_8 * x2 + theta_9)) * y3) / 3
70+
71+
gap> Lenses := CategoryOfLenses( SkeletalSmoothMaps );
72+
CategoryOfLenses( SkeletalSmoothMaps )
73+
74+
gap> optimizer := Lenses.AdamOptimizer( : learning_rate := 0.01, beta_1 := 0.9, beta_2 := 0.999 );;
75+
76+
gap> batch_size := 1;;
77+
78+
gap> one_epoch_update := OneEpochUpdateLens( ell, optimizer, training_set, batch_size );
79+
(ℝ^28, ℝ^28) -> (ℝ^1, ℝ^0) defined by:
80+
81+
Get Morphism:
82+
----------
83+
^28 ->^1
84+
85+
Put Morphism:
86+
----------
87+
^28 ->^28
88+
89+
gap> parameters := [ 0.1, -0.1, 0, 0.1, 0.2, 0, -0.2, 0.3, 0 ];;
90+
91+
gap> w := Concatenation( [ 1 ], 0 * parameters, 0 * parameters, parameters );;
92+
93+
gap> Eval( GetMorphism( one_epoch_update ), w );
94+
[ 0.345836 ]
95+
96+
gap> new_w := Eval( PutMorphism( one_epoch_update ), w );
97+
[ 31, 0.104642, -0.355463, -0.197135, -0.109428, -0.147082, 0.00992963,
98+
0.00478679, 0.502546, 0.187206, 0.0105493, 0.00642903, 0.00211548,
99+
0.00660062, 0.00274907, 0.00110985, 0.00278786, 0.0065483, 0.00112838,
100+
5.45195, -1.26208, 3.82578, -5.40639, -0.952146, -3.42835, -2.79496, 3.09008, -6.80672 ]
101+
102+
gap> nr_epochs := 4;;
103+
104+
gap> w := Fit( one_epoch_update, nr_epochs, w );
105+
Epoch 0/4 - loss = 0.34583622811001763
106+
Epoch 1/4 - loss = 0.6449437167091393
107+
Epoch 2/4 - loss = 0.023811108587716449
108+
Epoch 3/4 - loss = 0.0036371652708073405
109+
Epoch 4/4 - loss = 0.0030655216725219204
110+
[ 121, -4.57215e-05, -0.00190417, -0.0014116, -0.00181528, 0.00108949, 0.00065518, 0.001861, 0.000814679,
111+
0.000756424, 0.0104885, 0.00846858, 0.0022682, 0.00784643, 0.00551702, 0.0014626, 0.00351408, 0.00640225,
112+
0.00115053, 5.09137, -4.83379, 3.06257, -5.70976, 0.837175, -4.23622, -1.71171, 5.54301, -4.80856 ]
113+
114+
gap> theta := SplitDenseList( w, [ 19, 9 ] )[2];
115+
[ 5.09137, -4.83379, 3.06257, -5.70976, 0.837175, -4.23622, -1.71171, 5.54301, -4.80856 ]
116+
117+
gap> theta := SkeletalSmoothMaps.Constant( theta );
118+
^0 ->^9
119+
120+
gap> f_theta := ReparametriseMorphism( f, theta );;
121+
122+
gap> Display( f_theta );
123+
^2 ->^3 defined by:
124+
125+
Underlying Object:
126+
-----------------
127+
^0
128+
129+
Underlying Morphism:
130+
-------------------
131+
^2 ->^3
132+
133+
‣ Exp( (5.09137 * x1 + (- 4.83379) * x2 + 3.06257) )
134+
/ (Exp( 5.09137 * x1 + (- 4.83379) * x2 + 3.06257 )
135+
+ Exp( ((- 5.70976) * x1 + 0.837175 * x2 + (- 4.23622)) )
136+
+ Exp( ((- 1.71171) * x1 + 5.54301 * x2 + (- 4.80856)) ))
137+
‣ Exp( ((- 5.70976) * x1 + 0.837175 * x2 + (- 4.23622)) )
138+
/ (Exp( 5.09137 * x1 + (- 4.83379) * x2 + 3.06257 )
139+
+ Exp( ((- 5.70976) * x1 + 0.837175 * x2 + (- 4.23622)) )
140+
+ Exp( ((- 1.71171) * x1 + 5.54301 * x2 + (- 4.80856)) ))
141+
‣ Exp( ((- 1.71171) * x1 + 5.54301 * x2 + (- 4.80856)) )
142+
/ (Exp( 5.09137 * x1 + (- 4.83379) * x2 + 3.06257 )
143+
+ Exp( ((- 5.70976) * x1 + 0.837175 * x2 + (- 4.23622)) )
144+
+ Exp( ((- 1.71171) * x1 + 5.54301 * x2 + (- 4.80856)) ))
145+
146+
gap> f_theta := UnderlyingMorphism( f_theta );
147+
^2 ->^3
148+
149+
gap> predictions_vec := Eval( f_theta, [ 1, -1 ] );
150+
[ 1., 4.74723e-11, 1.31974e-11 ]
151+
152+
gap> PositionMaximum( predictions_vec );
153+
1
154+
155+
gap> predictions_vec := Eval( f_theta, [ 1, 3 ] );
156+
[ 7.13122e-08, 2.40484e-08, 1. ]
157+
158+
gap> PositionMaximum( predictions_vec );
159+
3

0 commit comments

Comments
 (0)