【发布时间】: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