LoginSignup
1
0

【Tensorflow】import tensorflow_hub実施時に、'register_load_context_function’に関するエラーが出力される。

Last updated at Posted at 2024-03-18

開発環境

  • Google Colab

事象

  • TensorFlowに関連したライブラリをpip installしました。
pip! install tensorflow-text
  • 上記操作後、Pythonをコーディングする際、import tensorflow_hubにて下記に示すメッセージが出力されました。

AttributeError: module 'tensorflow._api.v2.compat.v2.internal' has no attribute 'register_load_context_function'

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-5c017171c13e> in <cell line: 1>()
----> 1 import tensorflow_hub as hub

13 frames
/usr/local/lib/python3.10/dist-packages/tf_keras/src/saving/legacy/saved_model/load_context.py in <module>
     66 
     67 
---> 68 tf.__internal__.register_load_context_function(in_load_context)
     69 

AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'

原因

  • TensorFlowのバージョンとtf-kerasのバージョンが一致していないため。
    • 公式フォーラムによると、TensorFlow、tf-kerasを同一のバージョンで統一させる必要があるとのことです。
    • pip! install tensorflow-textによって、tensorflowが2.16.1になったにも関わらず、tf-kerasが2.15.1のままであるために、バージョンに差異があります。

参考リンク

Unable to import tensorflow_hub - Been stuck on this all evening

解決策

  • TensorFlowのバージョンとtf-kerasのバージョンを一致させる。本ケースでは、TensorFlowがバージョン2.16.1であったために、それに合わせてtf-kerasをpip installしました。
# TensorFlowのバージョンを確認する。
!python3 -m pip list | grep tensorflow | grep -v tensorflow-
tensorflow                       2.16.1

# tf_kerasのバージョンを確認する。
!python3 -m pip list | grep tf_keras
tf_keras                         2.15.1


# バージョンが不一致なために、tf-kerasをpip installする。
!pip install tf-keras==2.16.0rc0
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0