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

时间:2016-12-27 15:17:09

标签: python google-app-engine tensorflow google-cloud-storage google-cloud-platform

在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

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

看起来您正在为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链接