如何使用BatchNormalization规范化Keras中的LSTM输入数据



我的神经网络构造如下:

tempIn = Input(shape = (None, 4))
tempModel = LSTM(data.xRnnLosFeatures)(tempIn)
tempModel = BatchNormalization()(tempModel)
tempModel = Activation('tanh')(tempModel)
tempModel = Dropout(0.5)(tempModel)
tempModel = Dense(1)(tempModel)
model = Model(inputs=tempIn, outputs=tempModel)

然而,如果我在馈送这个网络之前不手动规范化我的输入数据,我会一直得到一个很大的错误。什么是正确规范我的输入数据的方法。我试图在LSTM层之前添加另一个,但这不起作用。谢谢

from sklearn.preprocessing import MinMaxScaler
scalerx = MinMaxScaler( feature_range=(0, 1) )  # To normalize the inputs
scalery = MinMaxScaler( feature_range=(0, 1) )  # To normalize the outputs
datax = scalerx.fit_transform( data['inputs'] ) # Assuming a dictionary with inputs
datay = scalerx.fit_transform( data['outputs'] ) # and outputs
model.fit( datax, datay )

运行模型后,您将获得[0,1]范围内的值,并且您需要恢复规范化来理解它们:

y_hat = model.predict( some_data_normalized_with_scalerx )
y_hat_denorm = scalery.inverse_transform( y_hat )

并且y_hat_denorm将从一开始就具有相同的单元,即来自data['outputs']的用于创建scalery的单元。

您可以使用kerasnormalise函数,也可以使用
scikit learnpreprocessing函数。

最新更新