|
| 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