hELLO EVERYONE i have this ValueError: could not interpret serialized object when a try to load a model with custom objects. This error comes from this class:
@tf.keras.saving.register_keras_serializable(package=“MyLayers”)
class SimilarityMatrix(tf.keras.layers.Layer):
def __init__(self,dims, **kwargs):
self.dims_length , self.dims_width = dims[1] , dims[0]
super(SimilarityMatrix, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self._m = self.add_weight(name='M',
shape=(self.dims_length,self.dims_width),
initializer='uniform',
trainable=True)
super(SimilarityMatrix, self).build(input_shape) # Be sure to call this at the end
def call(self, y):
xf, xs = y
sim1=tf.matmul(xf, self._m)
transposed = tf.reshape(K.transpose(xs),(-1, 100, 1))
sim2=tf.matmul(sim1, transposed)
return sim2
def compute_output_shape(self, input_shape):
return (1)
def get_config(self):
base_config = super().get_config()
config={
'dims_length': self.dims_length,
'dims_width': self.dims_width
}
#print(config)
return {**base_config , **config}
@classmethod
def from_config(cls, config):
#simm_config = config.pop("dims_length", "dims_width")
#simm_config= config.pop("dims_width")
#simm_config = config
#dims = tf.keras.saving.deserialize_keras_object(simm_config)
dims_length = config.pop("dims_length")
dims_width = config.pop("dims_width")
return cls(dims=(dims_length, dims_width), **config)
#return cls(dims,**config)
And the error is raised after running :
model = tf.keras.models.load_model(saveloc ,custom_objects={“SimilarityMatrix”: SimilarityMatrix} )
Prepare your input data for prediction
input_data = df_test.explain.iloc[2] # Make sure it’s in the right format
Perform predictions
predictions = model.predict(input_data)