Ejemplo n.º 1
0
def setup_training(
        snapshots: np.ndarray,
        hparams: tf.contrib.training.HParams) -> Tuple[tf.Tensor, tf.Tensor]:
    """Create Tensors for training.

  Args:
    snapshots: np.ndarray with shape [examples, x] with high-resolution
      training data.
    hparams: hyperparameters for training.

  Returns:
    Tensors for the current loss, and for taking a training step.
  """
    dataset = model.make_dataset(snapshots,
                                 hparams,
                                 dataset_type=model.Dataset.TRAINING)
    tensors = dataset.make_one_shot_iterator().get_next()

    predictions = model.predict_result(tensors['inputs'], hparams)

    loss_per_head = model.loss_per_head(predictions,
                                        labels=tensors['labels'],
                                        baseline=tensors['baseline'],
                                        hparams=hparams)
    loss = model.weighted_loss(loss_per_head, hparams)
    train_step = create_training_step(loss, hparams)

    return loss, train_step
    def __init__(self,
                 snapshots: np.ndarray,
                 hparams: tf.contrib.training.HParams,
                 training: bool = False):
        """Initialize an object for running inference.

    Args:
      snapshots: np.ndarray with shape [examples, x] with high-resolution
        training data.
      hparams: hyperparameters for training.
      training: whether to evaluate on training or validation datasets.
    """
        if training:
            dataset_type = model.Dataset.TRAINING
        else:
            dataset_type = model.Dataset.VALIDATION
        dataset = model.make_dataset(snapshots,
                                     hparams,
                                     dataset_type=dataset_type,
                                     repeat=False,
                                     evaluation=True)
        iterator = dataset.make_initializable_iterator()
        data = iterator.get_next()

        _, coarse_equation = equations.from_hparams(hparams)

        predictions = model.predict_result(data['inputs'], hparams)
        loss_per_head = model.loss_per_head(predictions,
                                            labels=data['labels'],
                                            baseline=data['baseline'],
                                            hparams=hparams)
        loss = model.weighted_loss(loss_per_head, hparams)

        results = dict(data, predictions=predictions)
        metrics = {
            k: tf.contrib.metrics.streaming_concat(v)
            for k, v in results.items()
        }
        metrics['loss'] = tf.metrics.mean(loss)

        space_loss, time_loss, integrated_loss = model.result_unstack(
            loss_per_head, coarse_equation)
        metrics['loss/space_derivatives'] = tf.metrics.mean(space_loss)
        metrics['loss/time_derivative'] = tf.metrics.mean(time_loss)
        if integrated_loss is not None:
            metrics['loss/integrated_solution'] = tf.metrics.mean(
                integrated_loss)

        initializer = tf.group(iterator.initializer,
                               tf.local_variables_initializer())

        self._initializer = initializer
        self._metrics = metrics
Ejemplo n.º 3
0
def determine_loss_scales(
        snapshots: np.ndarray,
        hparams: tf.contrib.training.HParams) -> Tuple[np.ndarray, np.ndarray]:
    """Determine scale factors for the loss.

  When passed into model.compute_loss, predictions of all zero should result
  in a loss of 1.0 when averaged over the full dataset.

  Args:
    snapshots: np.ndarray with shape [examples, x] with high-resolution
      training data.
    hparams: hyperparameters to use for training.

  Returns:
    Tuple of two numpy arrays:
      error_scale: array with dimensions [2, derivative] indicating the
        scaling in the loss to use on squared error and relative squared error
        for each derivative target.
      error_floor: numpy array with scale for weighting of relative errors.
  """
    with tf.Graph().as_default():
        dataset = model.make_dataset(snapshots, hparams, repeat=False)
        data = load_dataset(dataset)

    baseline_error = (data['labels'] - data['baseline'])**2
    percentile = 100 * hparams.error_floor_quantile
    error_floor = np.maximum(
        np.percentile(baseline_error, percentile, axis=(0, 1)), 1e-12)

    # predict zero for all derivatives, and a constant value for the integrated
    # solution over time.
    equation_type = equations.equation_type_from_hparams(hparams)
    num_zero_predictions = len(equation_type.DERIVATIVE_ORDERS) + 1
    labels_shape = data['labels'].shape
    predictions = np.concatenate([
        np.zeros(labels_shape[:-1] + (num_zero_predictions, )),
        np.repeat(data['inputs'][..., np.newaxis],
                  labels_shape[-1] - num_zero_predictions,
                  axis=-1)
    ],
                                 axis=-1)

    components = np.stack(
        model.abs_and_rel_error(predictions=predictions,
                                labels=data['labels'],
                                baseline=data['baseline'],
                                error_floor=error_floor))
    baseline_error = np.mean(components, axis=(1, 2))
    logging.info('baseline_error: %s', baseline_error)

    error_scale = np.where(baseline_error > 0, 1.0 / baseline_error, 0)
    return error_floor, error_scale