我花了几个小时尝试设置Tensorflow-hub模块“通用语句编码器”的Tensorflow服务。这里有一个类似的问题:
如何使用TensorFlow服务使TensorFlow集线器嵌入成为可服务的?
我一直在Windows计算机上执行此操作。
这是我用来构建模型的代码:
import tensorflow as tf
import tensorflow_hub as hub
MODEL_NAME = 'test'
VERSION = 1
SERVE_PATH = './models/{}/{}'.format(MODEL_NAME, VERSION)
with tf.Graph().as_default():
module = hub.Module("https://tfhub.dev/google/universal-sentence-
encoder/1")
text = tf.placeholder(tf.string, [None])
embedding = module(text)
init_op = tf.group([tf.global_variables_initializer(),
tf.tables_initializer()])
with tf.Session() as session:
session.run(init_op)
tf.saved_model.simple_save(
session,
SERVE_PATH,
inputs = {"text": text},
outputs = {"embedding": embedding},
legacy_init_op = tf.tables_initializer()
)
Run Code Online (Sandbox Code Playgroud)
我已经到了运行以下行的地步:
saved_model_cli show --dir ${PWD}/models/test/1 --tag_set serve --signature_def serving_default
Run Code Online (Sandbox Code Playgroud)
给我以下结果:
The given SavedModel SignatureDef contains the following input(s):
inputs['text'] tensor_info:
dtype: …Run Code Online (Sandbox Code Playgroud) 我正在尝试将张量流中心的预训练模型使用到我的对象检测模型中。我按照官方说明将集线器中的模型包装为 KerasLayer 对象。然后我意识到我无法访问这个预训练模型中的层。但我需要使用某些特定层的输出来构建我的模型。有没有办法访问tensorflow_hub.KerasLayer对象中的层?
写入import tensorflow_hub后,出现如下错误:
class LatestModuleExporter(tf.estimator.Exporter):
Run Code Online (Sandbox Code Playgroud)
AttributeError: 模块 'tensorflow.python.estimator.estimator_lib' 没有属性 'Exporter'
我在 Windows 10 上使用 python 3.6 和 tensorflow 1.7
谢谢!
我正在为一些句子创建嵌入,它对于相似性搜索非常有用,除非句子中有一些真正不寻常的单词。
在这种情况下,真正不寻常的单词实际上包含句子中任何单词的最相似信息,但由于该单词显然不在模型的词汇表中,因此所有这些信息在嵌入过程中都会丢失。
我想获得 GUSE 嵌入模型已知的所有单词的列表,以便我可以将这些已知单词从句子中屏蔽掉,只留下“新奇”单词。
然后,我可以对目标语料库中的那些新词进行精确的词搜索,并实现相似句子搜索的可用性。
例如“我喜欢使用 Xapian!” 被嵌入为“我喜欢使用 UNK”。
如果我只是对“Xapian”进行关键字搜索而不是语义相似性搜索,我会得到比使用 GUSE 和向量 KNN 更相关的结果。
关于如何提取 GUSE 已知/使用的词汇有什么想法吗?
我尝试在TensorFlow 2.0(alpha)中运行以下代码:
import tensorflow_hub as hub
@tf.function
def elmo(texts):
elmo_module = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
return elmo_module(texts, signature="default", as_dict=True)
embeds = elmo(tf.constant(["the cat is on the mat",
"dogs are in the fog"]))
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个错误:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-1-c7f14c7ed0e9> in <module>
9
10 elmo(tf.constant(["the cat is on the mat",
---> 11 "dogs are in the fog"]))
.../tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
417 # This is the first call of __call__, so we have to initialize.
418 initializer_map = {}
--> …Run Code Online (Sandbox Code Playgroud) 我正在使用 Colab 运行文本分析代码。我想从 tensorflow_hub获得通用句子编码器大。但是任何时候运行包含以下代码的块:
module = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
RuntimeError: variable_scope module_8/ was unused but the
corresponding name_scope was already taken.
Run Code Online (Sandbox Code Playgroud)
如果您知道如何修复此错误,我将不胜感激?
尝试使用 中的hub.load函数时tensorflow_hub,出现OSError: SavedModel file does not exist at:错误。
奇怪的是,它在几天前起作用了,所以我不太明白为什么我现在会收到这个错误。
重现代码:
import tensorflow as tf
import tensorflow_hub as hub
URL = 'https://tfhub.dev/google/universal-sentence-encoder/4'
embed = hub.load(URL)
Run Code Online (Sandbox Code Playgroud)
收到的具体错误:
OSError Traceback (most recent call last)
<ipython-input-11-dfb80f0299b2> in <module>
1 URL = 'https://tfhub.dev/google/universal-sentence-encoder/4'
----> 2 embed = hub.load(URL)
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_hub/module_v2.py in load(handle, tags)
100 if tags is None and is_hub_module_v1:
101 tags = []
--> 102 obj = tf_v1.saved_model.load_v2(module_path, tags=tags)
103 obj._is_hub_module_v1 = is_hub_module_v1 # pylint: disable=protected-access
104 return obj …Run Code Online (Sandbox Code Playgroud) 我正在研究这个colab笔记本:
我想用ELMo嵌入替换gnews旋转嵌入。
因此,更换
model = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
Run Code Online (Sandbox Code Playgroud)
与:
model = "https://tfhub.dev/google/elmo/2"
Run Code Online (Sandbox Code Playgroud)
这里发生了一系列变化,例如需要
tf.compat.v1.disable_eager_execution()
Run Code Online (Sandbox Code Playgroud)
但是我不了解要成功完成此替换的图形形状。具体来说,我看到了。
#model = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
model = "https://tfhub.dev/google/elmo/2"
elmo = hub.Module(model, trainable=True, name="{}_module".format("mymod"))
hub_layer = hub.KerasLayer(elmo,
# output_shape=[3,20],
# input_shape=(1,),
dtype=tf.string,
trainable=True)
hub_layer(train_examples[:3])
Run Code Online (Sandbox Code Playgroud)
产生
<tf.Tensor 'keras_layer_14/mymod_module_14_apply_default/truediv:0' shape=(3, 1024) dtype=float32>
Run Code Online (Sandbox Code Playgroud)
这似乎很好。但:
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
# First, I have to build, because I no longer have eager executon.
model.build(input_shape=(None,1024))
model.summary()
Run Code Online (Sandbox Code Playgroud)
然后给出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-8786753617e4> in <module>()
4 model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
5 …Run Code Online (Sandbox Code Playgroud) 我试图在 PyCharm 中运行以下代码行,并且安装并导入了 tensorflow_hub。
use = hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3")
Run Code Online (Sandbox Code Playgroud)
对于以下错误有什么建议吗?因为我的项目需要这个。
Traceback (most recent call last):
File "C:\Users\Jon10\miniconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3820, in _get_op_def
return self._op_def_cache[type]
KeyError: 'SentencepieceOp'
Run Code Online (Sandbox Code Playgroud)
在处理上述异常的过程中,又发生了一个异常:
Traceback (most recent call last):
File "C:/Users/Jon10/OneDrive/Documents/Computer Science/Dissertation/PythonPractice/TFTest/test.py", line 28, in <module>
use = hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3")
File "C:\Users\Jon10\miniconda3\envs\tensorflow\lib\site-packages\tensorflow_hub\module_v2.py", line 102, in load
obj = tf_v1.saved_model.load_v2(module_path, tags=tags)
File "C:\Users\Jon10\miniconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\saved_model\load.py", line 517, in load
return load_internal(export_dir, tags)
File "C:\Users\Jon10\miniconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\saved_model\load.py", line 541, in load_internal
export_dir)
File "C:\Users\Jon10\miniconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\saved_model\load.py", line 114, in __init__
meta_graph.graph_def.library))
File "C:\Users\Jon10\miniconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\saved_model\function_deserialization.py", line 312, in load_function_def_library
copy, copy_functions=False) …Run Code Online (Sandbox Code Playgroud) 我是 Tensorflow Hub 的新手。我想使用I3D模块并将这个网络微调到另一个数据集,我需要获得最后一个隐藏层以及其他一些层的输出。我想知道是否有办法获得其他层的激活。为 I3D 提供的唯一签名只是“默认”。我认为应该有一种方法可以使用 Tensorflow Hub 模块轻松获取所有层的输出。
import tensorflow_hub as hub
module = hub.Module("https://tfhub.dev/deepmind/i3d-kinetics-600/1", trainable=False)
logits = module(inp)
Run Code Online (Sandbox Code Playgroud)
这会给我最终的层输出。如何获取其他层的输出,例如第二个卷积层的输出?
tensorflow-hub ×10
tensorflow ×8
python ×4
directory ×1
elmo ×1
file ×1
keras ×1
oserror ×1
pycharm ×1