【问题标题】:Fine tune Universal Sentence Encoder with Keras使用 Keras 微调通用句子编码器
【发布时间】:2021-03-19 20:41:24
【问题描述】:

我正在尝试微调 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 层。但是,这就是我得到的。

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

【问题讨论】:

    标签: python tensorflow keras tensorflow-hub


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-13
      • 2020-04-30
      • 2020-02-27
      • 1970-01-01
      • 2021-09-28
      • 2019-12-13
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多