GCP 机器学习作业:如何将行打印到 .csv 输出文件?

GCP Machine Learning job: how to print lines to .csv output file?

关于 GCP 机器学习,我正在尝试 运行 训练作业,中间输出到 .csv 文件。我在 python.

中使用 Tensorflow

这是我目前尝试过的方法:

1.

with open('gs://<bucket>/<file>', 'wt') as csv_file:

...抛出异常:IOError: [Errno 2] No such file or directory

2.

gcs_file = gcs.open(filename,
                      'w',
                      content_type='text/plain',
                      options={'x-goog-meta-foo': 'foo',
                               'x-goog-meta-bar': 'bar'},
                      retry_params=write_retry_params)
  gcs_file.write('abcde\n')
  gcs_file.write('f'*1024*4 + '\n')
  gcs_file.close()

... 抛出异常 from google.appengine.api import app_identity ImportError: No module named appengine.api

有什么想法吗?

看起来您正在为 App Engine 环境编写代码,但您确定编写代码的环境是正确的吗?

如果您使用的是常规 GCP 实例,您可能应该使用 one of the other APIs

from google.cloud import storage

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print('File {} uploaded to {}.'.format(
        source_file_name,
        destination_blob_name))

编辑:替换 API link

对于选项(1),如果你使用的是TensorFlow,你可以使用tf.gfile.Open("gs://...", mode="w")在GCS中打开一个文件进行写入。

tf.gfile 模块使用 TensorFlow 的 C++ I/O 层,其中包括对 GCS 读取和写入的支持。内置的Pythonopen()函数只会打开本地文件系统中的文件。