简体   繁体   中英

Keras: Does model.predict() require normalized data if I train the model with normalized data?

After completing model training using Keras I am trying to use Keras' model.predict() in order to test the model on novel inputs.

When I trained the model, I normalized my training data with Scikit Learn's MinMaxScaler() .

Do I need to normalize the data as well when using model.predict() ? If so, how do I do it?

Yes. You need. Because your model has learned from data with a specific scale, so, it's better to convert your data to the same scale as your model works, then let it predict.

For example, you may use the Scikitlearn library to normalize and standardize the data:

x_scaler = StandardScaler()
x_train = x_scaler.fit_transform(x_train)
x_test = x_scaler.transform(x_test)

y_scaler = StandardScaler()
y_train = y_scaler.fit_transform(y_train)
y_test = y_scaler.transform(y_test)

Then, for the prediction you should use the same normalization parameters for the training dataset and then scale reverse to get back to the previous scale with the predicted value like this:

preds = y_scaler.inverse_transform(
         model.predict(x_scaler.transform(pred_input))
         )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM