我正在使用梁管道将文本预处理为整数词袋,类似于此示例 https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/reddit_tft/reddit.py
words = tft.map(tf.string_split, inputs[name])
result[name + '_bow'] = tft.string_to_int(
words, frequency_threshold=frequency_threshold)
预处理和训练似乎运行良好。我训练一个简单的线性模型并指向变换函数并运行实验。
saved_model.pbtxt 似乎保存了字典,我的目标是能够在 google cloud ml 上部署此模型进行预测,并使用原始文本作为输入进行查询:
{"inputs" : { "title": "E. D. Abbott Ltd", "text" : "Abbott of Farnham E D Abbott Limited was a British coachbuilding business" }}
运行时
gcloud ml-engine local predict \
--model-dir=$MODEL_DIR \
--json-instances="$DATA_DIR/test.json" \
我收到以下错误,不知道我做错了什么。
源代码/日志
警告:root:MetaGraph有多个签名2.对多个签名的支持是 有限的。默认情况下,我们选择命名签名。 错误:根:运行图表期间出现异常:无法从提要中获取元素 s 字节。 回溯(最近一次调用最后一次): 文件“lib/googlecloudsdk/command_lib/ml_engine/local_predict.py”,第 136 行,位于 主要的() 文件“lib/googlecloudsdk/command_lib/ml_engine/local_predict.py”,第 131 行,mai n 实例=实例) 文件“/Users/xyz/Downloads/google-cloud-sdk/lib/third_party/cloud_ml_engin e_sdk/prediction/prediction_lib.py”,第 656 行,在 local_predict 中 _, 预测 = model.predict(instances) 文件“/Users/xyz/Downloads/google-cloud-sdk/lib/third_party/cloud_ml_engin e_sdk/prediction/prediction_lib.py”,第 553 行,在预测中 输出= self._client.predict(列,统计数据) 文件“/Users/xyz/Downloads/google-cloud-sdk/lib/third_party/cloud_ml_engin e_sdk/prediction/prediction_lib.py”,第 382 行,在预测中 “运行图表时出现异常:” + str(e)) Prediction_lib.PredictionError: (4, '运行图表时出现异常: 无法 g et 元素以字节形式从 feed 中获取。')
def feature_columns(vocab_size=100000):
result = []
for key in TEXT_COLUMNS:
column = tf.contrib.layers.sparse_column_with_integerized_feature(key, vocab_size, combiner='sum')
result.append(column)
return result
model_fn = tf.contrib.learn.LinearClassifier(
feature_columns=feature_columns(),
n_classes=15,
model_dir=output_dir
)
def get_transformed_reader_input_fn(transformed_metadata,
transformed_data_paths,
batch_size,
mode):
"""Wrap the get input features function to provide the runtime arguments."""
return input_fn_maker.build_training_input_fn(
metadata=transformed_metadata,
file_pattern=(
transformed_data_paths[0] if len(transformed_data_paths) == 1
else transformed_data_paths),
training_batch_size=batch_size,
label_keys=[LABEL_COLUMN],
reader=gzip_reader_fn,
key_feature_name='key',
reader_num_threads=4,
queue_capacity=batch_size * 2,
randomize_input=(mode != tf.contrib.learn.ModeKeys.EVAL),
num_epochs=(1 if mode == tf.contrib.learn.ModeKeys.EVAL else None))
transformed_metadata = metadata_io.read_metadata(
args.transformed_metadata_path)
raw_metadata = metadata_io.read_metadata(args.raw_metadata_path)
train_input_fn = get_transformed_reader_input_fn(
transformed_metadata, args.train_data_paths, args.batch_size,
tf.contrib.learn.ModeKeys.TRAIN)
eval_input_fn = get_transformed_reader_input_fn(
transformed_metadata, args.eval_data_paths, args.batch_size,
tf.contrib.learn.ModeKeys.EVAL)
serving_input_fn = input_fn_maker.build_parsing_transforming_serving_input_fn(
raw_metadata,
args.transform_savedmodel,
raw_label_keys=[],
raw_feature_keys=model.TEXT_COLUMNS)
export_strategy = tf.contrib.learn.utils.make_export_strategy(
serving_input_fn,
default_output_alternative_key=None,
exports_to_keep=5,
as_text=True)
return Experiment(
estimator=model_fn,
train_input_fn=train_input_fn,
eval_input_fn=eval_input_fn,
export_strategies=export_strategy,
eval_metrics=model.get_eval_metrics(),
train_monitors=[],
train_steps=args.train_steps,
eval_steps=args.eval_steps,
min_eval_frequency=1
)
最佳答案
build_parsing_transforming_serving_input_fn() 的文档表示,它创建一个输入函数,将转换应用于编码为 tf.Examples 的序列化字符串的原始数据。让事情变得更复杂的是,该字符串必须进行 Base64 编码才能发送到预测服务(请参阅 section Data encoding )
我建议使用 build_default_transforming_serving_input_fn() 用于 json 输入。那么你的 json 文件应该只有
{ "title": "E. D. Abbott Ltd", "text" : "Abbott of Farnham E D Abbott Limited was a British coachbuilding business" }
{ "title": "another title", "text" : "more text" }
...
关于python - 运行图时出现异常: Unable to get element from the feed as bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43824550/