我试图制作一个 DNNCLassifier ,它使用张量流来分类输入,以在Google Cloud 平台(GCP)上训练模型 . 我有一些分类功能列,我使用的是vocabulary.txt文件 . 例如:

tf.feature_column.categorical_column_with_vocabulary_file(
         key = "feature_name", 
         vocabulary_file = vocab_file,
         vocabulary_size = vocab_size
         ),

我花了几个令人沮丧的时间发现你不能在GCP中使用open()因为它无法处理gs:// . 因此,我使用以下代码来读取词汇表文件:

def read_vocab_file(file_path):   
"""Reads a vocab file to memeory.    
  Args:
    file_path: path to Vocab file in cloud storage bucket

  Returns:
    Vocab list, the size of the vocabulary   """   

  with file_io.FileIO(file_path, 'r') as f:
    #vocab_lines = open(f,'r').readlines()
    vocab_lines = f.readlines()
    vocab_size = len(vocab_lines)

  return vocab_lines, vocab_size

这允许我提交一个培训作业,我将路径作为参数传递给词汇文件 .

gcloud ml-engine工作提交培训$ JOB_NAME \ --job-dir $ MODEL_DIR \ --runtime-version 1.4 \ --module-name trainer.task \ --package-path trainer / \ _region $ REGION \ - \ --train-files $ TRAIN_DATA \ --eval-files $ EVAL_DATA \ --vocab-paths $ VOCAB \ --latlon-data-paths $ LATLON \ --train-steps 1000 \ --eval-steps 100

这适用于培训,但后来我无法做出预测 . Is there a better way to train a model in the google cloud machine learning engine environment while using vocab.txt files to create categorical feature columns?

任何使用带有tf.estimator.DNNCLassifier的分类功能的示例代码都将非常受欢迎,特别是如果它可以在具有超参数优化的GCP上运行并在 Cloud 中进行预测 .

谢谢