How to fix "RuntimeError: Missing implementation that supports: loader" when calling hub.text_embedding_column method?

How to fix "RuntimeError: Missing implementation that supports: loader" when calling hub.text_embedding_column method?

我正在尝试拟合文本分类模型。因此我想使用 tensorflow-hub 提供的 text_embedding_column 函数。不幸的是我收到运行时错误

import tensorflow_hub as hub
embedded_text_feature_column = hub.text_embedding_column(
    key="sentence", 
    module_spec="https://tfhub.dev/google/nnlm-en-dim128/1")

我得到的错误如下:

RuntimeError                              Traceback (most recent call last)
<ipython-input-18-df9239a27166> in <module>()
      2 embedded_text_feature_column = hub.text_embedding_column(
      3     key="sentence",
----> 4     module_spec="https://tfhub.dev/google/nnlm-en-dim128/1")

/anaconda3/lib/python3.6/site-packages/tensorflow_hub/feature_column.py in text_embedding_column(key, module_spec, trainable)
     72      ValueError: if module_spec is not suitable for use in this feature column.
     73   
---> 74   module_spec = module.as_module_spec(module_spec)
     75   _check_module_is_text_embedding(module_spec)
     76   return _TextEmbeddingColumn(key=key, module_spec=module_spec,

/anaconda3/lib/python3.6/site-packages/tensorflow_hub/module.py in as_module_spec(spec)
     31     return spec
     32   elif isinstance(spec, six.string_types):
---> 33     return load_module_spec(spec)
     34   else:
     35     raise ValueError("Unknown module spec type: %r" % type(spec))

/anaconda3/lib/python3.6/site-packages/tensorflow_hub/module.py in load_module_spec(path)
     56   
     57   path = registry.resolver(path)
---> 58   return registry.loader(path)
     59 
     60 

/anaconda3/lib/python3.6/site-packages/tensorflow_hub/registry.py in __call__(self, *args, **kwargs)
     43     raise RuntimeError(
     44         "Missing implementation that supports: %s(*%r, **%r)" % (
---> 45             self._name, args, kwargs))
     46 
     47 

RuntimeError: Missing implementation that supports: loader(*('/var/folders/pc/h0fr0z2x1pjbmdb63mhn84_w0000gn/T/tfhub_modules/32f2b2259e1cc8ca58c876921748361283e73997',), **{})

我遇到了同样的错误,这就是我解决它的方法;

我的错误是:

RuntimeError: Missing implementation that supports: loader(*('C:\Users\Alber\AppData\Local\Temp\tfhub_modules\a7fe827a4e68369aab0fa6a65479cd37c499e0f4',), **{})

所以问题出在以下路径:

C:/Users/Alber/AppData/Local/Temp/tfhub_modules/a7fe827a4e68369aab0fa6a65479cd37c499e0f4

刚刚使用资源管理器,我检查了路径,发现a7fe827a4e68369aab0fa6a65479cd37c499e0f4文件夹是空的。我不知道为什么,但那不应该发生。

然后我就删除了 a7fe827a4e68369aab0fa6a65479cd37c499e0f4 文件夹甚至tf_hub文件夹(因为我没有没有任何其他东西,但我认为没有必要删除 tf_hub 文件夹)。

之后我 运行 脚本并再次下载 所需的模块正常

INFO:tensorflow:Using C:\Users\Alber\AppData\Local\Temp\tfhub_modules to cache modules.
INFO:tensorflow:Downloading TF-Hub Module 'https://tfhub.dev/google/nnlm-es-dim128-with-normalization/1'.
INFO:tensorflow:Downloading https://tfhub.dev/google/nnlm-es-dim128-with-normalization/1: 38.58MB
...

tensorflow 2 有新的集线器方法,称为 KerasLayer (https://www.tensorflow.org/hub)。使用它解决了我的问题。

import tensorflow as tf

import tensorflow_hub as hub

module_url = "https://tfhub.dev/google/nnlm-en-dim128/2"

embed = hub.KerasLayer(module_url)

embeddings = embed(["A long sentence.", "single-word",
                      "http://example.com"])

看起来较新的版本没有 tfhub_modules.pb,我们可能不得不使用 hub.load()。检查这个 link

https://www.tensorflow.org/hub/tf1_hub_module

已将新版本 2 更改为 1

embeded_song_list=hub.text_embedding_column(
    key="Sg", 
    module_spec="https://tfhub.dev/google/nnlm-en-dim50/2",
    trainable=False)

embeded_song_list=hub.text_embedding_column(
    key="Sg", 
    module_spec="https://tfhub.dev/google/nnlm-en-dim50/1",
    trainable=False)

然后成功了