检查目标时出错:预期 time_distributed_6 有 3 个维度,但得到形状为 (200, 80) 的数组

Error when checking target: expected time_distributed_6 to have 3 dimensions, but got array with shape (200, 80)

我正在尝试使用 LSTM 实现序列标记模型。例如,我有 200 个句子,其中每个标记都有一个 1024-dim 嵌入。我还将所有句子填充到 80 维向量中。所以,我有一个形状为 (200,80,1024) 的输入矩阵。

我也填充了目标。我为句子的每个标记都有一个标签。所以我的 y 的形状是 (200,80).

我用这种方式尝试了 LSTM

from keras.models import Model, Input
from keras.layers.merge import add
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Lambda

max_len = 80 
input_text = Input(shape=(max_len,1024), dtype=tf.float32)
x = Bidirectional(LSTM(units=512, return_sequences=True,
                       recurrent_dropout=0.2, dropout=0.2))(input_text)
x_rnn = Bidirectional(LSTM(units=512, return_sequences=True,
                           recurrent_dropout=0.2, dropout=0.2))(x)
x = add([x, x_rnn])  # residual connection to the first biLSTM
out = TimeDistributed(Dense(n_tags, activation="softmax"))(x)
model = Model(input_text, out)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

history = model.fit(np.array(full_embeddings), y,batch_size=32, epochs=10, verbose=1)

但是我得到这个错误:

ValueError: Error when checking target: expected time_distributed_6 to have 3 dimensions, but got array with shape (200, 80)

谁能给我解释一下这个问题?我对 Keras 和神经网络很陌生,我无法理解原因。

谢谢

由于您的最终输出层是 time distributed,因此它具有 3 个维度。您的目标 y 也应该有 3 个维度。重塑你的 y

y = np.expand_dims(y, -1)

形状为(200, 80, 1)