繁体   English   中英

ValueError: 层双向的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、120、1024、1024)

[英]ValueError: Input 0 of layer bidirectional is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 120, 1024, 1024)

我正在尝试命名实体识别,这里是我的 x_train 和 x_test 的详细信息: Shape X_train: (3555, 120, 1024) Shape X_test: (887, 120, 1024)

input = Input(shape=(120,))
word_embedding_size = 1024
model = Embedding(input_dim=n_words, output_dim=word_embedding_size, input_length=120)(input)
model = Bidirectional(LSTM(units=word_embedding_size, 
                           return_sequences=True, 
                           dropout=0.5, 
                           recurrent_dropout=0.5, 
                           kernel_initializer=k.initializers.he_normal()))(model)
model = LSTM(units=word_embedding_size * 2, 
             return_sequences=True, 
             dropout=0.5, 
             recurrent_dropout=0.5, 
             kernel_initializer=k.initializers.he_normal())(model)
model = TimeDistributed(Dense(n_tags, activation="relu"))(model)  # previously softmax output layer

crf = CRF(n_tags)  # CRF layer
out = crf(model)  # output
model = Model(input, out)

adam = k.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999)
model.compile(optimizer=adam, loss=crf.loss_function, metrics=[crf.accuracy, 'accuracy'])

model.summary()

model.fit(X_train , y_train, validation_data=(X_test, y_test), epochs=10, batch_size=32)

错误是: ValueError: Input 0 of layer bidirectional_14 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 120, 1024, 1024) ValueError: Input 0 of layer bidirectional_14 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 120, 1024, 1024)

这是model.summary() 在此处输入图像描述

请帮帮我,我无法通过其他答案解决。

您必须根据双向层的 output 形状在双向层之后使用 Reshape 层

这可能有效:

model.add(Reshape((120, 2048), input_shape = (2048, ))) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM