示例#1
0
    def get_loss(self, y_pred, y_true, X=None, train=False):
        """Return the loss for this batch.

        Parameters
        ----------
        y_pred : torch tensor
          Predicted target values

        y_true : torch tensor
          True target values.

        X : input data, compatible with skorch.dataset.Dataset
          By default, you should be able to pass:

            * numpy arrays
            * torch tensors
            * pandas DataFrame or Series
            * a dictionary of the former three
            * a list/tuple of the former three

          If this doesn't work with your data, you have to pass a
          ``Dataset`` that can deal with the data.

        train : bool (default=False)
          Whether train mode should be used or not.

        """
        y_true = to_var(y_true, use_cuda=self.use_cuda)
        return self.criterion_(y_pred, y_true)
示例#2
0
 def get_loss(self, y_pred, y_true, X=None, train=False):
     y_true = to_var(y_true, use_cuda=self.use_cuda)
     y_pred_log = torch.log(y_pred)
     return self.criterion_(
         y_pred_log,
         self._prepare_target_for_loss(y_true),
     )
示例#3
0
 def get_loss(self, y_pred, y_true, X=None, training=False):
     y_true = to_var(y_true, use_cuda=False)
     loss_a = torch.abs(y_true.float() - y_pred[:, 1]).mean()
     loss_b = ((y_true.float() - y_pred[:, 1])**2).mean()
     if training:
         self.history.record_batch('loss_a', to_numpy(loss_a)[0])
         self.history.record_batch('loss_b', to_numpy(loss_b)[0])
     return loss_a + loss_b
示例#4
0
    def infer(self, x, **fit_params):
        """Perform a single inference step on a batch of data.

        Parameters
        ----------
        x : input data
          A batch of the input data.

        **fit_params : dict
          Additional parameters passed to the ``forward`` method of
          the module and to the train_split call.

        """
        x = to_var(x, use_cuda=self.use_cuda)
        if isinstance(x, dict):
            x_dict = self._merge_x_and_fit_params(x, fit_params)
            return self.module_(**x_dict)
        return self.module_(x, **fit_params)
示例#5
0
    def _scoring(self, net, X, y):
        """Resolve scoring and apply it to data."""
        y = self.target_extractor(y)
        if self.scoring is None:
            score = net.score(X, y)
        elif isinstance(self.scoring, str):
            # scoring is a string
            try:
                scorer = getattr(metrics, self.scoring)
            except AttributeError:
                raise NameError("A metric called '{}' does not exist, "
                                "use a valid sklearn metric name."
                                "".format(self.scoring))
            y_pred = self.pred_extractor(
                net.infer(to_var(X, use_cuda=net.use_cuda)))
            score = scorer(y, y_pred)
        else:
            # scoring is a function
            score = self.scoring(net, X, y)

        return score
示例#6
0
    def layer_data_viz(self, expe_params, viz_params):
        expe_name = expe_params.get('expe_name', 'nan')
        viz_tag = '{}_{}_'.format(viz_params.get('name', 'nan'),
                                  viz_params.get('layer_name', 'nan'),
                                  str(uuid.uuid4())[:3])
        input_output = viz_params.get('input_output', None)
        label = viz_params['data'].get('y', None)
        if (input_output is None) or (label is None):
            # do nothing if no data
            return
        layer_output = to_var(input_output['layer_output'], use_cuda=False)
        label_list = to_numpy(label).tolist()

        if self.tensorboard:
            tensorboard_dir = os.path.join(expe_params.get('expe_dir', '.'),
                                           self.tensorboard_dir_name)
            tensorboard_expe_dir = os.path.join(tensorboard_dir, expe_name)
            writer = self.get_tensorboard_writer_by_logdir(
                tensorboard_expe_dir)
            writer.add_embedding(layer_output.data,
                                 metadata=label_list,
                                 global_step=viz_params['epoch'],
                                 tag=viz_tag)
示例#7
0
 def infer(self, x):
     x = to_var(x, use_cuda=self.use_cuda)
     if isinstance(x, dict):
         return self.module_(**x)
     return self.module_(x)