0

I am encountering an error when trying to load a Keras model with custom objects. The error message is:

AttributeError: 'function' object has no attribute 'get_shape'

This occurs in the following context:

custom_objects={'Self_Attention': Self_Attention, 'conditional_BCE': conditional_BCE})

Here is the relevant code snippet where the error occurs:

from keras.models import load_model

# Load the model with custom objects model = 

load_model('path_to_your_model.h5', custom_objects={'Self_Attention': Self_Attention, 'conditional_BCE': conditional_BCE})

Custom Object Code:

One of my custom objects is a custom loss function:

from keras import backend as K 

def conditional_BCE(input_mask, flag):
    def conditional_BCE2(y_true, y_pred): 
        loss = flag * K.binary_crossentropy(y_true, y_pred) * input_mask 
        return K.sum(loss) / K.sum(input_mask)
    return conditional_BCE2

Keras and TensorFlow Versions:

  • Keras: 2.0.8

  • TensorFlow: 1.2.1

  • Python: 2.7

What I Expected vs. What Happened:

Expected:

  • The model should load successfully with the custom objects, and I should be able to use it for inference or further training.

Actual:

  • I receive an AttributeError indicating that a 'function' object has no attribute 'get_shape'.

Steps Taken:

  • I have checked the custom objects code and believe it is correct.

  • I suspect this issue might be related to the TensorFlow or Keras version incompatibility.

Can someone help identify whether this issue is due to version incompatibility, an error in the custom objects, or something else? Any guidance on how to resolve this issue or workarounds would be greatly appreciated.

2
  • 1
    You always need to include the full traceback, not just one line. Commented Sep 17, 2024 at 8:26
  • Try with the latest version of tenserflow and keras and check you are using correctly implement Keras serializes and deserializes function-based loss functions,If you are still facing an issue, share your full code and error message so that I can help you,Thanks. Commented Mar 12 at 6:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.