4

I have a data for a regression task. The independent features(X_train) are scaled with a standard scaler. Built a Keras sequential model adding hidden layers. Compiled the model. Then fitting the model with model.fit(X_train_scaled, y_train ) Then I saved the model in a .hdf5 file.

Now how to include the scaling part inside the saved model, so that the same scaling parameters can be applied to unseen test data.

#imported all the libraries for training and evaluating the model
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42)
sc = StandardScaler()
X_train_scaled = sc.fit_transform(X_train)
X_test_scaled= sc.transform (X_test)



def build_model():
    model = keras.Sequential([layers.Dense(64, activation=tf.nn.relu,input_shape=[len(train_dataset.keys())]),
    layers.Dense(64, activation=tf.nn.relu),
    layers.Dense(1)
    ])

    optimizer = tf.keras.optimizers.RMSprop(0.001)

    model.compile(loss='mean_squared_error',
                optimizer=optimizer,
                metrics=['mean_absolute_error', 'mean_squared_error'])
    return model
model = build_model()
EPOCHS=1000
history = model.fit(X_train_scaled, y_train, epochs=EPOCHS,
                    validation_split = 0.2, verbose=0)

loss, mae, mse = model.evaluate(X_test_scaled, y_test, verbose=0)
Malathi
  • 350
  • 1
  • 11
  • 1
    Possible duplicate of [How to insert Keras model into scikit-learn pipeline?](https://stackoverflow.com/questions/42415076/how-to-insert-keras-model-into-scikit-learn-pipeline) – IonicSolutions Mar 30 '19 at 13:13

0 Answers0