parameters: (params,
                   graph_builder_class,
                   device_assigner,
                   weights_name=None,
                   model_head=None,
                   keys_name=None,
                   early_stopping_rounds=100,
                   early_stopping_loss_threshold=0.01,
                   num_trainers=1,
                   trainer_id=0,
                   report_feature_importances=False,
                   local_eval=False,
                   head_scope=None)
return_statement: return _model_fn
docstring: Return a model function given a way to construct a graph builder.
docstring_summary: Return a model function given a way to construct a graph builder.
docstring_tokens: Return a model function given a way to construct a graph builder .
function: def get_model_fn(params,
                   graph_builder_class,
                   device_assigner,
                   weights_name=None,
                   model_head=None,
                   keys_name=None,
                   early_stopping_rounds=100,
                   early_stopping_loss_threshold=0.01,
                   num_trainers=1,
                   trainer_id=0,
                   report_feature_importances=False,
                   local_eval=False,
                   head_scope=None):
    """Return a model function given a way to construct a graph builder."""
    if model_head is None:
      model_head = get_default_head(params, weights_name)
  
    def _model_fn(features, labels, mode):
      """Function that returns predictions, training loss, and training op."""
      if (isinstance(features, ops.Tensor) or
          isinstance(features, sparse_tensor.SparseTensor)):
        features = {'features': features}
      weights = None
      if weights_name and weights_name in features:
        weights = features.pop(weights_name)
  
      keys = None
      if keys_name and keys_name in features:
        keys = features.pop(keys_name)
  
      # If we're doing eval, optionally ignore device_assigner.
      # Also ignore device assigner if we're exporting (mode == INFER)
      dev_assn = device_assigner
      if (mode == model_fn_lib.ModeKeys.INFER or
          (local_eval and mode == model_fn_lib.ModeKeys.EVAL)):
        dev_assn = None
  
      graph_builder = graph_builder_class(params,
                                          device_assigner=dev_assn)
  
      logits = graph_builder.inference_graph(features)
      # For binary classification problems, convert probabilities to logits.
      # Includes hack to get around the fact that a probability might be 0 or 1.
      if not params.regression and params.num_classes == 2:
        class_1_probs = array_ops.slice(logits, [0, 1], [-1, 1])
        logits = math_ops.log(
            math_ops.maximum(class_1_probs / math_ops.maximum(
                1.0 - class_1_probs, EPSILON), EPSILON))
  
      # labels might be None if we're doing prediction (which brings up the
      # question of why we force everything to adhere to a single model_fn).
      training_graph = None
      training_hooks = []
      if labels is not None and mode == model_fn_lib.ModeKeys.TRAIN:
        with ops.control_dependencies([logits.op]):
          training_graph = control_flow_ops.group(
              graph_builder.training_graph(
                  features, labels, input_weights=weights,
                  num_trainers=num_trainers,
                  trainer_id=trainer_id),
              state_ops.assign_add(contrib_framework.get_global_step(), 1))
  
      # Put weights back in
      if weights is not None:
        features[weights_name] = weights
  
      # TensorForest's training graph isn't calculated directly from the loss
      # like many other models.
      def _train_fn(unused_loss):
        return training_graph
  
      model_ops = model_head.create_model_fn_ops(
          features=features,
          labels=labels,
          mode=mode,
          train_op_fn=_train_fn,
          logits=logits,
          scope=head_scope)
  
      if report_feature_importances:
        training_hooks.append(TensorForestRunOpAtEndHook(
            {'feature_importances': graph_builder.feature_importances()}))
  
      if early_stopping_rounds:
        training_hooks.append(
            TensorForestLossHook(
                early_stopping_rounds,
                early_stopping_loss_threshold=early_stopping_loss_threshold,
                loss_op=model_ops.loss))
  
      model_ops.training_hooks.extend(training_hooks)
  
      if keys is not None:
        model_ops.predictions[keys_name] = keys
  
      return model_ops
  
    return _model_fn
function_tokens: def get_model_fn ( params , graph_builder_class , device_assigner , weights_name = None , model_head = None , keys_name = None , early_stopping_rounds = 100 , early_stopping_loss_threshold = 0.01 , num_trainers = 1 , trainer_id = 0 , report_feature_importances = False , local_eval = False , head_scope = None ) : if model_head is None : model_head = get_default_head ( params , weights_name ) def _model_fn ( features , labels , mode ) : """Function that returns predictions, training loss, and training op.""" if ( isinstance ( features , ops . Tensor ) or isinstance ( features , sparse_tensor . SparseTensor ) ) : features = { 'features' : features } weights = None if weights_name and weights_name in features : weights = features . pop ( weights_name ) keys = None if keys_name and keys_name in features : keys = features . pop ( keys_name ) # If we're doing eval, optionally ignore device_assigner. # Also ignore device assigner if we're exporting (mode == INFER) dev_assn = device_assigner if ( mode == model_fn_lib . ModeKeys . INFER or ( local_eval and mode == model_fn_lib . ModeKeys . EVAL ) ) : dev_assn = None graph_builder = graph_builder_class ( params , device_assigner = dev_assn ) logits = graph_builder . inference_graph ( features ) # For binary classification problems, convert probabilities to logits. # Includes hack to get around the fact that a probability might be 0 or 1. if not params . regression and params . num_classes == 2 : class_1_probs = array_ops . slice ( logits , [ 0 , 1 ] , [ - 1 , 1 ] ) logits = math_ops . log ( math_ops . maximum ( class_1_probs / math_ops . maximum ( 1.0 - class_1_probs , EPSILON ) , EPSILON ) ) # labels might be None if we're doing prediction (which brings up the # question of why we force everything to adhere to a single model_fn). training_graph = None training_hooks = [ ] if labels is not None and mode == model_fn_lib . ModeKeys . TRAIN : with ops . control_dependencies ( [ logits . op ] ) : training_graph = control_flow_ops . group ( graph_builder . training_graph ( features , labels , input_weights = weights , num_trainers = num_trainers , trainer_id = trainer_id ) , state_ops . assign_add ( contrib_framework . get_global_step ( ) , 1 ) ) # Put weights back in if weights is not None : features [ weights_name ] = weights # TensorForest's training graph isn't calculated directly from the loss # like many other models. def _train_fn ( unused_loss ) : return training_graph model_ops = model_head . create_model_fn_ops ( features = features , labels = labels , mode = mode , train_op_fn = _train_fn , logits = logits , scope = head_scope ) if report_feature_importances : training_hooks . append ( TensorForestRunOpAtEndHook ( { 'feature_importances' : graph_builder . feature_importances ( ) } ) ) if early_stopping_rounds : training_hooks . append ( TensorForestLossHook ( early_stopping_rounds , early_stopping_loss_threshold = early_stopping_loss_threshold , loss_op = model_ops . loss ) ) model_ops . training_hooks . extend ( training_hooks ) if keys is not None : model_ops . predictions [ keys_name ] = keys return model_ops return _model_fn