Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 12 Text generation how do i save and load models? #233

Open
srivassid opened this issue Feb 10, 2024 · 6 comments
Open

Chapter 12 Text generation how do i save and load models? #233

srivassid opened this issue Feb 10, 2024 · 6 comments

Comments

@srivassid
Copy link

I am triying to save the model used in chapter 12, text generation but cannot do so. How would i be able to save it?

And load it back?

Thanks

@ifond
Copy link

ifond commented Feb 10, 2024 via email

@leocraig18
Copy link

leocraig18 commented Feb 10, 2024

You can save a keras model after training by calling the save() function:
model.save('model_name.h5')

Alternatively you could just save the models weights:
model.save_weights('model_weights_name.h5')

The above would save the model to your current directory.

To load the model:
load_model('model_name.h5')
To load the weights:
load_weights('model_weights_name.h5')

There are of course other ways to save your model. For instance if you are using a callback during training such as a model checkpoint.

I hope this is helpful.

@srivassid
Copy link
Author

i tried that, i was able to save the model but while loading the model i got the following error. I think the PositionalEmbeddings have not been taken into account.

i got this error

ValueError: Unknown layer: 'PositionalEmbedding'. Please ensure you are using a keras.utils.custom_object_scope and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

@leocraig18
Copy link

leocraig18 commented Feb 10, 2024

If you are using a custom layer then you need to specify this when calling load_model():

model = load_model('model_name.h5', custom_objects={'PositionalEmbedding': PositionalEmbedding})
or

with custom_object_scope({'PositionalEmbedding': PositionalEmbedding}):
    model = load_model('model_name.h5')

This is assuming that you have defined a custom layer in your model.

@srivassid
Copy link
Author

Loaded the model.

Now i get this error

Call arguments received by layer 'string_lookup' (type StringLookup):
  • inputs=<tf.RaggedTensor [[b'this', b'movie']]>

@blueworm-lee
Copy link

blueworm-lee commented Mar 12, 2024

Hello..

I have a same issue.

Chapter 11. Transformer Translatation from Eng to Spanish have a same issue.

`
class PositionalEmbedding(keras.layers.Layer):
...
}

class TransformerEncoder(layers.Layer):
...
}

class TransformerDecoder(layers.Layer):
...
}

...

transformer_model.save('model/eng_spa_transformer')

new_model = keras.models.load_model('model/eng_spa_transformer', custom_objects={'TransformerDecoder': TransformerDecoder,
'TransformerEncoder':TransformerEncoder,
'PositionalEmbedding':PositionalEmbedding})
`

Load new_model is ok but...
predicts = new_model.predict([eng_seq_pad, spa_seq_pad])

new_model can't predict well..

The problem is that eng_spa_transformer/assets directory is empy...

image

image

Any help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants