Skip to content
Advertisement

Save keras preprocessing layer

I have a model where I’m doing different preprocessing, outside the model itself.

One part of the preprocessing is using a category encoder based on keras with:

JavaScript

I apply this than with

JavaScript

to my pandas dataframe.

Now I want to store my model and in order to store the model I also have to store the 2 preprocessing layers cat_index and cat_encoder. Unfortunately I wasn’t able to figure out how I can store this layers on a file system. If I try to a save function than I get

‘CategoryEncoding’ object has no attribute ‘save’

How can a preprocessing layer like this be stored to a file system so that it can be reused during inference?

One workaround that comes to my mind is to store the cat_word_list and recreate the layers, but I expect there is a more keras based approach.

Advertisement

Answer

Use the get_config layers method to get the configuration:

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

For example:

JavaScript

This should contain all information needed to recreate the layers:

JavaScript

Output:

JavaScript

You can recreate the layers like this:

JavaScript

Layers have the same configuration, e.g.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement