【发布时间】: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 层。但是,这就是我得到的。
- 你知道我该如何解决这个
nan问题吗?我希望看到数值的编码。 - 是否只能按照this post 的建议将
tuned_use层保存为模型?理想情况下,我想像Universal Sentence Encoder一样保存tuned_use层,这样我就可以像hub.KerasLayer(tuned_use_location, input_shape=[], dtype=tf.string)一样打开和使用它。
【问题讨论】:
标签: python tensorflow keras tensorflow-hub