I’m encountering an issue when trying to deserialize a custom Transformer model in TensorFlow.And therefore I cannot save and load my model like this : model_directory =“saved_model.keras” transformer_model.save(model_directory) load_transformer_model = load_model(“saved_model.keras”, custom_objects=custom_objects) The error message I’m receiving is:
TypeError: <class ‘main.Transformer’> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config()
are explicitly deserialized in the model’s from_config()
method.
config={‘module’: None, ‘class_name’: ‘Transformer’, ‘config’: {‘trainable’: True, ‘dtype’: ‘float32’}, ‘registered_name’: ‘Custom>Transformer’, ‘build_config’: {‘input_shape’: [1, 10]}}.
Exception encountered: Unable to revive model from config. When overriding the get_config()
method, make sure that the returned config contains all items used as arguments in the constructor to <class ‘main.Transformer’>, which is the default behavior. You can override this default behavior by defining a from_config(cls, config)
class method to specify how to create an instance of Transformer from its config.
Received config={‘trainable’: True, ‘dtype’: ‘float32’}
Error encountered during deserialization: Transformer.init() got an unexpected keyword argument ‘trainable’
I tried to do this: Here’s a code snippet (last block of code) my Transformer Class (I have the following classes: Transformer, Encoder(tf.keras.layers.Layer), Decoder(tf.keras.layers.Layer), EncoderLayer(tf.keras.layers.Layer), DecoderLayer(tf.keras.layers.Layer)):
What I’ve Tried:
Implement/override a get_config(), from_config(), build_from_config() method for my Transformer class, then for each of my classes.
I’ve attempted to use the custom_objects when loading.
Here’s an example of one the get_config() method I tried to implement for my Transformer class:
def get_config(self):
# config = {
# 'num_layers': self.num_layers,
# 'd_model': self.d_model,
# 'num_heads': self.num_heads,
# 'd_feedforward': self.d_feedforward,
# 'input_vocab_size': self.input_vocab_size,
# 'target_vocab_size': self.target_vocab_size,
# 'max_num_positions_in_pe_encoder': self.max_num_positions_in_pe_encoder,
# 'max_num_positions_in_pe_decoder': self.max_num_positions_in_pe_decoder,
# 'dropout_rate': self.dropout_rate,
# }
# return config
def get_config(self):
# config = {
# 'num_layers': self.num_layers,
# 'd_model': self.d_model,
# 'num_heads': self.num_heads,
# 'd_feedforward': self.d_feedforward,
# 'input_vocab_size': self.input_vocab_size,
# 'target_vocab_size': self.target_vocab_size,
# 'max_num_positions_in_pe_encoder': self.max_num_positions_in_pe_encoder,
# 'max_num_positions_in_pe_decoder': self.max_num_positions_in_pe_decoder,
# 'dropout_rate': self.dropout_rate,
# }
# return config
class Transformer(tf.keras.Model):
“”"
The Transformer model architecture, consisting of an Encoder and Decoder.
“”"
def __init__(
self,
num_layers,
d_model,
num_heads,
d_feedforward,
input_vocab_size,
target_vocab_size,
max_num_positions_in_pe_encoder,
max_num_positions_in_pe_decoder,
dropout_rate=0.1
#**kwargs,
):
"""
Parameters:
num_layers (int): Number of layers in both Encoder and Decoder.
d_model (int): Dimension of the model.
num_heads (int): Number of attention heads.
d_feedforward (int): Dimension of the feed forward network.
input_vocab_size (int): Size of the input vocabulary.
target_vocab_size (int): Size of the target vocabulary.
max_num_positions_in_pe_encoder (int): The maximum positions for input.
max_num_positions_in_pe_decoder (int): The maximum positions for
target.
dropout_rate (float): Dropout dropout_rate.
"""
super(Transformer, self).__init__()
self.encoder = Encoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
input_vocab_size=input_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_encoder,
dropout_rate=dropout_rate,
)
self.decoder = Decoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
target_vocab_size=target_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_decoder,
dropout_rate=dropout_rate,
)
self.final_layer = Dense(target_vocab_size)
class Transformer(tf.keras.Model):
“”"
The Transformer model architecture, consisting of an Encoder and Decoder.
“”"
def __init__(
self,
num_layers,
d_model,
num_heads,
d_feedforward,
input_vocab_size,
target_vocab_size,
max_num_positions_in_pe_encoder,
max_num_positions_in_pe_decoder,
dropout_rate=0.1
#**kwargs,
):
"""
Parameters:
num_layers (int): Number of layers in both Encoder and Decoder.
d_model (int): Dimension of the model.
num_heads (int): Number of attention heads.
d_feedforward (int): Dimension of the feed forward network.
input_vocab_size (int): Size of the input vocabulary.
target_vocab_size (int): Size of the target vocabulary.
max_num_positions_in_pe_encoder (int): The maximum positions for input.
max_num_positions_in_pe_decoder (int): The maximum positions for
target.
dropout_rate (float): Dropout dropout_rate.
"""
super(Transformer, self).__init__()
self.encoder = Encoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
input_vocab_size=input_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_encoder,
dropout_rate=dropout_rate,
)
self.decoder = Decoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
target_vocab_size=target_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_decoder,
dropout_rate=dropout_rate,
)
self.final_layer = Dense(target_vocab_size)I'm encountering an issue when trying to deserialize a custom Transformer model in TensorFlow.And therefore I cannot save and load my model like this : model_directory ="saved_model.keras" transformer_model.save(model_directory) load_transformer_model = load_model("saved_model.keras", custom_objects=custom_objects) The error message I'm receiving is:TypeError: <class '__main__.Transformer'> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by `get_config()` are explicitly deserialized in the model's `from_config()` method.
config={‘module’: None, ‘class_name’: ‘Transformer’, ‘config’: {‘trainable’: True, ‘dtype’: ‘float32’}, ‘registered_name’: ‘Custom>Transformer’, ‘build_config’: {‘input_shape’: [1, 10]}}.
Exception encountered: Unable to revive model from config. When overriding the get_config()
method, make sure that the returned config contains all items used as arguments in the constructor to <class ‘main.Transformer’>, which is the default behavior. You can override this default behavior by defining a from_config(cls, config)
class method to specify how to create an instance of Transformer from its config.
Received config={‘trainable’: True, ‘dtype’: ‘float32’}
Error encountered during deserialization: Transformer.init() got an unexpected keyword argument 'trainable’I tried to do this: Here’s a code snippet (last block of code) my Transformer Class (I have the following classes: Transformer, Encoder(tf.keras.layers.Layer), Decoder(tf.keras.layers.Layer), EncoderLayer(tf.keras.layers.Layer), DecoderLayer(tf.keras.layers.Layer)):What I’ve Tried:Implement/override a get_config(), from_config(), build_from_config() method for my Transformer class, then for each of my classes.
I’ve attempted to use the custom_objects when loading.Here’s an example of one the get_config() method I tried to implement for my Transformer class:# def get_config(self):
# config = {
# ‘num_layers’: self.num_layers,
# ‘d_model’: self.d_model,
# ‘num_heads’: self.num_heads,
# ‘d_feedforward’: self.d_feedforward,
# ‘input_vocab_size’: self.input_vocab_size,
# ‘target_vocab_size’: self.target_vocab_size,
# ‘max_num_positions_in_pe_encoder’: self.max_num_positions_in_pe_encoder,
# ‘max_num_positions_in_pe_decoder’: self.max_num_positions_in_pe_decoder,
# ‘dropout_rate’: self.dropout_rate,
# }
# return config
def get_config(self):
# config = {
# 'num_layers': self.num_layers,
# 'd_model': self.d_model,
# 'num_heads': self.num_heads,
# 'd_feedforward': self.d_feedforward,
# 'input_vocab_size': self.input_vocab_size,
# 'target_vocab_size': self.target_vocab_size,
# 'max_num_positions_in_pe_encoder': self.max_num_positions_in_pe_encoder,
# 'max_num_positions_in_pe_decoder': self.max_num_positions_in_pe_decoder,
# 'dropout_rate': self.dropout_rate,
# }
# return config
class Transformer(tf.keras.Model):
“”"
The Transformer model architecture, consisting of an Encoder and Decoder.
“”"
def __init__(
self,
num_layers,
d_model,
num_heads,
d_feedforward,
input_vocab_size,
target_vocab_size,
max_num_positions_in_pe_encoder,
max_num_positions_in_pe_decoder,
dropout_rate=0.1
#**kwargs,
):
"""
Parameters:
num_layers (int): Number of layers in both Encoder and Decoder.
d_model (int): Dimension of the model.
num_heads (int): Number of attention heads.
d_feedforward (int): Dimension of the feed forward network.
input_vocab_size (int): Size of the input vocabulary.
target_vocab_size (int): Size of the target vocabulary.
max_num_positions_in_pe_encoder (int): The maximum positions for input.
max_num_positions_in_pe_decoder (int): The maximum positions for
target.
dropout_rate (float): Dropout dropout_rate.
"""
super(Transformer, self).__init__()
self.encoder = Encoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
input_vocab_size=input_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_encoder,
dropout_rate=dropout_rate,
)
self.decoder = Decoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
target_vocab_size=target_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_decoder,
dropout_rate=dropout_rate,
)
self.final_layer = Dense(target_vocab_size)
class Transformer(tf.keras.Model):
“”"
The Transformer model architecture, consisting of an Encoder and Decoder.
“”"
def __init__(
self,
num_layers,
d_model,
num_heads,
d_feedforward,
input_vocab_size,
target_vocab_size,
max_num_positions_in_pe_encoder,
max_num_positions_in_pe_decoder,
dropout_rate=0.1
#**kwargs,
):
"""
Parameters:
num_layers (int): Number of layers in both Encoder and Decoder.
d_model (int): Dimension of the model.
num_heads (int): Number of attention heads.
d_feedforward (int): Dimension of the feed forward network.
input_vocab_size (int): Size of the input vocabulary.
target_vocab_size (int): Size of the target vocabulary.
max_num_positions_in_pe_encoder (int): The maximum positions for input.
max_num_positions_in_pe_decoder (int): The maximum positions for
target.
dropout_rate (float): Dropout dropout_rate.
"""
super(Transformer, self).__init__()
self.encoder = Encoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
input_vocab_size=input_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_encoder,
dropout_rate=dropout_rate,
)
self.decoder = Decoder(
num_layers=num_layers,
d_model=d_model,
num_heads=num_heads,
d_feedforward=d_feedforward,
target_vocab_size=target_vocab_size,
maximum_positions_in_pe=max_num_positions_in_pe_decoder,
dropout_rate=dropout_rate,
)
self.final_layer = Dense(target_vocab_size)