我试图加载谷歌通用句子编码器v1与Tensorflow 2.4.1。GUSE v4加载tensorflow 2, GUSE v1加载tensorflow 1,但不幸的是,如果可能的话,我需要使用tensorflow 2运行GUSE v1的实验。
第一次尝试:
>>> import tensorflow_hub as hub
>>> guse_v1 = hub.load("https://tfhub.dev/google/universal-sentence-encoder/1")
给出了很多警告,然后
>>> guse_v1(["This is a sentence.",])
与失败
TypeError: 'AutoTrackable' object is not callable
我在谷歌上搜索这个错误,发现https://www.tensorflow.org/hub/common_issues,这导致我尝试:
>>> guse_v1.signatures["default"](["This is a sentence.",])
导致
TypeError: pruned(sentence): expected argument #0(zero-based) to be a Tensor; got list (['This is a sentence.'])
我不知道如何解决这个问题,但经过一些更多的挖掘,我发现https://www.tensorflow.org/hub/model_compatibility所以我尝试:
>>> guse_v1 = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/1", signature="default")
>>> guse_v1(["This is a sentence.",])
但是对于
,这仍然失败TypeError: pruned(sentence): expected argument #0(zero-based) to be a Tensor; got list (['This is a sentence.'])
你知道我怎么才能让它工作吗?
import tensorflow as tf
guse_v1.signatures["default"](tf.constant(["This is a sentence."]))