如何使用BatchNormalization标准化Keras中的LSTM输入数据

问题描述 投票:0回答:2

我的神经网络构造如下:

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层之前添加另一个,但这不起作用。谢谢!

python keras
2个回答
0
投票
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


-1
投票

您可以使用keras normalise函数,或者您也可以使用scikit-learn preprocessing函数。

© www.soinside.com 2019 - 2024. All rights reserved.