From 67a7fc19c36ef1f0f5b52351fa3c0fbf1e4112f7 Mon Sep 17 00:00:00 2001 From: Lucio Anderlini Date: Fri, 26 Sep 2025 14:40:44 +0200 Subject: [PATCH 1/2] Added elu and celu --- README.md | 12 +++++++----- scikinC/layers/BaseLayerConverter.py | 4 +++- setup.py | 2 +- test/test_keras.py | 2 ++ 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d5b79ad..fe02336 100644 --- a/README.md +++ b/README.md @@ -211,11 +211,13 @@ A few notes: | `BatchNormalization` | Available | Available | | #### Keras Activation functions - | Model | Implementation | Test | Notes | - | ---------------------------- | --------------- | --------- | ----------------------------- | - | `tanh` | Available | Available | | - | `sigmoid` | Available | Available | | - | `relu` | Available | Available | | + | Model | Implementation | Test | Notes | + |-----------| --------------- | --------- |--------------------------------| + | `tanh` | Available | Available | | + | `sigmoid` | Available | Available | | + | `relu` | Available | Available | | + | `elu` | Available | | | + | `celu` | Available | | Only for alpha=1 (celu == elu) | ## Running tests diff --git a/scikinC/layers/BaseLayerConverter.py b/scikinC/layers/BaseLayerConverter.py index df70e77..0857c2e 100644 --- a/scikinC/layers/BaseLayerConverter.py +++ b/scikinC/layers/BaseLayerConverter.py @@ -17,7 +17,9 @@ def activate (self, x): elif activation == 'tanh': return "%(x)s = tanh(%(x)s);" % {'x':x} elif activation == 'relu': - return "%(x)s = %(x)s > 0. ? %(x)s : 0.;" % {'x':x} + return "%(x)s = %(x)s > 0. ? %(x)s : 0.;" % {'x':x} + elif activation == 'celu' or activation == 'elu': + return "%(x)s = %(x)s > 0. ? %(x)s : exp(x) - 1;" % {'x': x} elif activation == 'linear': return "" else: diff --git a/setup.py b/setup.py index 226ee40..6cc100f 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setup( name='scikinC', # Required - version="0.2.8", # Required + version="0.2.9", # Required description='A converter for scikit learn and keras to hardcoded C function', long_description=long_description, long_description_content_type='text/markdown', # Optional (see note above) diff --git a/test/test_keras.py b/test/test_keras.py index 2d6c456..e0a028a 100644 --- a/test/test_keras.py +++ b/test/test_keras.py @@ -42,6 +42,8 @@ def classifier_dense(): tf.keras.layers.Dense(16, activation='tanh'), tf.keras.layers.Dense(16, activation='sigmoid'), tf.keras.layers.Dense(16, activation='relu'), + tf.keras.layers.Dense(16, activation='elu'), + tf.keras.layers.Dense(16, activation='celu'), tf.keras.layers.Dense(16), tf.keras.layers.Dense(1, activation='sigmoid') ]) From cd18c361386f4dd88604415c6e64753b3e4f70bd Mon Sep 17 00:00:00 2001 From: Lucio Anderlini Date: Fri, 26 Sep 2025 14:52:29 +0200 Subject: [PATCH 2/2] Fixed failing tests --- scikinC/layers/BaseLayerConverter.py | 2 +- test/test_keras.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scikinC/layers/BaseLayerConverter.py b/scikinC/layers/BaseLayerConverter.py index 0857c2e..e505149 100644 --- a/scikinC/layers/BaseLayerConverter.py +++ b/scikinC/layers/BaseLayerConverter.py @@ -19,7 +19,7 @@ def activate (self, x): elif activation == 'relu': return "%(x)s = %(x)s > 0. ? %(x)s : 0.;" % {'x':x} elif activation == 'celu' or activation == 'elu': - return "%(x)s = %(x)s > 0. ? %(x)s : exp(x) - 1;" % {'x': x} + return "%(x)s = %(x)s > 0. ? %(x)s : exp(%(x)s) - 1;" % {'x': x} elif activation == 'linear': return "" else: diff --git a/test/test_keras.py b/test/test_keras.py index e0a028a..cc95f72 100644 --- a/test/test_keras.py +++ b/test/test_keras.py @@ -42,8 +42,8 @@ def classifier_dense(): tf.keras.layers.Dense(16, activation='tanh'), tf.keras.layers.Dense(16, activation='sigmoid'), tf.keras.layers.Dense(16, activation='relu'), - tf.keras.layers.Dense(16, activation='elu'), - tf.keras.layers.Dense(16, activation='celu'), + tf.keras.layers.Dense(16, activation='elu' if hasattr(tf.keras.activations, 'elu') else 'linear'), + tf.keras.layers.Dense(16, activation='celu' if hasattr(tf.keras.activations, 'celu') else 'linear'), tf.keras.layers.Dense(16), tf.keras.layers.Dense(1, activation='sigmoid') ])