在张量流函数模型中获得不兼容的形状错误

时间:2021-07-05 16:43:25

标签: python tensorflow keras nlp

我正在尝试使用 tensorflow.keras 实现一个深度模型,其中包含一个嵌入层 + Conv1D + 2 个 BiLstm 层。这是顺序模式下的实现:

model = models.Sequential()
model.add(layers.Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=limit_on_length, trainable=False))
model.add(layers.Conv1D(50, 4, padding='same', activation='relu'))
model.add(layers.Bidirectional(layers.LSTM(units=200, return_sequences=True, dropout=0.2, recurrent_dropout=0.2)))
model.add(layers.Bidirectional(layers.LSTM(units=200, return_sequences=False,dropout=0.2, recurrent_dropout=0.2)))
model.add(layers.Dense(len(set(tr_intents)), activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

我像这样拟合模型:

model.fit(train_padded, to_categorical(tr_intents), epochs=15, batch_size=32, validation_split=0.1)

在这种顺序模式下,一切都进行得非常好。但是当我在功能模式下实现模型时,我得到了这种错误:

ValueError: Shapes (32, 22) and (32, 11, 22) are incompatible

这是我在功能结构中的实现:

input_layer = layers.Input(shape=(None,))
x = layers.Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=limit_on_length, trainable=False)(input_layer)
x = layers.Conv1D(50, 4, padding='same', activation='relu')(x)
x = layers.Bidirectional(layers.LSTM(units=200, return_sequences=True, dropout=0.2, recurrent_dropout=0.2))(x)
x = layers.Bidirectional(layers.LSTM(units=200, return_sequences=True, dropout=0.2, recurrent_dropout=0.2))(x)
intents_out = layers.Dense(n_intents, activation='softmax')(x)
model = models.Model(input_layer, intents_out)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

有人能帮我解决这个错误吗?我需要在功能模式下实现模型,因为我必须再添加一个输出层。 我的意图(或标签)的数量是 22,每个句子的长度为 11。

0 个答案:

没有答案