我有LSTM网络,有5个输入。第一个输入的典型值为1000到3000。其余输入的值从-1到1不等。
我想插入BatchNormalization作为第一层。但是输入2-5已经在-1和1之间,第一输入比第一输入大得多。也就是说,我只想对第一个输入应用批处理规范化,并保持输入2-5不变。然后,第1(标准化)输入和2-5输入应传递到LSTM层。
+----+ +---+
1 -->| BN |-->| |
+----+ | L |
2 ----------->| S |
3 ----------->| T |
4 ----------->| M |
5 ----------->| |
+---+
怎样才能在Keras做到这一点呢?
我认为我可以为第一个输入创建带有BatchNormalization裸层的模型,然后将其与其他层连接起来。但我不确定也不知道到底是怎么做到的。
发布于 2017-11-06 21:36:24
尝试以下定义:
from keras.layers.merge import concatenate
input_tensor = Input(shape=(timesteps, 5))
# now let's split tensors
split_1 = Lambda(lambda x: x[:, :, :1])(input_tensor)
split_2 = Lambda(lambda x: x[:, :, 1:])(input_tensor)
split_1 = BatchNormalization()(split_1)
# now let's concatenate them again
follow = concatenate([split_1, split_2])
但是,正如Daniel在他的评论中所提到的那样--为了处理这些不一致,最好将数据规范化--使用BatchNormalization
可能会导致性能更差。
发布于 2017-11-06 13:31:42
考虑到您的训练数据形状为(batch,timeSteps,5)
,也许您应该像这样更改输入:
maxVal = abs(X_train[:,:,0].max())
minVal = abs(X_train[:,:,0].min())
maxVal = max(maxVal,minVal)
X_train[:,:,0] = X_train[:,:,0] / maxVal
https://stackoverflow.com/questions/47137980
复制相似问题