Keras: How to save tokenizer/dictionary for use on unseen data

Created on 5 Nov 2017  路  4Comments  路  Source: rstudio/keras

I have trained a model on 'seen' data (trained on a training set, tested on a test set). But I now need to apply the model to new, unseen data.

My understanding is that if I apply a tokenizer to this new data and then supply the sequenced data to my model, it will likely contain differing sequences for same words that were trained earlier.

For this purpose, I was wondering how to save the existing tokenizer and apply it on new data.

Most helpful comment

Just added save_text_tokenizer() and load_text_tokenizer() functions. Docs are here: https://keras.rstudio.com/reference/save_text_tokenizer.html

All 4 comments

Just an update: Tried saving the entire environment, hoping that all objects (tokeniser, model, etc.) will be saved too.

Loaded the environment, but since the said objects are not R objects, attempt to access them just prints this:

<pointer: 0x0>

Thanks for reporting this~ Yes, Keras objects are under the hood Python objects which of course don't automatically serialize. The solution is to use pickle to save and load the tokenizer (see example code below). I will wrap this code in higher level save_tokenizer() load_tokenizer() functions soon:

library(reticulate)
pickle <- import("pickle")
utils <- import_builtins()

# saving
with(utils$open("tokenizer.pickle", "wb") %as% handle, {
  pickle$dump(tokenizer, handle, protocol = pickle$HIGHEST_PROTOCOL)
})

# loading
with(utils$open("tokenizer.pickle", "rb") %as% handle, {
  tokenizer <- pickle$load(handle)
})

Thanks ever so much @jjallaire :+1: !

Just added save_text_tokenizer() and load_text_tokenizer() functions. Docs are here: https://keras.rstudio.com/reference/save_text_tokenizer.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LarsHill picture LarsHill  路  6Comments

faltinl picture faltinl  路  4Comments

pbhogale picture pbhogale  路  5Comments

vincenhe picture vincenhe  路  7Comments

qade544 picture qade544  路  5Comments