OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Issue with registering custom activation function in tensorflow

  • Thread starter Thread starter deckard
  • Start date Start date
D

deckard

Guest
I have a code that used to work on Python 3.8. I am trying to port it to 3.11. I can't get the following to work:

Code:
import tensorflow as tf
from tensorflow.keras.layers import Activation, Dense, Input
from tensorflow.keras.models import Sequential

def softplus_shifted(x):
    """
    A shifted softplus function = a + exp(x)
    """
    a = 1.2214027581601699  # np.exp(0.20), reducing operations
    return tf.math.log(tf.constant(a, dtype=tf.float32) +
                       tf.math.exp(x))

# Register the activation function
tf.keras.utils.get_custom_objects().update(
    {'softplus_shifted': Activation(softplus_shifted)})

# Build the model
model = Sequential([
    Input(shape=(4,)),
    Dense(10, activation='softplus_shifted')
])

model.compile(optimizer='adam', loss='mse')
print(model.summary())

I get the following error: enter image description here

As text: C:\Users\rghig\Dropbox\sys_strats_rg.venv\Scripts\python.exe "C:/Program Files/JetBrains/PyCharm Community Edition 2024.1.1/plugins/python-ce/helpers/pydev/pydevconsole.py" --mode=client --host=127.0.0.1 --port=54195 import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['C:\Users\rghig\Dropbox\sys_strats_rg', 'C:\Users\rghig\Dropbox\sys_strats_rg\libs', 'C:\Users\rghig\Dropbox\sys_strats_rg\tests_functional']) PyDev console: starting. Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32 runfile('C:\Users\rghig\AppData\Roaming\JetBrains\PyCharmCE2024.1\scratches\scratch_2.py', wdir='C:\Users\rghig\AppData\Roaming\JetBrains\PyCharmCE2024.1\scratches') Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() ^^^^^^ File "", line 1, in File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:\Users\rghig\AppData\Roaming\JetBrains\PyCharmCE2024.1\scratches\scratch_2.py", line 20, in Dense(10, activation='softplus_shifted') File "C:\Users\rghig\Dropbox\sys_strats_rg.venv\Lib\site-packages\keras\src\layers\core\dense.py", line 89, in init self.activation = activations.get(activation) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\rghig\Dropbox\sys_strats_rg.venv\Lib\site-packages\keras\src\activations_init_.py", line 104, in get raise ValueError( ValueError: Could not interpret activation function identifier: softplus_shifted

I tried with ChatGPT, but no luck either. Any help would be great! Thank you very much!

FWIW my IDE (PyCharm) says that this "from tensorflow.keras.utils import get_custom_objects" is an unresolved reference
<p>I have a code that used to work on Python 3.8. I am trying to port it to 3.11. I can't get the following to work:</p>
<pre><code>import tensorflow as tf
from tensorflow.keras.layers import Activation, Dense, Input
from tensorflow.keras.models import Sequential

def softplus_shifted(x):
"""
A shifted softplus function = a + exp(x)
"""
a = 1.2214027581601699 # np.exp(0.20), reducing operations
return tf.math.log(tf.constant(a, dtype=tf.float32) +
tf.math.exp(x))

# Register the activation function
tf.keras.utils.get_custom_objects().update(
{'softplus_shifted': Activation(softplus_shifted)})

# Build the model
model = Sequential([
Input(shape=(4,)),
Dense(10, activation='softplus_shifted')
])

model.compile(optimizer='adam', loss='mse')
print(model.summary())
</code></pre>
<p>I get the following error:
<a href="https://i.sstatic.net/6HVVzCGB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6HVVzCGB.png" alt="enter image description here" /></a></p>
<p>As text:
C:\Users\rghig\Dropbox\sys_strats_rg.venv\Scripts\python.exe "C:/Program Files/JetBrains/PyCharm Community Edition 2024.1.1/plugins/python-ce/helpers/pydev/pydevconsole.py" --mode=client --host=127.0.0.1 --port=54195
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\Users\rghig\Dropbox\sys_strats_rg', 'C:\Users\rghig\Dropbox\sys_strats_rg\libs', 'C:\Users\rghig\Dropbox\sys_strats_rg\tests_functional'])
PyDev console: starting.
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
runfile('C:\Users\rghig\AppData\Roaming\JetBrains\PyCharmCE2024.1\scratches\scratch_2.py', wdir='C:\Users\rghig\AppData\Roaming\JetBrains\PyCharmCE2024.1\scratches')
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
coro = func()
^^^^^^
File "", line 1, in
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\JetBrains\PyCharm Community Edition 2024.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Users\rghig\AppData\Roaming\JetBrains\PyCharmCE2024.1\scratches\scratch_2.py", line 20, in
Dense(10, activation='softplus_shifted')
File "C:\Users\rghig\Dropbox\sys_strats_rg.venv\Lib\site-packages\keras\src\layers\core\dense.py", line 89, in <strong>init</strong>
self.activation = activations.get(activation)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\rghig\Dropbox\sys_strats_rg.venv\Lib\site-packages\keras\src\activations_<em>init</em>_.py", line 104, in get
raise ValueError(
ValueError: Could not interpret activation function identifier: softplus_shifted</p>
<p>I tried with ChatGPT, but no luck either. Any help would be great! Thank you very much!</p>
<p>FWIW my IDE (PyCharm) says that this "from tensorflow.keras.utils import get_custom_objects" is an unresolved reference</p>
 

Latest posts

J
Replies
0
Views
1
Jeff P Chacko
J
Top