使用Keras微调通用句子编码器

2024-05-06 14:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试微调Universal Sentence Encoder,并将新的编码器层用于其他用途

import tensorflow as tf
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, Dropout
import tensorflow_hub as hub

module_url = "universal-sentence-encoder"
model = Sequential([
    hub.KerasLayer(module_url, input_shape=[], dtype=tf.string, trainable=True, name="use"),
    Dropout(0.5, name="dropout"),
    Dense(256, activation="relu", name="dense"),
    Dense(len(y), activation="sigmoid", name="activation")
])

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(X, y, batch_size=256, epochs=30, validation_split=0.25)

这起作用了。损失减少了,准确度也不错。现在我只想提取Universal Sentence Encoder层。然而,这是我得到的。 enter image description here

  1. 你知道我如何解决这个问题吗?我希望看到数字值的编码
  2. 是否只能按照this post的建议将tuned_use层保存为模型?理想情况下,我希望像Universal Sentence Encoder一样保存tuned_use层,这样我就可以像hub.KerasLayer(tuned_use_location, input_shape=[], dtype=tf.string)一样打开并使用它

Tags: namefromimportencodermodelusetftensorflow
1条回答
网友
1楼 · 发布于 2024-05-06 14:23:53

我希望这能帮助一些人,最后用universal-sentence-encoder-4而不是universal-sentence-encoder-large-5解决了这个问题。我花了相当多的时间进行故障排除,但这很困难,因为输入数据没有问题,并且模型训练成功。这可能是由于梯度爆炸问题,但无法将gradient clippingLeaky ReLU添加到原始体系结构中

相关问题 更多 >