Error during tensorflow package import -tensorflow._api.v2.compat.v2.__internal__

Hy, everyone!

I’m trying to use tensorflow and tensorflow_hub.
But during the tensorflow package import, I’m already getting the following error:

AttributeError: module ‘tensorflow._api.v2.compat.v2.internal’ has no attribute ‘register_load_context_function’. Did you mean: ‘register_call_context_function’?

I’m using the current version of python:
Python 3.12.2

And I’ve also reinstalled tensorflow packed some times, and followed other instructions I’ve found over discussions but not seems to helping.

Does anyone can help me with any tips, please?

Thanks!

Hi Marcus, welcome to the TF Forum

do you have a code snippet that can reproduce the issue?

also, TFHub now moved to Kaggle Models. I think it would be better to use that route instead.

Hey
Even I am facing the same problem. I am using the latest python version and I am trying to load one of my trained h5 model file which I have uploaded

from flask import Flask, request, render_template
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
app = Flask(__name__)

# Load the pre-trained BERT model
model = tf.keras.models.load_model(
       ("modelnew.h5"),
       custom_objects={'KerasLayer':hub.KerasLayer}
)

can you remove the tensorflow_hub dependency and show the error please?
from your code, you are not using it anywhere

It is being used when I am accessing KerasLayer in custom objects in the second last line

Now I see it! thanks for pointing out!

can you make sure that TF and the tf-keras package both have the same version?
I’ve seen this error today and that was the main issue (specifically if you are using tf 2.16)

2 Likes

You were precise.
Now the error has passed from this line of code.
Thank you very much!!!

1 Like