【问题标题】:Error when checking target: expected time_distributed_6 to have 3 dimensions, but got array with shape (200, 80)检查目标时出错:预期 time_distributed_6 有 3 个维度,但得到了形状为 (200, 80) 的数组
【发布时间】:2020-03-30 10:10:05
【问题描述】:

我试图用 LSTM 实现一个序列标记模型。举个例子,我有 200 个句子,其中每个标记都有一个 1024 维嵌入。我还将所有句子填充到 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 和神经网络很陌生,我无法理解原因。

谢谢

【问题讨论】:

    标签: python tensorflow keras neural-network recurrent-neural-network


    【解决方案1】:

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

    y = np.expand_dims(y, -1)
    

    形状为(200, 80, 1)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-22
      • 2019-01-16
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多