save a model with KerasPickleWrapper including data preprocessing steps

268 Views Asked by At

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'))
0

There are 0 best solutions below