<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Model registry does not load adapted Normalization-Layer (Keras) correctly in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/model-registry-does-not-load-adapted-normalization-layer-keras/m-p/10875#M5929</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a problem with the model registry. I'm not sure if I'm registering the model incorrectly or if it´s a bug.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here are some code snippets:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pandas as pd
&amp;nbsp;
import mlflow
import mlflow.keras
import mlflow.tensorflow
from mlflow.tracking.client import MlflowClient
&amp;nbsp;
from tensorflow.keras import models, layers, utils, optimizers
&amp;nbsp;
&amp;nbsp;
X = pd.DataFrame({'f1': [1,2,3], 'f2': [4,5,6], 'f3': [7,8,9]})
y = pd.DataFrame({'target': [4,5,6]})
&amp;nbsp;
_layers = []
&amp;nbsp;
normalize_layer = layers.Normalization(axis=1, name="normalization")
normalize_layer.adapt(X)
_layers.append(normalize_layer)
&amp;nbsp;
h1_layer = layers.Dense(
                name="h1",
                units=2,
                activation='relu',
            )
_layers.append(h1_layer)
&amp;nbsp;
out_layer = (
    layers.Dense(name="output", units=y.shape[1], activation="linear")
)
_layers.append(out_layer)
&amp;nbsp;
denormalize_layer = layers.Normalization(
    axis=1, invert=True, name="denormalization"
)
denormalize_layer.adapt(y)
_layers.append(denormalize_layer)
&amp;nbsp;
model = models.Sequential(layers=_layers)
&amp;nbsp;
&amp;nbsp;
with mlflow.start_run(run_name='keras_bug'):
    mlflow.tensorflow.autolog()
    model.compile(optimizer='adam', loss="mae")
    history = model.fit(
        X,
        y,
        epochs=100,
        shuffle=True,
        validation_split=0.33,
        verbose=1,
    )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I use some random data and build a model with a normalization and denormalization layer. Both layers are adapted to X or y.&lt;/P&gt;&lt;P&gt;Normally these layers store the mean and standard deviation (from the training set) and use those values to normalize the following data.&lt;/P&gt;&lt;P&gt;Afterwards I register the run in the databricks model-registry.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here are the predictions from the trained - not loaded - model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;model.predict(X)
&amp;nbsp;
array([[3.9997296],
       [4.1370635],
       [3.6655028]], dtype=float32)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I use the run_id and load the logged model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;logged_model = 'runs:/33af506cd02d4a56b525134588c5c30a/model'
loaded_model = mlflow.pyfunc.load_model(logged_model)
&amp;nbsp;
loaded_model.predict(X)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;These are the results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;0
0	-6.247854
1	-6.041854
2	-6.749195&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The results are completely different compared to the results from the trained model.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Both predictions are correct if I don´t use the Normalization layers so I think the mean and standard deviations are not stored correctly in the registered model.&lt;/P&gt;&lt;P&gt;I tried to get the mean from the loaded_model but I could not find a way to get these values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a warning I receive when loading the model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;/databricks/python/lib/python3.9/site-packages/keras/backend.py:451: UserWarning: `tf.keras.backend.set_learning_phase` is deprecated and will be removed after 2020-10-11. To update it, simply pass a True/False value to the `training` argument of the `__call__` method of your layer or model.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried to set both layers to trainable=False before the registration:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;normalize_layer.trainable=False&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but this does not solve the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My environment:&lt;/P&gt;&lt;P&gt;ML-Runtime 12.1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ML-Runtime 12.1 uses tensorflow 2.10. I know that tensorflow 2.10 has a bug in the denormalization layer. This bug is solved with tf2.11 but this is another problem and has nothing to do with this behaviour.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have an idea how I could solve this problem?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Jan 2023 08:27:10 GMT</pubDate>
    <dc:creator>s_plank</dc:creator>
    <dc:date>2023-01-23T08:27:10Z</dc:date>
    <item>
      <title>Model registry does not load adapted Normalization-Layer (Keras) correctly</title>
      <link>https://community.databricks.com/t5/data-engineering/model-registry-does-not-load-adapted-normalization-layer-keras/m-p/10875#M5929</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a problem with the model registry. I'm not sure if I'm registering the model incorrectly or if it´s a bug.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here are some code snippets:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pandas as pd
&amp;nbsp;
import mlflow
import mlflow.keras
import mlflow.tensorflow
from mlflow.tracking.client import MlflowClient
&amp;nbsp;
from tensorflow.keras import models, layers, utils, optimizers
&amp;nbsp;
&amp;nbsp;
X = pd.DataFrame({'f1': [1,2,3], 'f2': [4,5,6], 'f3': [7,8,9]})
y = pd.DataFrame({'target': [4,5,6]})
&amp;nbsp;
_layers = []
&amp;nbsp;
normalize_layer = layers.Normalization(axis=1, name="normalization")
normalize_layer.adapt(X)
_layers.append(normalize_layer)
&amp;nbsp;
h1_layer = layers.Dense(
                name="h1",
                units=2,
                activation='relu',
            )
_layers.append(h1_layer)
&amp;nbsp;
out_layer = (
    layers.Dense(name="output", units=y.shape[1], activation="linear")
)
_layers.append(out_layer)
&amp;nbsp;
denormalize_layer = layers.Normalization(
    axis=1, invert=True, name="denormalization"
)
denormalize_layer.adapt(y)
_layers.append(denormalize_layer)
&amp;nbsp;
model = models.Sequential(layers=_layers)
&amp;nbsp;
&amp;nbsp;
with mlflow.start_run(run_name='keras_bug'):
    mlflow.tensorflow.autolog()
    model.compile(optimizer='adam', loss="mae")
    history = model.fit(
        X,
        y,
        epochs=100,
        shuffle=True,
        validation_split=0.33,
        verbose=1,
    )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I use some random data and build a model with a normalization and denormalization layer. Both layers are adapted to X or y.&lt;/P&gt;&lt;P&gt;Normally these layers store the mean and standard deviation (from the training set) and use those values to normalize the following data.&lt;/P&gt;&lt;P&gt;Afterwards I register the run in the databricks model-registry.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here are the predictions from the trained - not loaded - model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;model.predict(X)
&amp;nbsp;
array([[3.9997296],
       [4.1370635],
       [3.6655028]], dtype=float32)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I use the run_id and load the logged model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;logged_model = 'runs:/33af506cd02d4a56b525134588c5c30a/model'
loaded_model = mlflow.pyfunc.load_model(logged_model)
&amp;nbsp;
loaded_model.predict(X)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;These are the results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;0
0	-6.247854
1	-6.041854
2	-6.749195&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The results are completely different compared to the results from the trained model.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Both predictions are correct if I don´t use the Normalization layers so I think the mean and standard deviations are not stored correctly in the registered model.&lt;/P&gt;&lt;P&gt;I tried to get the mean from the loaded_model but I could not find a way to get these values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a warning I receive when loading the model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;/databricks/python/lib/python3.9/site-packages/keras/backend.py:451: UserWarning: `tf.keras.backend.set_learning_phase` is deprecated and will be removed after 2020-10-11. To update it, simply pass a True/False value to the `training` argument of the `__call__` method of your layer or model.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried to set both layers to trainable=False before the registration:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;normalize_layer.trainable=False&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but this does not solve the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My environment:&lt;/P&gt;&lt;P&gt;ML-Runtime 12.1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ML-Runtime 12.1 uses tensorflow 2.10. I know that tensorflow 2.10 has a bug in the denormalization layer. This bug is solved with tf2.11 but this is another problem and has nothing to do with this behaviour.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have an idea how I could solve this problem?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2023 08:27:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/model-registry-does-not-load-adapted-normalization-layer-keras/m-p/10875#M5929</guid>
      <dc:creator>s_plank</dc:creator>
      <dc:date>2023-01-23T08:27:10Z</dc:date>
    </item>
  </channel>
</rss>

