如何在没有IOError的情况下使用GCS路径加载文件?

2024-10-03 02:40:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在googlecloudtpu上运行XLNet代码时遇到了一些问题。当我选择gs://{model_path}/...作为模型路径时,结果是IOError。在

像这样:

Traceback (most recent call last):
  File "run_classifier.py", line 903, in <module>
    tf.app.run()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "run_classifier.py", line 722, in main
    sp.Load(FLAGS.spiece_model_file)
  File "/usr/local/lib/python2.7/dist-packages/sentencepiece.py", line 118, in Load
    return _sentencepiece.SentencePieceProcessor_Load(self, filename)
IOError: Not found: "gs://ykproject/pre-trained/xlnet_cased_L-24_H-1024_A-16/spiece.model": No such file or directory Error #2

原始代码是:

^{pr2}$

我试图找出原因。所以我决定在Python文件中加载GCS文件:

f = open("gs://ykproject/test.txt". "r")

错误仍然存在:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'gs://ykproject/test.txt'

Tags: run代码inpygsmostmodelline
1条回答
网友
1楼 · 发布于 2024-10-03 02:40:05

似乎您正在尝试访问对象文件,就像它们在计算机的文件系统中一样。要访问云存储上的对象(通过Python),您需要实例化客户机,访问特定的bucket并获取对象的数据:

# Imports the Google Cloud client library
from google.cloud import storage

# Instantiates a client
storage_client = storage.Client()

# Instantiates the bucket
bucket = storage_client.get_bucket(bucket_name)

# Instantiates the object
blob = bucket.blob(source_blob_name)

# Optionally download the object into your file system
blob.download_to_filename(destination_file_name)

{下载有关云存储的更多信息}

另外,确保您的云TPU服务帐户可以访问云存储,如果没有,您可以使用“gsutil”CLI工具更新权限。像这样(阅读):

^{pr2}$

或者像这样(写作):

gsutil acl ch -u [SERVICE_ACCOUNT]:WRITER gs://[BUCKET_NAME]

有关它的信息here。在

相关问题 更多 >