Skip to content

Commit

Permalink
Merge pull request #2 from kamalsaleh/master
Browse files Browse the repository at this point in the history
Fix bug in `(Sigmoid)BinaryCrossEntropyLoss`
  • Loading branch information
kamalsaleh authored Jul 9, 2024
2 parents a342ff5 + 084259d commit 88f5250
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion PackageInfo.g
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SetPackageInfo( rec(

PackageName := "MachineLearningForCAP",
Subtitle := "Exploring categorical machine learning in CAP",
Version := "2024.07-04",
Version := "2024.07-09",
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)( ),
License := "GPL-2.0-or-later",

Expand Down
5 changes: 3 additions & 2 deletions gap/CategoryOfParametrisedMorphisms.gi
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ InstallOtherMethod( \.,
end;

elif f in [ "Constant", "Zero", "IdFunc", "Sum", "Mean", "Mul", "Power", "PowerBase", "Relu", "Sigmoid_", "Sigmoid", "Softmax_", "Softmax",
"QuadraticLoss_", "QuadraticLoss", "CrossEntropyLoss_", "CrossEntropyLoss", "SoftmaxCrossEntropyLoss_", "SoftmaxCrossEntropyLoss" ] then
"QuadraticLoss_", "QuadraticLoss", "CrossEntropyLoss_", "CrossEntropyLoss", "SoftmaxCrossEntropyLoss_", "SoftmaxCrossEntropyLoss",
"SigmoidBinaryCrossEntropyLoss_", "SigmoidBinaryCrossEntropyLoss" ] then

return
function ( arg... )
Expand All @@ -455,7 +456,7 @@ InstallOtherMethod( \.,

end;

elif f in [ "Sqrt", "Exp", "Log", "Sin", "Cos", "SigmoidBinaryCrossEntropyLoss_", "SigmoidBinaryCrossEntropyLoss" ] then
elif f in [ "Sqrt", "Exp", "Log", "Sin", "Cos" ] then

return C.( f ) / Para;

Expand Down
2 changes: 1 addition & 1 deletion gap/CategoryOfSkeletalSmoothMaps.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ InstallOtherMethod( \.,

return PreCompose( Smooth,
DirectProductFunctorial( Smooth, [ Smooth.Sigmoid_( 1 ), Smooth.IdFunc( 1 ) ] ),
Smooth.BinaryCrossEntropyLoss_ );
Smooth.BinaryCrossEntropyLoss_( n ) );

end;

Expand Down
25 changes: 25 additions & 0 deletions tst/categorical_vs_direct_constructions.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
gap> Smooth := SkeletalSmoothMaps;;
gap> Lenses := CategoryOfLenses( Smooth );;
gap> Para := CategoryOfParametrisedMorphisms( Smooth );;
gap> Assert( 0, Para.Softmax_( 3 ) = Para.Softmax( 3 ) );
gap> Assert( 0, Para.Sigmoid_( 3 ) = Para.Sigmoid( 3 ) );
gap> Assert( 0, Para.QuadraticLoss_( 3 ) = Para.QuadraticLoss( 3 ) );
gap> Assert( 0, Para.CrossEntropyLoss_( 3 ) = Para.CrossEntropyLoss( 3 ) );
gap> Assert( 0, Para.SoftmaxCrossEntropyLoss_( 3 ) = Para.SoftmaxCrossEntropyLoss( 3 ) );
gap> Assert( 0, Para.QuadraticLoss_( 3 ) = Para.QuadraticLoss( 3 ) );
gap> Assert( 0, Para.QuadraticLoss_( 3 ) = Para.QuadraticLoss( 3 ) );
gap> Assert( 0, Para.SigmoidBinaryCrossEntropyLoss_( 1 ) = Para.SigmoidBinaryCrossEntropyLoss( 1 ) );
gap> Assert( 0, Para.AffineTransformation_( 3, 4 ) = Para.AffineTransformation( 3, 4 ) );
gap> Eval( Smooth.PolynomialTransformation( 2, 3, 4 ), [ 1 .. 47 ] );
[ 122341573, 479204128, 836066683 ]
gap> EvalJacobianMatrix( Smooth.PolynomialTransformation( 2, 3, 4 ), [ 1 .. 47 ] );
[ [ 4477456, 4574792, 97336, 4674244, 99452, 2116, 4775858, 101614, 2162, 46, 4879681, 103823, 2209, 47, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2585050, 7818740 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4477456, 4574792, 97336, 4674244, 99452, 2116, 4775858, 101614, 2162, 46,
4879681, 103823, 2209, 47, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17701630, 23262470 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4477456, 4574792, 97336,
4674244, 99452, 2116, 4775858, 101614, 2162, 46, 4879681, 103823, 2209, 47, 1, 32818210, 38706200 ] ]
gap> Assert( 0, Lenses.GradientDescentOptimizer_( )( 3 ) = Lenses.GradientDescentOptimizer( )( 3 ) );
gap> Assert( 0, Lenses.GradientDescentWithMomentumOptimizer_( )( 3 ) = Lenses.GradientDescentWithMomentumOptimizer( )( 3 ) );
gap> Assert( 0, Lenses.AdagradOptimizer_( )( 3 ) = Lenses.AdagradOptimizer( )( 3 ) );
gap> Assert( 0, Lenses.AdamOptimizer_( )( 3 ) = Lenses.AdamOptimizer( )( 3 ) );

0 comments on commit 88f5250

Please sign in to comment.