我正在尝试使用TPU引擎打开google colab工作簿中google存储桶中存储的文件。然而,我总是面临错误:
FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'
我的问题很简单:我应该如何使谷歌存储中的一个桶从谷歌colab中可读?我什么都试过了:
但没有一种选择是正确的。更让我困惑的是,公开水桶的作用是有限的。我也读过this post,但答案没有帮助。此外,我并不真正关心阅读或写作的权利
我正在以以下方式初始化TPU:
import os
use_tpu = True #@param {type:"boolean"}
bucket = 'vocab_jb'
if use_tpu:
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
from google.colab import auth
auth.authenticate_user()
%tensorflow_version 2.x
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR']) # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
with open("gs://vocab_jb/merges.txt", 'rb') as f:
a = f.read()
FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'
找到了使用库
gcsfs
的article,该库在colab中读取云存储桶。我查了一下GCSFS,这个库是beta版的,不是官方的谷歌库只需确保首先在collab中安装库
下面是代码中的实现:
以下是读取示例文件时的输出片段:
您不能简单地使用os软件包在gcs上打开文件。如果您将gcs存储桶装载到文件系统中,以便操作系统可以通过FUSE访问文件,那么您将能够做到这一点。但为了使事情变得简单,你应该导入gcs 将cloudstorage导入为gcs 然后使用gcs_file=gcs.open(文件名)
有关更多示例,请参阅谷歌GCS文档https://cloud.google.com/storage/docs/downloading-objects#code-samples 或应用程序引擎的示例 https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage
我希望这能解决你的问题
相关问题 更多 >
编程相关推荐