title: How does one invert an encoded prediction in Keras for model serving?
body: <p>I have a Keras model in which i have successfully added a <code>StringLookUp</code> pre-processing step as part of the model definition. This is generally a good practice because i can then feed it the raw data to get back a prediction.</p>
  <p>I am feeding the model string words that are mapped to an integer. The Y values are also string words that have been mapped to an integer.</p>
  <p>Here is the implementation of the encoder and decoders:</p>
  <pre><code>#generate the encoder and decoders
  encoder = tf.keras.layers.StringLookup(vocabulary=vocab, )
  decoder = tf.keras.layers.StringLookup(vocabulary=vocab, output_mode=&quot;int&quot;, invert=True)
  </code></pre>
  <p>Here is the some of the code that makes the inference model</p>
  <pre><code># For inference, you can export a model that accepts strings as input
  inputs = Input(shape=(6,), dtype=&quot;string&quot;)
  x = encoder(inputs)
  outputs = keras_model(x)
  inference_model = Model(inputs, outputs)
  
  inference_model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])  
  inference_model.summary()
  </code></pre>
  <p>The <code>encoder</code> above is just a function that implements <code>tf.keras.layers.StringLookup</code></p>
  <p>Now, inside the notebook, I can easily convert the predictions back to the Original String representations by using a <code>decoder</code> which implements the reverse of <code>StringLookUp</code>.</p>
  <p><em><strong>Here's my problem</strong></em>
  While this works fine inside the notebook, this isn't very practical for deploying the model as a REST API because the calling program has no way of knowing how the encoded integer maps back to the original string representation.</p>
  <p><em><strong>So the question is what strategy should I use to implement the keras predict so that it returns the original string which I can then serialize using mlflow &amp; cloudpickle to deploy it as a servable model in databricks</strong></em></p>
  <p>Any guidance would be very much appreciated. I've seen a lot of example of Keras, but none that show how to do enact this kind of behavior for model deployment.</p>