keras 3.*, tensorflow 2.16 and keras 2.* compatibility problem

  • TensorFlow Version: 2.16.1
  • Keras Version: 3.3.3
  • tf_keras Version: 2.16.0

Issue Description:

I encountered an error while running my training script training_holdout.py with the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export TF_USE_LEGACY_KERAS=1
python3 training_holdout.py
</code>
<code>export TF_USE_LEGACY_KERAS=1 python3 training_holdout.py </code>
export TF_USE_LEGACY_KERAS=1 
python3 training_holdout.py

Note: It’s crucial to use export TF_USE_LEGACY_KERAS=1 to ensure that TensorFlow utilizes Keras version 2.* for compatibility reasons.

The script failed and produced the following traceback:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Traceback (most recent call last):
File "/media/fernando/B0EA304AEA300EDA/Dados/Fernando/CODE/PESQUISA/programs-tese/SYSTEM-TRAINING/cnn_emotion4/create/training_holdout.py", line 184, in <module>
model, target_size = mpp.create_model(model_type=model_type, load_weights=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/fernando/B0EA304AEA300EDA/Dados/Fernando/CODE/PESQUISA/programs-tese/SYSTEM-TRAINING/cnn_emotion4/create/../library/BodyEmotion4Lib/lib_model.py", line 58, in create_model
modelo = tf.keras.Sequential([
^^^^^^^^^^^^^^^^^^^^^
File "/home/fernando/.local/lib/python3.12/site-packages/tensorflow/python/trackable/base.py", line 204, in _method_wrapper
result = method(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/fernando/.local/lib/python3.12/site-packages/tf_keras/src/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/lib/python3.12/random.py", line 336, in randint
return self.randrange(a, b+1)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/random.py", line 312, in randrange
istop = _index(stop)
^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer
</code>
<code>Traceback (most recent call last): File "/media/fernando/B0EA304AEA300EDA/Dados/Fernando/CODE/PESQUISA/programs-tese/SYSTEM-TRAINING/cnn_emotion4/create/training_holdout.py", line 184, in <module> model, target_size = mpp.create_model(model_type=model_type, load_weights=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/media/fernando/B0EA304AEA300EDA/Dados/Fernando/CODE/PESQUISA/programs-tese/SYSTEM-TRAINING/cnn_emotion4/create/../library/BodyEmotion4Lib/lib_model.py", line 58, in create_model modelo = tf.keras.Sequential([ ^^^^^^^^^^^^^^^^^^^^^ File "/home/fernando/.local/lib/python3.12/site-packages/tensorflow/python/trackable/base.py", line 204, in _method_wrapper result = method(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/fernando/.local/lib/python3.12/site-packages/tf_keras/src/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/usr/lib/python3.12/random.py", line 336, in randint return self.randrange(a, b+1) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/random.py", line 312, in randrange istop = _index(stop) ^^^^^^^^^^^^ TypeError: 'float' object cannot be interpreted as an integer </code>
Traceback (most recent call last):
  File "/media/fernando/B0EA304AEA300EDA/Dados/Fernando/CODE/PESQUISA/programs-tese/SYSTEM-TRAINING/cnn_emotion4/create/training_holdout.py", line 184, in <module>
    model, target_size = mpp.create_model(model_type=model_type, load_weights=False)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/media/fernando/B0EA304AEA300EDA/Dados/Fernando/CODE/PESQUISA/programs-tese/SYSTEM-TRAINING/cnn_emotion4/create/../library/BodyEmotion4Lib/lib_model.py", line 58, in create_model
    modelo = tf.keras.Sequential([
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/fernando/.local/lib/python3.12/site-packages/tensorflow/python/trackable/base.py", line 204, in _method_wrapper
    result = method(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/fernando/.local/lib/python3.12/site-packages/tf_keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/lib/python3.12/random.py", line 336, in randint
    return self.randrange(a, b+1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/random.py", line 312, in randrange
    istop = _index(stop)
            ^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer

Cause of the Error:

The error originates from the randint function in Python’s random module, which is being passed a float instead of an integer. This causes a TypeError because the randrange function expects integer inputs.

Solution:

To resolve this issue, I modified line 336 in the file /usr/lib/python3.12/random.py from:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return self.randrange(a, b+1)
</code>
<code>return self.randrange(a, b+1) </code>
return self.randrange(a, b+1)

to:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return self.randrange(int(a), int(b+1))
</code>
<code>return self.randrange(int(a), int(b+1)) </code>
return self.randrange(int(a), int(b+1))

This ensures that the arguments passed to randrange are integers, thereby preventing the TypeError.

Recommendation:

It would be beneficial to implement a type check or cast within the relevant parts of the TensorFlow or Keras codebase to handle such cases where float inputs might be passed inadvertently.

I would like to report this issue to the appropriate bug tracking system, but I am uncertain whether it falls under TensorFlow, Keras, or the Python random module. Could you please advise where to submit this bug report to ensure it reaches the correct library maintainers?

New contributor

Fernando Pujaico Rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật