Python ValueError:输入0与层批处理\u规范化\u 1不兼容:预期ndim=3,发现ndim=2

Python ValueError:输入0与层批处理\u规范化\u 1不兼容:预期ndim=3,发现ndim=2,python,tensorflow,keras,Python,Tensorflow,Keras,我正在尝试使用DeepTriage的实现,这是一种针对bug分类的深入学习方法。包括数据集、源代码和论文。我知道这是一个非常具体的领域,但我会尽量简化 在本文中,他们用以下代码部分定义了他们的方法“DBRNN-A:具有注意机制和长-短期记忆单元(LSTM)的深层双向递归神经网络”: input = Input(shape=(max_sentence_len,), dtype='int32') sequence_embed = Embedding(vocab_size, embed_size_wo

我正在尝试使用DeepTriage的实现,这是一种针对bug分类的深入学习方法。包括数据集、源代码和论文。我知道这是一个非常具体的领域,但我会尽量简化

在本文中,他们用以下代码部分定义了他们的方法“DBRNN-A:具有注意机制和长-短期记忆单元(LSTM)的深层双向递归神经网络”:

input = Input(shape=(max_sentence_len,), dtype='int32')
sequence_embed = Embedding(vocab_size, embed_size_word2vec, input_length=max_sentence_len)(input)

forwards_1 = LSTM(1024, return_sequences=True, dropout_U=0.2)(sequence_embed)
attention_1 = SoftAttentionConcat()(forwards_1)
after_dp_forward_5 = BatchNormalization()(attention_1)

backwards_1 = LSTM(1024, return_sequences=True, dropout_U=0.2, go_backwards=True)(sequence_embed)
attention_2 = SoftAttentionConcat()(backwards_1)
after_dp_backward_5 = BatchNormalization()(attention_2)

merged = merge([after_dp_forward_5, after_dp_backward_5], mode='concat', concat_axis=-1)
after_merge = Dense(1000, activation='relu')(merged)
after_dp = Dropout(0.4)(after_merge)
output = Dense(len(train_label), activation='softmax')(after_dp)                
model = Model(input=input, output=output)
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['accuracy']) 
SoftAttentionConcat
的实现来自。其余函数来自
keras
。此外,它们的共同结构如下:

在第一批标准化行中,它抛出以下错误:

ValueError: Input 0 is incompatible with layer batch_normalization_1: expected ndim=3, found ndim=2
当我使用
max_-tension_-len=50
max_-tension_-len=200
时,我查看尺寸直到错误点,我看到以下形状:

Input               -> (None, 50)
Embedding           -> (None, 50, 200)
LSTM                -> (None, None, 1024)
SoftAttentionConcat -> (None, 2048) 

那么,有人看到了这个问题吗?

我想问题在于Keras结构中使用TensorFlow代码或某些版本问题

通过使用问题和答案,我在Keras中实施了如下注意机制:

attention_1 = Dense(1, activation="tanh")(forwards_1)
attention_1 = Flatten()(attention_1)  # squeeze (None,50,1)->(None,50)
attention_1 = Activation("softmax")(attention_1)
attention_1 = RepeatVector(num_rnn_unit)(attention_1)
attention_1 = Permute([2, 1])(attention_1)
attention_1 = multiply([forwards_1, attention_1])
attention_1 = Lambda(lambda xin: K.sum(xin, axis=1), output_shape=(num_rnn_unit,))(attention_1)

last_out_1 = Lambda(lambda xin: xin[:, -1, :])(forwards_1)
sent_representation_1 = concatenate([last_out_1, attention_1])
这很有效。我用于实现的所有源代码都在中