本文整理汇总了Python中tensorflow.EstimatorSpec方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.EstimatorSpec方法的具体用法?Python tensorflow.EstimatorSpec怎么用?Python tensorflow.EstimatorSpec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.EstimatorSpec方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_model_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import EstimatorSpec [as 别名]
def make_model_fn(prediction_helper, dataset_config_file, model_dir):
"""Returns a model function for estimator given prediction base class.
Args:
prediction_helper : Helper class containing prediction, loss, and evaluation
metrics
dataset_config_file: see make_input_fn.
model_dir : directory for writing output files. If model dir is not None,
a file containing all of the necessary command line flags to rehydrate
the model will be written in model_dir.
Returns:
A function that returns a tf.EstimatorSpec
"""
def _model_fn(features, labels, params, mode=None):
"""Returns tf.EstimatorSpec."""
# Input labels are ignored. All data are in features.
del labels
if model_dir is not None:
_log_command_line_string(prediction_helper.model_type, model_dir, params)
pred_op, pred_op_for_loss = prediction_helper.make_prediction_ops(
features[fmap_constants.SPECTRUM_PREDICTION], params, mode)
loss_op = prediction_helper.make_loss(
pred_op_for_loss, features[fmap_constants.SPECTRUM_PREDICTION], params)
if mode == tf.estimator.ModeKeys.TRAIN:
train_op = tf.contrib.layers.optimize_loss(
loss=loss_op,
global_step=tf.train.get_global_step(),
clip_gradients=params.gradient_clip,
learning_rate=params.learning_rate,
optimizer='Adam')
eval_op = None
elif mode == tf.estimator.ModeKeys.PREDICT:
train_op = None
eval_op = None
elif mode == tf.estimator.ModeKeys.EVAL:
train_op = None
eval_op = prediction_helper.make_evaluation_metrics(
features, params, dataset_config_file, output_dir=model_dir)
else:
raise ValueError('Invalid mode. Must be '
'tf.estimator.ModeKeys.{TRAIN,PREDICT,EVAL}.')
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=pred_op,
loss=loss_op,
train_op=train_op,
eval_metric_ops=eval_op)
return _model_fn