You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tensorflow-Hub Universal Sentence Encoder 4加载失败,求离线解决方案

离线加载Universal Sentence Encoder 4解决方案(适配AWS实例)

问题背景

在家中虚拟环境可正常运行的Universal Sentence Encoder 4,在外重新配置环境时,无论使用酒店WiFi还是移动热点,加载模块时都会冻结最终报错,报错显示tar包读取异常,提示模块无效。需要无联网加载的方案,且适配AWS实例。

运行代码

import tensorflow_hub as hub
embed =  hub.load(
            'https://tfhub.dev/google/universal-sentence-encoder/4')
print('if this prints it actually loaded')

报错信息

WARNING:absl:Deleting lock file C:\Users\reill\AppData\Local\Temp\tfhub_modules\063d866c06683311b44b4992fd46003be952409c.lock due to inactivity.
Traceback (most recent call last):
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\resolver.py", line 192, in download_and_uncompress
file_utils.extract_tarfile_to_destination(
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\file_utils.py", line 52, in extract_tarfile_to_destination
extract_file(tgz, tarinfo, abs_target_path, log_function=log_function)
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\file_utils.py", line 35, in extract_file
buf = src.read(buffer_size)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\reill\AppData\Local\Programs\Python\Python311\Lib\tarfile.py", line 696, in readinto
buf = self.read(len(b))
^^^^^^^^^^^^^^^^^
File "C:\Users\reill\AppData\Local\Programs\Python\Python311\Lib\tarfile.py", line 687, in read
raise ReadError("unexpected end of data")
tarfile.ReadError: unexpected end of data

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\test.py", line 2, in 
embed =  hub.load(
^^^^^^^^^
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\module_v2.py", line 93, in load
module_path = resolve(handle)
^^^^^^^^^^^^^^^
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\module_v2.py", line 48, in resolve
return registry.resolver(handle)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\registry.py", line 49, in __call__
return impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\compressed_module_resolver.py", line 67, in __call__
return resolver.atomic_download(handle, download, module_dir,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\resolver.py", line 421, in atomic_download
download_fn(handle, tmp_dir)
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\compressed_module_resolver.py", line 64, in download
return resolver.DownloadManager(handle).download_and_uncompress(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\reill\OneDrive\Desktop\pythontest\Testing environ\env\Lib\site-packages\tensorflow_hub\resolver.py", line 200, in download_and_uncompress
raise IOError("%s does not appear to be a valid module." % self._url)
OSError: https://tfhub.dev/google/universal-sentence-encoder/4 does not appear to be a valid module.

离线加载解决方案(适配AWS)

步骤1:预下载模型到本地

在能正常联网的环境(如家中电脑)执行代码触发模型下载并获取缓存路径:

import tensorflow_hub as hub
# 触发模型下载缓存
embed = hub.load('https://tfhub.dev/google/universal-sentence-encoder/4')
# 打印模型本地缓存路径
print(hub.resolve('https://tfhub.dev/google/universal-sentence-encoder/4'))

运行后会输出类似C:\Users\你的用户名\AppData\Local\Temp\tfhub_modules\063d866c06683311b44b4992fd46003be952409c的路径。

步骤2:打包模型文件

将上述缓存路径下的整个文件夹打包为zip或tar.gz格式,传输到目标离线环境(当前电脑或后续AWS实例)。

步骤3:离线加载模型

在目标环境中解压模型文件夹,修改代码加载本地路径:

import tensorflow_hub as hub
# 替换为解压后的模型文件夹绝对路径
embed = hub.load('/path/to/universal-sentence-encoder-4-folder')
print('if this prints it actually loaded')

步骤4:AWS实例适配

  • 传输模型:通过S3上传模型包到AWS,或用scp命令直接传到EC2实例的本地目录。
  • 环境配置:确保EC2实例的Python环境安装了与本地一致版本的tensorflow和tensorflow-hub依赖。
  • 权限设置:保证EC2实例的运行用户对模型文件夹有读取权限。
  • 运行代码:执行修改后的离线加载代码即可。

额外优化提示

  • 若用Docker部署,可将模型文件夹打包进镜像,避免每次部署重复传输模型。
  • 可以把模型存储在AWS EFS中,多个实例共享访问,节省存储空间。

内容的提问来源于stack exchange,提问作者Zor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:34:57