问题描述
有人可以提供Tensorflow完整工作代码的示例
Can someone provide examples of full working code for Tensorflow
tf.contrib.learn.ExportStrategy
该文档缺少示例.对于这个看似晦涩的Tensorflow操作,我也没有在Github或Stackoverflow上找到任何示例.
The documentation lacks examples. I also could not find any examples on Github or Stackoverflow for this seemingly obscure Tensorflow operation.
文档: https://www.tensorflow.org/api_docs/python/tf/contrib/learn/ExportStrategy
推荐答案
Google CloudML在这里有一个很好的工作示例: https://github.com/GoogleCloudPlatform/cloudml-samples/树/主/普查/customestimator/培训师
Google CloudML has a good working example here:https://github.com/GoogleCloudPlatform/cloudml-samples/tree/master/census/customestimator/trainer
您需要使用他们的完整代码来使示例工作,但是这里是使用ExportStrategy的要点:
You'll need to use their full code to get the example to work, but here is the gist of how to use ExportStrategy:
import tensorflow as tf
from tensorflow.contrib.learn.python.learn import learn_runner
from tensorflow.contrib.learn.python.learn.utils import (
saved_model_export_utils)
from tensorflow.contrib.training.python.training import hparam
def csv_serving_input_fn():
"""Build the serving inputs."""
csv_row = tf.placeholder(
shape=[None],
dtype=tf.string
)
features = parse_csv(csv_row)
# Ignore label column
features.pop(LABEL_COLUMN)
return tf.estimator.export.ServingInputReceiver(
features, {'csv_row': csv_row})
def example_serving_input_fn():
"""Build the serving inputs."""
example_bytestring = tf.placeholder(
shape=[None],
dtype=tf.string,
)
features = tf.parse_example(
example_bytestring,
tf.feature_column.make_parse_example_spec(INPUT_COLUMNS)
)
return tf.estimator.export.ServingInputReceiver(
features, {'example_proto': example_bytestring})
def json_serving_input_fn():
"""Build the serving inputs."""
inputs = {}
for feat in INPUT_COLUMNS:
inputs[feat.name] = tf.placeholder(shape=[None], dtype=feat.dtype)
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
SERVING_FUNCTIONS = {
'JSON': json_serving_input_fn,
'EXAMPLE': example_serving_input_fn,
'CSV': csv_serving_input_fn
}
# Run the training job
# learn_runner pulls configuration information from environment
# variables using tf.learn.RunConfig and uses this configuration
# to conditionally execute Experiment, or param server code
learn_runner.run(
generate_experiment_fn(
min_eval_frequency=args.min_eval_frequency,
eval_delay_secs=args.eval_delay_secs,
train_steps=args.train_steps,
eval_steps=args.eval_steps,
export_strategies=[saved_model_export_utils.make_export_strategy(
SERVING_FUNCTIONS[args.export_format],
exports_to_keep=1
)]
),
run_config=tf.contrib.learn.RunConfig(model_dir=args.job_dir),
hparams=hparam.HParams(**args.__dict__)
)
这篇关于tensorflow.contrib.learn.ExportStrategy的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!