Пример #1
0
def features_for_problem(problem_instance,
                         p_hparams,
                         hparams,
                         data_filepatterns,
                         num_datashards,
                         mode,
                         batch_size=None,
                         name="problem_inputs"):
    """Feature map for Problem."""
    with tf.name_scope(name):
        with tf.device("/cpu:0"):  # Input reading on CPU
            capacity = (p_hparams.max_expected_batch_size_per_shard *
                        num_datashards)
            batching_scheme = data_reader.hparams_to_batching_scheme(
                hparams,
                shard_multiplier=num_datashards,
                drop_long_sequences=(mode == tf.estimator.ModeKeys.TRAIN
                                     or hparams.eval_drop_long_sequences),
                length_multiplier=(p_hparams.batch_size_multiplier))
            if batch_size:
                # If batch_size is fixed, use a single input bucket
                batching_scheme["batch_sizes"] = [batch_size]
                batching_scheme["boundaries"] = []
            feature_map = data_reader.input_pipeline(problem_instance,
                                                     data_filepatterns,
                                                     capacity, mode, hparams,
                                                     batching_scheme)

    # Reverse inputs and targets features if the problem was reversed.
    if problem_instance is not None:
        problem_instance.maybe_reverse_features(feature_map)
        problem_instance.maybe_copy_features(feature_map)
    else:
        if p_hparams.was_reversed:
            inputs = feature_map["inputs"]
            targets = feature_map["targets"]
            feature_map["inputs"] = targets
            feature_map["targets"] = inputs
        # Use the inputs as the targets if the problem is a copy problem.
        if p_hparams.was_copy:
            feature_map["targets"] = feature_map["inputs"]

    # Ensure inputs and targets are proper rank.
    if problem_instance.has_inputs:
        while len(feature_map["inputs"].get_shape()) != 4:
            feature_map["inputs"] = tf.expand_dims(feature_map["inputs"],
                                                   axis=-1)
    while len(feature_map["targets"].get_shape()) != 4:
        feature_map["targets"] = tf.expand_dims(feature_map["targets"],
                                                axis=-1)

    if problem_instance.has_inputs:
        feature_map["input_space_id"] = tf.constant(p_hparams.input_space_id)
    feature_map["target_space_id"] = tf.constant(p_hparams.target_space_id)
    return feature_map
Пример #2
0
def features_for_problem(problem_instance,
                         p_hparams,
                         hparams,
                         data_dir,
                         num_datashards,
                         mode,
                         batch_size=None,
                         dataset_split=None,
                         shard=None,
                         name="problem_inputs"):
    """Feature map for Problem."""
    with tf.name_scope(name):
        with tf.device("/cpu:0"):  # Input reading on CPU
            capacity = (p_hparams.max_expected_batch_size_per_shard *
                        num_datashards)
            batching_scheme = data_reader.hparams_to_batching_scheme(
                hparams,
                shard_multiplier=num_datashards,
                drop_long_sequences=(mode == tf.estimator.ModeKeys.TRAIN
                                     or hparams.eval_drop_long_sequences),
                length_multiplier=(p_hparams.batch_size_multiplier))
            if batch_size:
                # If batch_size is fixed, use a single input bucket
                batching_scheme["batch_sizes"] = [batch_size]
                batching_scheme["boundaries"] = []
                # Log new batching scheme if updated
                tf.logging.info("Updated batching_scheme = %s",
                                batching_scheme)
            feature_map = data_reader.input_pipeline(
                problem_instance,
                data_dir,
                capacity,
                mode,
                hparams,
                batching_scheme,
                dataset_split=dataset_split,
                shard=shard)

    # Ensure inputs and targets are proper rank.
    #feature_map["teachers"]=tf.Print(feature_map["teachers"],[feature_map["teachers"]],"#feature_map['teachers']")
    if problem_instance.has_inputs:
        while len(feature_map["inputs"].get_shape()) != 4:
            feature_map["inputs"] = tf.expand_dims(feature_map["inputs"],
                                                   axis=-1)
    while len(feature_map["targets"].get_shape()) != 4:
        feature_map["targets"] = tf.expand_dims(feature_map["targets"],
                                                axis=-1)
    while len(feature_map["teachers"].get_shape()) != 4:
        feature_map["teachers"] = tf.expand_dims(feature_map["teachers"],
                                                 axis=-1)

    if problem_instance.has_inputs:
        feature_map["input_space_id"] = tf.constant(p_hparams.input_space_id)
    feature_map["target_space_id"] = tf.constant(p_hparams.target_space_id)
    return feature_map
Пример #3
0
def features_for_problem(problem_instance,
                         p_hparams,
                         hparams,
                         data_dir,
                         num_datashards,
                         mode,
                         batch_size=None,
                         dataset_split=None,
                         shard=None,
                         name="problem_inputs"):
  """Feature map for Problem."""
  with tf.name_scope(name):
    with tf.device("/cpu:0"):  # Input reading on CPU
      capacity = (p_hparams.max_expected_batch_size_per_shard * num_datashards)
      batching_scheme = data_reader.hparams_to_batching_scheme(
          hparams,
          shard_multiplier=num_datashards,
          drop_long_sequences=(mode == tf.estimator.ModeKeys.TRAIN or
                               hparams.eval_drop_long_sequences),
          length_multiplier=(p_hparams.batch_size_multiplier))
      if batch_size:
        # If batch_size is fixed, use a single input bucket
        batching_scheme["batch_sizes"] = [batch_size]
        batching_scheme["boundaries"] = []
        # Log new batching scheme if updated
        tf.logging.info("Updated batching_scheme = %s", batching_scheme)
      feature_map = data_reader.input_pipeline(
          problem_instance,
          data_dir,
          capacity,
          mode,
          hparams,
          batching_scheme,
          dataset_split=dataset_split,
          shard=shard)

  # Ensure inputs and targets are proper rank.
  if problem_instance.has_inputs:
    while len(feature_map["inputs"].get_shape()) != 4:
      feature_map["inputs"] = tf.expand_dims(feature_map["inputs"], axis=-1)
  while len(feature_map["targets"].get_shape()) != 4:
    feature_map["targets"] = tf.expand_dims(feature_map["targets"], axis=-1)

  if problem_instance.has_inputs:
    feature_map["input_space_id"] = tf.constant(p_hparams.input_space_id)
  feature_map["target_space_id"] = tf.constant(p_hparams.target_space_id)
  return feature_map
Пример #4
0
    def input_fn():
        """Supplies input to our model.

    This function supplies input to our model, where this input is a
    function of the mode. For example, we supply different data if
    we're performing training versus evaluation.

    Returns:
      A tuple consisting of 1) a dictionary of tensors whose keys are
      the feature names, and 2) a tensor of target labels if the mode
      is not INFER (and None, otherwise).

    Raises:
      ValueError: if one of the parameters has an unsupported value.
    """
        problem_count, batches = len(hparams.problems), []
        with tf.name_scope("input_reader"):
            for n in xrange(problem_count):
                if fixed_problem is not None and n != fixed_problem:
                    continue
                problem_instance = hparams.problem_instances[n]
                p_hparams = hparams.problems[n]
                with tf.name_scope("problem_%d" % n):
                    with tf.device("/cpu:0"):  # Input reading on CPU
                        capacity = (
                            p_hparams.max_expected_batch_size_per_shard *
                            num_datashards)
                        feature_map = data_reader.input_pipeline(
                            problem_instance, data_file_patterns
                            and data_file_patterns[n], capacity, mode, hparams,
                            data_reader.hparams_to_batching_scheme(
                                hparams,
                                shard_multiplier=num_datashards,
                                drop_long_sequences=(
                                    mode == tf.estimator.ModeKeys.TRAIN
                                    or hparams.eval_drop_long_sequences),
                                length_multiplier=(
                                    p_hparams.batch_size_multiplier)))

                # Reverse inputs and targets features if the problem was reversed.
                if problem_instance is not None:
                    problem_instance.maybe_reverse_features(feature_map)
                    problem_instance.maybe_copy_features(feature_map)
                else:
                    if p_hparams.was_reversed:
                        inputs = feature_map["inputs"]
                        targets = feature_map["targets"]
                        feature_map["inputs"] = targets
                        feature_map["targets"] = inputs
                    # Use the inputs as the targets if the problem is a copy problem.
                    if p_hparams.was_copy:
                        feature_map["targets"] = feature_map["inputs"]

                # Ensure inputs and targets are proper rank.
                while len(feature_map["inputs"].get_shape()) != 4:
                    feature_map["inputs"] = tf.expand_dims(
                        feature_map["inputs"], axis=-1)
                while len(feature_map["targets"].get_shape()) != 4:
                    feature_map["targets"] = tf.expand_dims(
                        feature_map["targets"], axis=-1)

                batches.append(
                    (feature_map["inputs"], feature_map["targets"],
                     tf.constant(n), tf.constant(p_hparams.input_space_id),
                     tf.constant(p_hparams.target_space_id)))

        # We choose which problem to process.
        loss_moving_avgs = []  # Need loss moving averages for that.
        for n in xrange(problem_count):
            with tf.variable_scope("losses_avg"):
                loss_moving_avgs.append(
                    tf.get_variable("problem_%d/total_loss" % n,
                                    initializer=100.0,
                                    trainable=False))
        if fixed_problem is None:
            if (hparams.problem_choice == "uniform"
                    or mode != tf.estimator.ModeKeys.TRAIN):
                problem_choice = tf.random_uniform([],
                                                   maxval=problem_count,
                                                   dtype=tf.int32)
            elif hparams.problem_choice == "adaptive":
                loss_moving_avgs = tf.stack(loss_moving_avgs)
                problem_choice = tf.multinomial(
                    tf.reshape(loss_moving_avgs, [1, -1]), 1)
                problem_choice = tf.to_int32(tf.squeeze(problem_choice))
            elif hparams.problem_choice == "distributed":
                assert worker_replicas >= problem_count
                assert worker_replicas % problem_count == 0
                problem_choice = tf.to_int32(worker_id % problem_count)
            else:
                raise ValueError(
                    "Value of hparams.problem_choice is %s and must be "
                    "one of [uniform, adaptive, distributed]" %
                    hparams.problem_choice)

            # Inputs and targets conditional on problem_choice.
            rand_inputs, rand_target, choice, inp_id, tgt_id = cond_on_index(
                lambda n: batches[n], problem_choice, 0, problem_count - 1)
        else:
            problem_choice = tf.constant(fixed_problem)
            # Take the only constructed batch, which is the fixed_problem.
            rand_inputs, rand_target, choice, inp_id, tgt_id = batches[0]

        # Set shapes so the ranks are clear.
        rand_inputs.set_shape([None, None, None, None])
        rand_target.set_shape([None, None, None, None])
        choice.set_shape([])
        inp_id.set_shape([])
        tgt_id.set_shape([])
        #  Forced shape obfuscation is necessary for inference.
        if mode == tf.estimator.ModeKeys.PREDICT:
            rand_inputs._shape = tf.TensorShape([None, None, None, None])  # pylint: disable=protected-access
            rand_target._shape = tf.TensorShape([None, None, None, None])  # pylint: disable=protected-access

        # Final feature map.
        rand_feature_map = {
            "inputs": rand_inputs,
            "problem_choice": choice,
            "input_space_id": inp_id,
            "target_space_id": tgt_id
        }
        if mode == tf.estimator.ModeKeys.PREDICT:
            rand_feature_map["infer_targets"] = rand_target
            rand_target = None
            # This is because of a bug in the Estimator that short-circuits prediction
            # if it doesn't see a QueueRunner.  DummyQueueRunner implements the
            # minimal expected interface but does nothing.
            tf.add_to_collection(tf.GraphKeys.QUEUE_RUNNERS,
                                 DummyQueueRunner())

        return rand_feature_map, rand_target