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

时间:2018-11-13 00:44:35

标签: python keras

我的神经网络构造如下:

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

.qmake.conf

运行模型后,您将获得[0,1]范围内的值,并且需要还原归一化以使其有意义:

make

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 ) 从一开始就具有相同的单位,即来自y_hat = model.predict( some_data_normalized_with_scalerx ) y_hat_denorm = scalery.inverse_transform( y_hat ) 的单位,用于创建y_hat_denorm