Добавьте следующую функцию в файл AttentionWithContext.py:
def create_custom_objects():
instance_holder = {"instance": None}
class ClassWrapper(AttentionWithContext):
def __init__(self, *args, **kwargs):
instance_holder["instance"] = self
super(ClassWrapper, self).__init__(*args, **kwargs)
def loss(*args):
method = getattr(instance_holder["instance"], "loss_function")
return method(*args)
def accuracy(*args):
method = getattr(instance_holder["instance"], "accuracy")
return method(*args)
return {"ClassWrapper": ClassWrapper ,"AttentionWithContext": ClassWrapper, "loss": loss,
"accuracy":accuracy}
При загрузке модели:
from AttentionWithContext import create_custom_objects
model = keras.models.load_model(model_path, custom_objects=create_custom_objects())
model.evaluate(X_test, y_test) # or model.predict