如果您的問題未在此列出,請先搜尋 github 議題,然後再填寫新的議題。
TypeError: 'AutoTrackable' 物件不可呼叫
# BAD: Raises error
embed = hub.load('https://tfhub.dev/google/nnlm-en-dim128/1')
embed(['my text', 'batch'])
在 TF2 中使用 hub.load()
API 載入 TF1 Hub 格式的模型時,經常會發生此錯誤。新增正確的簽名應可修正此問題。如需移至 TF2 的詳細資訊,以及在 TF2 中使用 TF1 Hub 格式的模型,請參閱TF-Hub 移轉至 TF2 指南。
embed = hub.load('https://tfhub.dev/google/nnlm-en-dim128/1')
embed.signatures['default'](['my text', 'batch'])
無法下載模組
在使用 URL 中的模組過程中,可能會因網路堆疊而出現許多錯誤。通常這是執行程式碼的機器特有的問題,而非程式庫的問題。以下是常見問題的清單
「通訊協定中發生 EOF 錯誤」- 如果已安裝的 Python 版本不支援託管模組的伺服器的 TLS 需求,則很可能會產生此問題。特別是,已知 Python 2.7.5 無法解析來自 tfhub.dev 網域的模組。修正:請更新至較新的 Python 版本。
「無法驗證 tfhub.dev 的憑證」- 如果網路上的某些項目嘗試作為 dev gTLD,則很可能會產生此問題。在 .dev 用作 gTLD 之前,開發人員和架構有時會使用 .dev 名稱來協助測試程式碼。修正:找出並重新設定攔截 ".dev" 網域中名稱解析的軟體。
無法寫入快取目錄
/tmp/tfhub_modules
(或類似目錄):如需相關資訊以及如何變更其位置,請參閱快取。
如果上述錯誤和修正方式無效,您可以嘗試手動下載模組,方法是模擬附加 ?tf-hub-format=compressed
至 URL 的通訊協定,以下載必須手動解壓縮到本機檔案的 tar 壓縮檔。然後可以使用本機檔案的路徑來取代 URL。以下是一個快速範例
# Create a folder for the TF hub module.
$ mkdir /tmp/moduleA
# Download the module, and uncompress it to the destination folder. You might want to do this manually.
$ curl -L "https://tfhub.dev/google/universal-sentence-encoder/2?tf-hub-format=compressed" | tar -zxvC /tmp/moduleA
# Test to make sure it works.
$ python
> import tensorflow_hub as hub
> hub.Module("/tmp/moduleA")
在預先初始化的模組上執行推論
如果您正在編寫 Python 程式,以在輸入資料上多次套用模組,您可以套用下列方法。(注意:若要在生產服務中提供要求,請考慮TensorFlow Serving 或其他可擴充、無 Python 的解決方案。)
假設您的使用案例模型是初始化和後續要求 (例如 Django、Flask、自訂 HTTP 伺服器等),您可以如下設定服務
TF2 SavedModel
- 在初始化部分
- 載入 TF2.0 模型。
import tensorflow_hub as hub
embedding_fn = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
- 在要求部分
- 使用嵌入函式執行推論。
embedding_fn(["Hello world"])
tf.function 的此呼叫已針對效能進行最佳化,請參閱 tf.function 指南。
TF1 Hub 模組
- 在初始化部分
- 使用預留位置 (圖表的進入點) 建構圖表。
- 初始化工作階段。
import tensorflow as tf
import tensorflow_hub as hub
# Create graph and finalize (finalizing optional but recommended).
g = tf.Graph()
with g.as_default():
# We will be feeding 1D tensors of text into the graph.
text_input = tf.placeholder(dtype=tf.string, shape=[None])
embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")
embedded_text = embed(text_input)
init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()])
g.finalize()
# Create session and initialize.
session = tf.Session(graph=g)
session.run(init_op)
- 在要求部分
- 使用工作階段透過預留位置將資料饋送至圖表。
result = session.run(embedded_text, feed_dict={text_input: ["Hello world"]})
無法變更模型的 dtype (例如,從 float32 變更為 bfloat16)
TensorFlow 的 SavedModel (在 TF Hub 上共用或其他方式) 包含對固定資料類型 (通常是類神經網路的權重和中繼啟動的 float32) 運作的運算。載入 SavedModel 後,這些類型無法事後變更 (但模型發布者可以選擇發布具有不同資料類型的不同模型)。
更新模型版本
可以更新模型版本的說明文件metadata。但是,版本的資產 (模型檔案) 是不可變更的。如果您想要變更模型資產,您可以發布較新版本的模型。最佳做法是擴充說明文件,並新增變更記錄來說明版本之間的變更。