1

最近,我尝试在 tensorflow 中使用 elmo,但我遇到了一些错误,如果你能帮助我,我将非常感激。

这是我的测试代码:

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import urllib.request

if __name__ == '__main__':
        elmo = hub.Module('https://tfhub.dev/google/elmo/3',trainable=True)
        x = ["Hi my friend"]
        embeddings = elmo(tf.constant(x),signature="default",as_dict=True)["elmo"]
        print(embeddings.numpy())

我运行它,我的电脑会报告一个错误。

urllib.error.URLError: <urlopen error [Errno 60] Operation timed out>

我找到了很多方法,但仍然无法解决这个问题,所以如果你能帮助我,我将不胜感激。并且有所有错误报告。

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1346, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 950, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1417, in connect
    super().connect()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 921, in connect
    self.sock = self._create_connection(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/elmo_test.py", line 7, in <module>
    elmo = hub.Module('https://tfhub.dev/google/elmo/3',trainable=True)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/module.py", line 157, in __init__
    self._spec = as_module_spec(spec)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/module.py", line 30, in as_module_spec
    return load_module_spec(spec)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/module.py", line 64, in load_module_spec
    path = registry.resolver(path)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/registry.py", line 51, in __call__
    return impl(*args, **kwargs)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/compressed_module_resolver.py", line 67, in __call__
    return resolver.atomic_download(handle, download, module_dir,
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/resolver.py", line 418, in atomic_download
    download_fn(handle, tmp_dir)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/compressed_module_resolver.py", line 63, in download
    response = self._call_urlopen(request)
  File "/Users/jinsongyang/PycharmProjects/pythonProject11/lib/python3.9/site-packages/tensorflow_hub/resolver.py", line 522, in _call_urlopen
    return urllib.request.urlopen(request)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1389, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1349, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 60] Operation timed out>
4

1 回答 1

0

Tensorflow version你用的是哪个?

您需要禁用 Tensorlfow 急切执行模式才能运行此代码。

使用以下代码:

import tensorflow.compat.v1 as tf 
tf.disable_v2_behavior() 

在上述代码之前并删除.numpy()as.numpy仅在急切模式下受支持。如果您处于图形模式,它将不受支持。

于 2021-12-02T18:29:28.900 回答