Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float number in randint Python 3.12 #803

Open
dk222dk opened this issue Oct 2, 2024 · 5 comments
Open

float number in randint Python 3.12 #803

dk222dk opened this issue Oct 2, 2024 · 5 comments

Comments

@dk222dk
Copy link

dk222dk commented Oct 2, 2024

I noticed in the source code there are some calls of generator.randint(1, 1e9) 1e9 is converted to float but in python 3.12 calling float in randint causes an error

System information.

  • Python 3.12
  • tf_keras 2.17.0
  • tensorflow 2.17.0

Running with env variable: TF_USE_LEGACY_KERAS=1

Simple steps to reproduce:

import tensorflow as tf
tf.keras.utils.set_random_seed(10)
from tensorflow.keras.initializers import VarianceScaling
vs = VarianceScaling()
vs((3, 3))

Error log

Traceback (most recent call last):
  File "/mnt/g/PycharmProjects/testing/first.py", line 8, in <module>
    vs((3, 3))
  File "/home/user/miniconda3/envs/t2/lib/python3.12/site-packages/tf_keras/src/initializers/initializers.py", line 648, in __call__
    return self._generate_init_val(shape=shape, dtype=dtype, nonce=nonce)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/t2/lib/python3.12/site-packages/tf_keras/src/initializers/initializers.py", line 663, in _generate_init_val
    return self._random_generator.truncated_normal(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/t2/lib/python3.12/site-packages/tf_keras/src/backend.py", line 2134, in truncated_normal
    self._maybe_init()
  File "/home/user/miniconda3/envs/t2/lib/python3.12/site-packages/tf_keras/src/backend.py", line 1976, in _maybe_init
    self._seed = self._create_seed(self._seed)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/t2/lib/python3.12/site-packages/tf_keras/src/backend.py", line 2032, in _create_seed
    return _SEED_GENERATOR.generator.randint(1, 1e9)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/t2/lib/python3.12/random.py", line 336, in randint
    return self.randrange(a, b+1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/t2/lib/python3.12/random.py", line 312, in randrange
    istop = _index(stop)
            ^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer 
@tilakrayal
Copy link
Collaborator

@dk222dk,
I tried to execute the mentioned code on both Keras2.0 & keras3.0 and observed that the code was executed without any issues/fail. Kindly find the gist of it here. Thank you!

@dk222dk
Copy link
Author

dk222dk commented Oct 16, 2024

@tilakrayal You didn't use python 3.12 this error occurs only with python >=3.12 and in previous versions you get warnings that calling randint with float is deprecated and support will be removed in next python versions

Please try in fresh docker environment with python 3.12:

docker pull python:3.12
docker run --rm --entrypoint /bin/bash -it python:3.12

inside docker:

pip install tf_keras==2.17.0 tensorflow==2.17.0
apt update
apt install nano
nano script.py

copy this:

import tensorflow as tf
tf.keras.utils.set_random_seed(10)
from tensorflow.keras.initializers import VarianceScaling
vs = VarianceScaling()
vs((3, 3))


export TF_USE_LEGACY_KERAS=1
python script.py

The solution would be to find all calls of .randint(...) with floats and wrap them with int(), I saw there was one place in source code where it was done but not everywhere

for example: generator.randint(1, 1e9) -> generator.randint(1, int(1e9))

@dk222dk
Copy link
Author

dk222dk commented Oct 23, 2024

@tilakrayal are you able to create a MR with that change and release new version of tf_keras? it should be a simple change currently it's something that blocks users from using tf_keras with python 3.12 since it doesn't work with that version.

Copy link

github-actions bot commented Nov 7, 2024

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Nov 7, 2024
@dk222dk
Copy link
Author

dk222dk commented Nov 7, 2024

I noticed there are other test fails with python 3.12

@github-actions github-actions bot removed the stale label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants