使用Keras + Theano我成功地制作了一个复发的双向LSTM神经网络,能够训练和分类任意长度的DNA序列,使用以下模型(完整工作代码参见:http://pastebin.com/jBLv8B72):< / p>
sequence = Input(shape=(None, ONE_HOT_DIMENSION), dtype='float32')
dropout = Dropout(0.2)(sequence)
# bidirectional LSTM
forward_lstm = LSTM(
output_dim=50, init='uniform', inner_init='uniform', forget_bias_init='one', return_sequences=True,
activation='tanh', inner_activation='sigmoid',
)(dropout)
backward_lstm = LSTM(
output_dim=50, init='uniform', inner_init='uniform', forget_bias_init='one', return_sequences=True,
activation='tanh', inner_activation='sigmoid', go_backwards=True,
)(dropout)
blstm = merge([forward_lstm, backward_lstm], mode='concat', concat_axis=-1)
dense = TimeDistributed(Dense(NUM_CLASSES))(blstm)
self.model = Model(input=sequence, output=dense)
self.model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
为了提高模型的性能,我想添加其他图层。最优选卷积和最大池化层。我做了几次尝试,但每次都失败了。例如,将第2行更改为以下3行:
convolution = Convolution1D(filter_length=6, nb_filter=10)(sequence)
max_pooling = MaxPooling1D(pool_length=2)(convolution)
dropout = Dropout(0.2)(max_pooling)
模型编译,但抛出错误:
ValueError: Input dimension mis-match. (input[0].shape[1] = 111, input[1].shape[1] = 53)
Apply node that caused the error: Elemwise{Composite{((i0 * log(i1)) + (i2 * log(i3)))}}(timedistributed_1_target, Elemwise{clip,no_inplace}.0, Elemwise{sub,no_inplace}.0, Elemwise{sub,no_inplace}.0)
Toposort index: 546
Inputs types: [TensorType(float32, 3D), TensorType(float32, 3D), TensorType(float32, 3D), TensorType(float32, 3D)]
Inputs shapes: [(1L, 111L, 2L), (1L, 53L, 2L), (1L, 111L, 2L), (1L, 53L, 2L)]
Inputs strides: [(888L, 8L, 4L), (424L, 8L, 4L), (888L, 8L, 4L), (424L, 8L, 4L)]
Inputs values: ['not shown', 'not shown', 'not shown', 'not shown']
Outputs clients: [[Sum{axis=[1, 2], acc_dtype=float64}(Elemwise{Composite{((i0 * log(i1)) + (i2 * log(i3)))}}.0)]]
显然尺寸存在问题。我已经尝试了重塑层,但没有成功。
甚至可以在任意序列长度的上下文中使用卷积和/或最大池化层吗?
非常感谢任何有关此事的帮助!