From 084259d663e2913efadb75520dde9d2ed8c0c4b0 Mon Sep 17 00:00:00 2001 From: Kamal Saleh Date: Tue, 9 Jul 2024 14:15:51 +0200 Subject: [PATCH] bug fix in SigmoidBinaryCrossEntropyLoss --- PackageInfo.g | 2 +- gap/CategoryOfParametrisedMorphisms.gi | 5 +++-- gap/CategoryOfSkeletalSmoothMaps.gi | 2 +- tst/categorical_vs_direct_constructions.tst | 25 +++++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 tst/categorical_vs_direct_constructions.tst diff --git a/PackageInfo.g b/PackageInfo.g index c05bb33..dc44739 100644 --- a/PackageInfo.g +++ b/PackageInfo.g @@ -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", diff --git a/gap/CategoryOfParametrisedMorphisms.gi b/gap/CategoryOfParametrisedMorphisms.gi index 425aee3..f2f19de 100644 --- a/gap/CategoryOfParametrisedMorphisms.gi +++ b/gap/CategoryOfParametrisedMorphisms.gi @@ -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... ) @@ -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; diff --git a/gap/CategoryOfSkeletalSmoothMaps.gi b/gap/CategoryOfSkeletalSmoothMaps.gi index 9f6499d..8787a9b 100644 --- a/gap/CategoryOfSkeletalSmoothMaps.gi +++ b/gap/CategoryOfSkeletalSmoothMaps.gi @@ -1408,7 +1408,7 @@ InstallOtherMethod( \., return PreCompose( Smooth, DirectProductFunctorial( Smooth, [ Smooth.Sigmoid_( 1 ), Smooth.IdFunc( 1 ) ] ), - Smooth.BinaryCrossEntropyLoss_ ); + Smooth.BinaryCrossEntropyLoss_( n ) ); end; diff --git a/tst/categorical_vs_direct_constructions.tst b/tst/categorical_vs_direct_constructions.tst new file mode 100644 index 0000000..b5e83d3 --- /dev/null +++ b/tst/categorical_vs_direct_constructions.tst @@ -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 ) );