0

I would like to save a model in keras using KerasPickleWrapper as a .sav format and send it to another person in order to do the predictions. I scaled my data, and the other party will not. I was wondering if there's a way to wrap the scaling procedure, so the other party would not need to inverse the scaling before making the predictions.

Here's my logic behind the question, I don't know if this is correct to assume: If I fit the model with the scaled training data, my model will not perform well once the other person try do predictions using un-scaled data.

#scale data: 
scaler.fit(X_train)
X_train=scaler.fit_transform(X_train)
Y_train=scaler.fit_transform(Y_train)
X_test=scaler.fit_transform(X_test)
Y_test=scaler.fit_transform(Y_test)

#Model
optimizer = keras.optimizers.Adam(lr=0.0001)
model = Sequential()
model.add(Dense(1, input_dim=1, activation='relu'))
model.add(Dense(10583, activation='relu')
model.add(Dense(1, activation='linear'))

#compile model 
model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mse'])

#wrap model
mw = KerasPickleWrapper(model)
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)

#Fit model
history= mw().fit(X_train_Xaxis, Y_train_Xaxis, epochs=100, batch_size=32, validation_split=0.2,     validation_data=None, verbose=1, callbacks=[callback])

#Save Model
import pickle
filename = 'model.sav'
pickle.dump(mw, open(filename, 'wb'))
the phoenix
  • 641
  • 7
  • 15
  • I think you can dump the scalar the same way as the model: `dump(scaler, open('scaler.pkl', 'wb'))` and then use `scaler = load(open('scaler.pkl', 'rb'))` to load the scalar object. Pls let me know if this works. – Ishwar May 01 '21 at 13:59
  • @Ishwar, that would work but is there a way to have everything (model+ scaler) pickled in one file? – the phoenix May 03 '21 at 08:30
  • https://stackoverflow.com/questions/20716812/saving-and-loading-multiple-objects-in-pickle-file This could answer your question. – Ishwar May 03 '21 at 18:16
  • After dumping the model, just open the file and dump the scalar object to it. – Ishwar May 03 '21 at 18:17

0 Answers0