首页 > 解决方案 > 如何在 Keras 中使用带有通用句子编码的 LSTM

问题描述

我正在尝试通过从 Universal Sentence Encoder 获取编码来训练模型并使用 LSTM 进行训练。这是我的代码:

module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/3"
embed = hub.Module(module_url, trainable=True)

def UniversalEmbedding(x):
    embeds = embed(tf.squeeze(tf.cast(x, tf.string)))
    return embeds

input_text = Input(shape=(num_sentences,), dtype=tf.string)
embedding = Lambda(UniversalEmbedding, output_shape=(None,512))(input_text)
lstm = LSTM(128, return_sequences=False)(embedding)
dense = Dense(256, activation="relu")(lstm )
pred = Dense(num_classes,  activation='softmax')(dense)
model = Model(inputs=[input_text], outputs=pred)
model.compile(Adam(3e-4), loss='categorical_crossentropy', 
              metrics=["accuracy", "categorical_accuracy", "top_k_categorical_accuracy"])

我收到以下错误:

ValueError: Input 0 of layer lstm_10 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 512)

问题是 USE 返回具有形状 (None, 512) 的 2-d 张量,而 LSTM 期望 3-d 张量可以使用。

标签: pythontensorflowmachine-learningkerasnlp

解决方案


您可以tf.keras.layers.Reshape在 LSTM 之前添加一个以将尺寸扩大 1. 参考 - https://www.tensorflow.org/api_docs/python/tf/keras/layers/Reshape


推荐阅读