运行通用句子编码器时出错,未初始化表

时间:2020-06-17 08:52:40

标签: python python-3.x tensorflow

我正在尝试通过使用通用句子编码器进行编码来计算两个句子之间的相似度,并计算向量之间的余弦相似度。

我正在运行此代码

embed =  hub.Module("https://tfhub.dev/google/universal-sentence-encoder/1")
sent_embed = embed(sentences)
sess = tf.compat.v1.Session()
init = tf.compat.v1.global_variables_initializer()
init2 = tf.compat.v1.local_variables_initializer()
sess.run(init)
sess.run(init2)
sess.run(sent_embed)

但是,我收到此错误

FailedPreconditionError                   Traceback (most recent call last)
c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1364     try:
-> 1365       return fn(*args)
   1366     except errors.OpError as e:

c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1349       return self._call_tf_sessionrun(options, feed_dict, fetch_list,
-> 1350                                       target_list, run_metadata)
   1351 

c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1442                                             fetch_list, target_list,
-> 1443                                             run_metadata)
   1444 

FailedPreconditionError: Table not initialized.
     [[{{node module_apply_default_2/string_to_index_Lookup/hash_table_Lookup}}]]

During handling of the above exception, another exception occurred:

FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-38-f1c09144bbd2> in <module>
      4 sess.run(init)
      5 sess.run(init2)
----> 6 sess.run(sent_embed_fake)

c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    956     try:
    957       result = self._run(None, fetches, feed_dict, options_ptr,
--> 958                          run_metadata_ptr)
    959       if run_metadata:
    960         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1179     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1180       results = self._do_run(handle, final_targets, final_fetches,
-> 1181                              feed_dict_tensor, options, run_metadata)
   1182     else:
   1183       results = []

c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1357     if handle is None:
   1358       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1359                            run_metadata)
   1360     else:
   1361       return self._do_call(_prun_fn, handle, feeds, fetches)

c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1382                     '\nsession_config.graph_options.rewrite_options.'
   1383                     'disable_meta_optimizer = True')
-> 1384       raise type(e)(node_def, op, message)
   1385 
   1386   def _extend_graph(self):

FailedPreconditionError: Table not initialized.
     [[node module_apply_default_2/string_to_index_Lookup/hash_table_Lookup (defined at c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow_hub\native_module.py:610) ]]

Errors may have originated from an input operation.
Input Source operations connected to node module_apply_default_2/string_to_index_Lookup/hash_table_Lookup:
 module/string_to_index/hash_table (defined at c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow_hub\native_module.py:505)

Original stack trace for 'module_apply_default_2/string_to_index_Lookup/hash_table_Lookup':
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\traitlets\config\application.py", line 664, in launch_instance
    app.start()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel\kernelapp.py", line 597, in start
    self.io_loop.start()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\platform\asyncio.py", line 149, in start
    self.asyncio_loop.run_forever()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\asyncio\base_events.py", line 541, in run_forever
    self._run_once()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\asyncio\base_events.py", line 1786, in _run_once
    handle._run()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\asyncio\events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\ioloop.py", line 690, in <lambda>
    lambda f: self._run_callback(functools.partial(callback, future))
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\ioloop.py", line 743, in _run_callback
    ret = callback()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\gen.py", line 787, in inner
    self.run()
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\gen.py", line 748, in run
    yielded = self.gen.send(value)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel\kernelbase.py", line 365, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel\kernelbase.py", line 268, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel\kernelbase.py", line 545, in execute_request
    user_expressions, allow_stdin,
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tornado\gen.py", line 209, in wrapper
    yielded = next(result)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel\ipkernel.py", line 300, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py", line 2858, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py", line 2886, in _run_cell
    return runner(coro)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py", line 3063, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py", line 3254, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-30-f18ed993b89d>", line 1, in <module>
    sent_embed_fake = embed(sentences)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow_hub\module.py", line 261, in __call__
    name=name)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow_hub\native_module.py", line 610, in create_apply_graph
    import_scope=relative_scope_name)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\training\saver.py", line 1462, in import_meta_graph
    **kwargs)[0]
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\training\saver.py", line 1486, in _import_meta_graph_with_return_elements
    **kwargs))
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\meta_graph.py", line 799, in import_scoped_meta_graph_with_return_elements
    return_elements=return_elements)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\importer.py", line 405, in import_graph_def
    producer_op_list=producer_op_list)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\importer.py", line 513, in _import_graph_def_internal
    _ProcessNewOps(graph)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\importer.py", line 243, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\ops.py", line 3466, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\ops.py", line 3466, in <listcomp>
    for c_op in c_api_util.new_tf_operations(self)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\ops.py", line 3352, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages\tensorflow\python\framework\ops.py", line 1791, in __init__
    self._traceback = tf_stack.extract_stack()


我真的很困惑为什么会发生此错误以及如何解决该错误

我的tensorflow版本 2.2.0 我的python版本是 3.7.7

预先感谢

0 个答案:

没有答案