ValueError:输入0与图层batch_normalization_1不兼容:预期ndim = 3,找到的ndim = 2

时间:2019-03-24 14:24:16

标签: python tensorflow keras

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

the source code中,他们用以下代码部分定义了方法“ 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的实现来自here。其余功能来自keras。另外,在the paper中,他们共享以下结构:

DBRNN-A

在第一批规范化行中,它引发以下错误:

ValueError: Input 0 is incompatible with layer batch_normalization_1: expected ndim=3, found ndim=2

当我使用max_sentence_len=50max_sentence_len=200时,我会查看尺寸直到错误点,然后看到以下形状:

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

那么,有人在这里看到问题吗?

1 个答案:

答案 0 :(得分:0)

我想问题是在Keras结构中使用了Tensorflow代码或某些版本问题。

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

class LeiterUserAdmin(UserAdmin):
    add_form = CustomLeiterCreationForm
    form = CustomLeiterChangeForm
    model = Leiter
    # some fields defined on Leiter
    list_display = ['email', 'username',]

admin.site.register(Leiter, LeiterUserAdmin)

这很好。我用于该实现的所有源代码均在GitHub中。