Ejemplo n.º 1
0
    def predict_on_batch(self, *x):
        """
        Returns the predictions of the network given a batch ``x``, where the
        tensors are converted into Numpy arrays.

        Args:
            x: Batch for which to predict. Should contain ``(x_1, ..., x_n)``
                where the ``x_i``'s are are ``n`` inputs. Should NOT contain the
                ground truths ``y`` as the last element contrary to the other
                methods.

        Returns:
            The predictions with tensors converted into Numpy arrays.
        """
        self.model.eval()
        with torch.no_grad():
            x = self._process_input(x)
            return torch_to_numpy(self.model(*x))
Ejemplo n.º 2
0
    def _compute_loss_and_metrics(self,
                                  x,
                                  y,
                                  return_loss_tensor=False,
                                  return_pred=False):
        x, y = self._process_input(x, y)
        x = x if isinstance(x, (list, tuple)) else (x, )
        pred_y = self.model(*x)
        loss = self.loss_function(pred_y, y)
        if not return_loss_tensor:
            loss = float(loss)
        with torch.no_grad():
            metrics = self._compute_metrics(pred_y, y)
            for epoch_metric in self.epoch_metrics:
                epoch_metric(pred_y, y)

        pred_y = torch_to_numpy(pred_y) if return_pred else None
        return loss, metrics, pred_y
Ejemplo n.º 3
0
    def _validate(self, step_iterator, return_pred=False, return_ground_truth=False):
        pred_list = None
        true_list = None
        if return_pred:
            pred_list = []
        if return_ground_truth:
            true_list = []

        with self._set_training_mode(False):
            for step, (x, y) in step_iterator:
                step.loss, step.metrics, pred_y = self._compute_loss_and_metrics(x, y, return_pred=return_pred)
                if return_pred:
                    pred_list.append(pred_y)
                if return_ground_truth:
                    true_list.append(torch_to_numpy(y))

                step.size = self._get_batch_size(x, y)

        return step_iterator.loss, step_iterator.metrics, pred_list, true_list
    def _test_checkpointer(self, checkpointer, lr_scheduler):
        scheduler_states = {}
        generator = some_data_generator(OptimizerCheckpointTest.batch_size)

        checkpointer.set_params({'epochs': OptimizerCheckpointTest.epochs, 'steps': 1})
        checkpointer.set_model(self.model)
        checkpointer.on_train_begin({})
        for epoch in range(1, OptimizerCheckpointTest.epochs + 1):
            checkpointer.on_epoch_begin(epoch, {})
            checkpointer.on_batch_begin(1, {})
            loss = self._update_model(generator)
            checkpointer.on_batch_end(1, {'batch': 1, 'size': OptimizerCheckpointTest.batch_size, 'loss': loss})
            checkpointer.on_epoch_end(epoch, {'epoch': epoch, 'loss': loss, 'val_loss': 1})
            filename = self.checkpoint_filename.format(epoch=epoch)
            self.assertTrue(os.path.isfile(filename))
            scheduler_states[epoch] = torch_to_numpy(lr_scheduler.scheduler.state_dict(), copy=True)
        checkpointer.on_train_end({})

        self._test_checkpoint(scheduler_states, lr_scheduler)
Ejemplo n.º 5
0
    def predict(self, x):
        r"""
        Returns the predictions of the network given an input ``x``, where the output tensors
        are converted into Numpy arrays.

        Arguments
        ---------
            x : Union[torch.Tensor, np.ndarray]
                Input Dataset (should not contains output) for which the model should predict readout.

        Returns
        -------
            Numpy arrays of the predictions.
        """
        pred_y = []
        self.model.eval()
        with torch.no_grad():
            x = self._process_input(x)
            pred_y.append(torch_to_numpy(self.model(x)))
        return np.concatenate(pred_y)
Ejemplo n.º 6
0
    def _compute_loss_and_metrics(self,
                                  x,
                                  y,
                                  *,
                                  return_loss_tensor=False,
                                  return_pred=False):
        (adj, x, mask), *y = self._process_input(x, *y)
        mols, y, *w = y
        if len(w):
            w = w[0]
        else:
            w = None
        pred_y, side_loss = self.model(x, adj, mask)
        loss = self.loss_function(pred_y, y, weights=w) + side_loss
        if not return_loss_tensor:
            loss = float(loss)

        with torch.no_grad():
            metrics = self._compute_metrics(pred_y, y)
        pred_y = torch_to_numpy(pred_y) if return_pred else None
        return loss, metrics, pred_y
Ejemplo n.º 7
0
    def predict_generator(self, generator, *, steps=None):
        """
        Returns the predictions of the network given batches of samples ``x``, where the tensors are
        converted into Numpy arrays.

        generator: Generator-like object for the dataset. The generator must yield a batch of
            samples. See the :func:`fit_generator()` method for details on the types of generators
            supported. This should only yield input data ``x`` and not the target ``y``.
        steps (int, optional): Number of iterations done on ``generator``.
            (Defaults the number of steps needed to see the entire dataset)

        Returns:
            List of the predictions of each batch with tensors converted into Numpy arrays.
        """
        if steps is None and hasattr(generator, '__len__'):
            steps = len(generator)
        pred_y = []
        with self._set_training_mode(False):
            for _, x in _get_step_iterator(steps, generator):
                x = self._process_input(x)
                x = x if isinstance(x, (tuple, list)) else (x, )
                pred_y.append(torch_to_numpy(self.model(*x)))
        return pred_y