quantnn/models/pytorch/common.py
def _train_step(
        self, x, y, loss, adversarial_training, metrics=None, transformation=None
    ):
        """
        Performs a single training step and returns a dictionary of
        losses for every output.

        Args:
            x: The input data for the current batch.
            y: The output data for the current batch.
            adversarial_training: Scaling factor for adversarial training or
                None if no adversarial training should be performed.

        Returns:
            A single loss or a dictionary of losses in the case of a multi-output
            network.
        """
        y_pred = self(x)

        # Keep track of x gradients if adversarial training
        # is to be performed.
        if adversarial_training is not None:
            x.requires_grad = True

        # Make sure both outputs are dicts.
        if not isinstance(y_pred, dict):
            y_pred = {"__loss__": y_pred}
        if not isinstance(y, dict):
            y = {next(iter(y_pred.keys())): y}
        if not isinstance(loss, dict):
            loss = {k: loss for k in y_pred}

        avg_loss = None
        tot_loss = None
        losses = {}

        # Loop over keys in prediction.
        for k in y_pred:

            loss_k = loss[k]
            y_pred_k = y_pred[k]

            if loss_k.mask is not None:
                mask = torch.tensor(loss_k.mask).to(
                    dtype=y_pred_k.dtype,
                    device=y_pred_k.device
                )
            else:
                mask = None

            if isinstance(transformation, dict):
                transform_k = transformation[k]
            else:
                transform_k = transformation

            try:
                y_k = y[k]
            except KeyError:
                raise DatasetError(f"No targets provided for ouput '{k}'.")

            if y_k.ndim < y_pred_k.ndim:
                y_k = torch.unsqueeze(y_k, 1)

            if transform_k is None:
                y_k_t = y_k
                y_pred_k_t = y_pred_k
            else:
                y_k_t = transform_k(y_k)
                if mask is not None:
                    y_k_t = torch.where(y_k > mask, y_k_t, mask)
                y_pred_k_t = transform_k.invert(y_pred_k)

            if k == "__loss__":
                l = loss_k(y_pred_k, y_k_t)
            else:
                l = loss_k(y_pred_k, y_k_t, k)

            cache = {}
            with torch.no_grad():
                if metrics is not None:
                    for m in metrics:
                        m.process_batch(k, y_pred_k_t, y_k, cache=cache)

            losses[k] = l.item()
            if not avg_loss:
                avg_loss = 0.0
                tot_loss = 0.0
                n_samples = 0
            avg_loss += l
            if mask is not None:
                n = (y_k > mask).sum().item()
            else:
                n = torch.numel(y_k)
            tot_loss += (l * n).item()
            n_samples += n

        return avg_loss, tot_loss, losses, n_samples

        Performs a single training step and returns a dictionary of
        losses for every output.

        Args:
            x: The input data for the current batch.
            y: The output data for the current batch.
            adversarial_training: Scaling factor for adversarial training or
                None if no adversarial training should be performed.

        Returns:
            A single loss or a dictionary of losses in the case of a multi-output
            network.
        
Performs a single training step and returns a dictionary of
losses for every output.
Performs a single training step and returns a dictionary of losses for every output .
def _train_step(
        self, x, y, loss, adversarial_training, metrics=None, transformation=None
    ):
        y_pred = self(x)
        if adversarial_training is not None:
            x.requires_grad = True
        if not isinstance(y_pred, dict):
            y_pred = {"__loss__": y_pred}
        if not isinstance(y, dict):
            y = {next(iter(y_pred.keys())): y}
        if not isinstance(loss, dict):
            loss = {k: loss for k in y_pred}
        avg_loss = None
        tot_loss = None
        losses = {}
        for k in y_pred:
            loss_k = loss[k]
            y_pred_k = y_pred[k]
            if loss_k.mask is not None:
                mask = torch.tensor(loss_k.mask).to(
                    dtype=y_pred_k.dtype,
                    device=y_pred_k.device
                )
            else:
                mask = None
            if isinstance(transformation, dict):
                transform_k = transformation[k]
            else:
                transform_k = transformation
            try:
                y_k = y[k]
            except KeyError:
                raise DatasetError(f"No targets provided for ouput '{k}'.")
            if y_k.ndim < y_pred_k.ndim:
                y_k = torch.unsqueeze(y_k, 1)
            if transform_k is None:
                y_k_t = y_k
                y_pred_k_t = y_pred_k
            else:
                y_k_t = transform_k(y_k)
                if mask is not None:
                    y_k_t = torch.where(y_k > mask, y_k_t, mask)
                y_pred_k_t = transform_k.invert(y_pred_k)
            if k == "__loss__":
                l = loss_k(y_pred_k, y_k_t)
            else:
                l = loss_k(y_pred_k, y_k_t, k)
            cache = {}
            with torch.no_grad():
                if metrics is not None:
                    for m in metrics:
                        m.process_batch(k, y_pred_k_t, y_k, cache=cache)
            losses[k] = l.item()
            if not avg_loss:
                avg_loss = 0.0
                tot_loss = 0.0
                n_samples = 0
            avg_loss += l
            if mask is not None:
                n = (y_k > mask).sum().item()
            else:
                n = torch.numel(y_k)
            tot_loss += (l * n).item()
            n_samples += n
        return avg_loss, tot_loss, losses, n_samples
def _train_step ( self , x , y , loss , adversarial_training , metrics = None , transformation = None ) : y_pred = self ( x ) if adversarial_training is not None : x . requires_grad = True if not isinstance ( y_pred , dict ) : y_pred = { "__loss__" : y_pred } if not isinstance ( y , dict ) : y = { next ( iter ( y_pred . keys ( ) ) ) : y } if not isinstance ( loss , dict ) : loss = { k : loss for k in y_pred } avg_loss = None tot_loss = None losses = { } for k in y_pred : loss_k = loss [ k ] y_pred_k = y_pred [ k ] if loss_k . mask is not None : mask = torch . tensor ( loss_k . mask ) . to ( dtype = y_pred_k . dtype , device = y_pred_k . device ) else : mask = None if isinstance ( transformation , dict ) : transform_k = transformation [ k ] else : transform_k = transformation try : y_k = y [ k ] except KeyError : raise DatasetError ( f"No targets provided for ouput '{k}'." ) if y_k . ndim < y_pred_k . ndim : y_k = torch . unsqueeze ( y_k , 1 ) if transform_k is None : y_k_t = y_k y_pred_k_t = y_pred_k else : y_k_t = transform_k ( y_k ) if mask is not None : y_k_t = torch . where ( y_k > mask , y_k_t , mask ) y_pred_k_t = transform_k . invert ( y_pred_k ) if k == "__loss__" : l = loss_k ( y_pred_k , y_k_t ) else : l = loss_k ( y_pred_k , y_k_t , k ) cache = { } with torch . no_grad ( ) : if metrics is not None : for m in metrics : m . process_batch ( k , y_pred_k_t , y_k , cache = cache ) losses [ k ] = l . item ( ) if not avg_loss : avg_loss = 0.0 tot_loss = 0.0 n_samples = 0 avg_loss += l if mask is not None : n = ( y_k > mask ) . sum ( ) . item ( ) else : n = torch . numel ( y_k ) tot_loss += ( l * n ) . item ( ) n_samples += n return avg_loss , tot_loss , losses , n_samples
Performs a single training step and returns a dictionary of
losses for every output.
Performs a single training step and returns a dictionary of losses for every output .
"""
        Performs a single training step and returns a dictionary of
        losses for every output.

        Args:
            x: The input data for the current batch.
            y: The output data for the current batch.
            adversarial_training: Scaling factor for adversarial training or
                None if no adversarial training should be performed.

        Returns:
            A single loss or a dictionary of losses in the case of a multi-output
            network.
        """ # Keep track of x gradients if adversarial training # is to be performed. # Make sure both outputs are dicts. # Loop over keys in prediction.
A single loss or a dictionary of losses in the case of a multi-output
network.
A single loss or a dictionary of losses in the case of a multi - output network .
self
x
The input data for the current batch.
The input data for the current batch .
y
The output data for the current batch.
The output data for the current batch .
loss
adversarial_training
Scaling factor for adversarial training or
None if no adversarial training should be performed.
Scaling factor for adversarial training or None if no adversarial training should be performed .
metrics
transformation