当前位置: 首页>>代码示例>>Python>>正文


Python tensorflow.EstimatorSpec方法代码示例

本文整理汇总了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 
开发者ID:brain-research,项目名称:deep-molecular-massspec,代码行数:56,代码来源:molecule_estimator.py


注:本文中的tensorflow.EstimatorSpec方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。