![](/img/trans.png)
[英]How can I apply BatchNormalization to an Input for a Keras LSTM?
[英]How to normalize LSTM input data in Keras with BatchNormalization
我的神经网络构造如下:
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
和y_hat_denorm
将具有相同的单位,即用于创建scalery
data['outputs']
scalery
。
您可以使用keras normalise
功能,也可以使用scikit-learn preprocessing
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.