我正在尝试使用通用句子编码器多语言模块。我正在使用张量流 1.14 版本。在参考了关于stackoverflow的其他问题之后,一个可能的原因是使用旧版本的tensorflow,而这里的情况并非如此。
更新:添加了python包版本
### Tensorflow version : 1.14.0
### sentencepiece: 0.1.82
### tf-sentencepiece: 0.1.82.1
use_large_module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/3" #@param ["https://tfhub.dev/google/universal-sentence-encoder/2", "https://tfhub.dev/google/universal-sentence-encoder-large/3"]
use_lite_module_url = "https://tfhub.dev/google/universal-sentence-encoder-lite/2"
use_multilingual_url = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual/1' #@param ['https://tfhub.dev/google/universal-sentence-encoder-multilingual/1', 'https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/1', 'https://tfhub.dev/google/universal-sentence-encoder-xling-many/1']
# embed = hub.Module(use_lite_module_url)
# embed = hub.Module(use_large_module_url)
embed = hub.Module(use_multilingual_url)
错误
---------------------------------------------------------------------------
NotFoundError Traceback (most recent call last)
<ipython-input-11-22dba73760e9> in <module>()
5 # embed = hub.Module(use_lite_module_url)
6 # embed = hub.Module(use_large_module_url)
----> 7 embed = hub.Module(use_multilingual_url)
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/module.py in __init__(self, spec, trainable, name, tags)
168 name=self._name,
169 trainable=self._trainable,
--> 170 tags=self._tags)
171 # pylint: enable=protected-access
172
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/native_module.py in _create_impl(self, name, trainable, tags)
338 trainable=trainable,
339 checkpoint_path=self._checkpoint_variables_path,
--> 340 name=name)
341
342 def _export(self, path, variables_saver):
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/native_module.py in __init__(self, spec, meta_graph, trainable, checkpoint_path, name)
380
381 register_ops_if_needed({
--> 382 op.name for op in self._meta_graph.meta_info_def.stripped_op_list.op})
383
384 if _is_tpu_graph_function():
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/native_module.py in register_ops_if_needed(graph_ops)
820 "Graph ops missing from the python registry (%s) are also absent from "
821 "the c++ registry."
--> 822 % missing_ops.difference(set(cpp_registry_ops.keys())))
823
824
NotFoundError: Graph ops missing from the python registry ({'SentencepieceEncodeSparse'}) are also absent from the c++ registry.
同样的问题在这里。这是我修复它的方法:
pip install tf_sentencepiece
然后你必须在代码中加载它:
import tensorflow as tf
import tensorflow_hub as hub
import tf_sentencepiece # <- ADD THIS
embed = hub.Module(...)
从错误 msg 来看,这并不明显。要解决此错误,请执行以下操作
pip install tensorflow_text
并在您的 Python 脚本导入
中import tensorflow_text as tf_txt
这应该可以解决上述错误。
在Windows中,我相信目前没有可用的解决方案,但是如果您使用的是Linux/Ubuntu,请尝试以下操作:
TFHUB_CACHE_DIR=/data/models/tf-hub
(数据/模型是你的本地路径,随心所欲地改变(
并创建一个环境变量,然后
source ~/.bashrc
以反映变化。祝你好运