简体   繁体   English

Keras:在使用标准化数据训练的模型中使用预测?

[英]Keras: Using Predict with a Model Trained with Normalized Data?

I'm creating a deep neural network in Keras to perform an NN regression using tabular data. 我正在Keras创建一个深度神经网络,使用表格数据执行NN回归。 Best practice is to normalize the inputs and output series. 最佳做法是规范化输入和输出系列。 I'd also like to use the predict function to provide estimates of the model's output for various sets of inputs. 我还想使用predict函数来为各种输入集提供模型输出的估计。 If the training data was normalized, I assume I'll need to also normalize the predict data set using the same scaling parameters. 如果训练数据被标准化,我假设我还需要使用相同的缩放参数来标准化predict数据集。 What's the best way to do this? 最好的方法是什么? Is there a way to automatically normalize the data within the model? 有没有办法自动规范化模型中的数据?

I typically like to use sklearn for this, and it does save the parameters and allows you to "inverse transform" back to the original values. 我通常喜欢使用sklearn,它确实保存了参数并允许您“反向转换”回原始值。 For predictions you would send them through the inverse_transform function to get their real predicted values. 对于预测,您将通过inverse_transform函数发送它们以获得它们的实际预测值。

Here is a working example for you to reference. 这是一个供您参考的工作示例。 The parameters of the scalers can be easily adjusted. 可以轻松调整定标器的参数。

from sklearn.preprocessing import MinMaxScaler, StandardScaler
import numpy as np

example = np.array([0., 1., 1., 0., 2., 3., 4., 4., 5.]).reshape(-1, 1)

# MinMax Scaling Example
scaler = MinMaxScaler(feature_range=(0.01, 0.99))
min_max_scaled = scaler.fit_transform(example)
min_max_orig = scaler.inverse_transform(min_max_scaled)

# Normalizing Example  (mean 0, std 1)
norm = StandardScaler()
normalized = norm.fit_transform(example)
normalized_orig = norm.inverse_transform(normalized)

There is no best way to do this (it depends on the problem), but the most common thing to do is to normalize both the train and the test data so that they have mean 0 and standard deviation 1. 没有最好的方法(这取决于问题),但最常见的做法是将列车和测试数据标准化,使它们的平均值为0,标准差为1。

Yes, with Batch Normalization you can automatically normalize the data within the model provided that you feed batches of a reasonable size into the network. 是的,使用批量标准化,您可以自动规范化模型中的数据,前提是您将合理大小的批次提供给网络。 This might produce a similar effect to data augmentation, because the signals the network will see during training will rarely repeat (as signals for one example now depend on its entire batch). 这可能会产生类似于数据增强的效果,因为网络在训练期间将看到的信号很少会重复(因为一个示例的信号现在取决于其整个批次)。 In Keras, this can be implemented by adding a BatchNorm layer right after the input layer. 在Keras中,可以通过在输入层之后添加BatchNorm图层来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用训练有素的 keras 模型预测数据 - How do I predict data using a trained keras model Keras:如果我用标准化数据训练 model,model.predict() 是否需要标准化数据? - Keras: Does model.predict() require normalized data if I train the model with normalized data? 如何使用训练有素的Keras GRU模型预测新的数据系列? - How to predict a new data series with a trained Keras GRU model? 如何使用Keras训练模型预测输入图像? - How to predict input image using trained model in Keras? 错误“IndexError:如何在 Keras 中使用经过训练的 model 预测输入图像? - Error “IndexError: How to predict input image using trained model in Keras? 如何在Keras中使用训练模型预测输入图像? - How to predict input image with trained model in Keras? 如何使用经过训练的 XGB 分类模型预测新数据行? - How to predict on new data row using trained XGB classification model? 使用经过训练的 model 预测具有不同 input_shape 数据的类 - Using trained model to predict classes with data of different input_shape 当使用python 3调用预测函数时,使用python 2训练的keras模型会给出nan值 - keras model trained with python 2 gives nan values when predict functions is called using python 3 如何在 Keras、.h5 保存文件中使用训练有素的 model 预测输入图像? - How to predict input image using trained model in Keras, .h5 saved file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM