2

I created a simple Keras regression model and trained it and then saved it in h5 format. Then in order to deploy on greengrass, i compiled this model using neo and then deployed it and inferenced using lamda function. Final prediction result is slightly different than the prediction of actual keras model.

Model:

import pandas as pd
from keras.models import Sequential
from keras.layers import *

model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')

Is it an expected behaviour?

Anudocs
  • 686
  • 1
  • 13
  • 54

1 Answers1

1

This is because of how you saved your Keras model. This has been a common issue. Keras runs tf.global_variables_initializer if you do not specify it to do so manually, and so this means that when you try to save your model it will first re-initialize all of the weights.

To prevent this, in Keras you can set the _MANUAL_VAR_INIT flag in the Tensorflow backend prior to training your model:

from keras.backend import manual_variable_initialization
manual_variable_initialization(True)

After doing this, the results using Neo and your Keras model should align.

Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44