quantnn/models/pytorch/common.py
def train(
        self,
        training_data,
        validation_data=None,
        loss=None,
        optimizer=None,
        scheduler="default",
        n_epochs=None,
        adversarial_training=None,
        batch_size=None,
        device="cpu",
        logger=None,
        metrics=None,
        keys=None,
        transformation=None,
    ):
        """
        Train the network.

        This trains the network for the given number of epochs using the
        provided training and validation data.

        If desired, the training can be augmented using adversarial training.
        In this case the network is additionally trained with an adversarial
        batch of examples in each step of the training.

        Arguments:
            training_data: pytorch dataloader providing the training data
            validation_data: pytorch dataloader providing the validation data
            n_epochs: the number of epochs to train the network for
            adversarial_training: whether or not to use adversarial training
            eps_adv: The scaling factor to use for adversarial training.
        """
        # Avoid nameclash with PyTorch train method.
        if type(training_data) == bool:
            return nn.Module.train(self, training_data)

        if logger is None:
            logger = TrainingLogger(n_epochs)

        # Determine device to use
        if torch.cuda.is_available() and device in ["gpu", "cuda"]:
            device = torch.device("cuda")
        elif device == "cpu":
            device = torch.device("cpu")
        else:
            device = torch.device(device)

        # Handle input data
        try:
            x, y = handle_input(training_data, device)
            training_data = BatchedDataset((x, y), batch_size=batch_size)
        except:
            pass

        try:
            x, y = handle_input(validation_data, device)
            validation_data = BatchedDataset((x, y), batch_size=batch_size)
        except Exception:
            pass

        # Optimizer
        if not optimizer:
            optimizer = _get_default_optimizer(self)
        self.optimizer = optimizer

        # Training scheduler
        if scheduler == "default":
            scheduler = _get_default_scheduler(optimizer)
        if scheduler is not None:
            scheduler_sig = signature(scheduler.step)
        else:
            scheduler_sig = None

        self.to(device)

        if isinstance(loss, dict):
            {k: loss[k].to(device) for k in loss}
        else:
            loss.to(device)

        # metrics
        if metrics is None:
            metrics = []

        training_losses = []
        validation_losses = []

        state = {}
        for m in self.modules():
            state[m] = m.training

        # Training loop
        with logger:
            for i in range(n_epochs):

                for m in metrics:
                    m.reset()

                epoch_error = 0.0
                n = 0

                logger.epoch_begin(self)

                for j, data in enumerate(training_data):

                    self.optimizer.zero_grad(**_ZERO_GRAD_ARGS)

                    x, y = _get_x_y(data, keys)

                    if not isinstance(x, torch.Tensor):
                        if isinstance(x, Iterable):
                            x = [x_i.float().to(device) for x_i in x]
                        else:
                            raise ValueError(
                                "Batch input 'x' should be a torch.Tensor or"
                                " an Iterable of tensors."
                            )
                    else:
                        x = x.float().to(device)

                    if isinstance(y, dict):
                        for k in y:
                            y[k] = y[k].to(device)
                    else:
                        y = y.to(device)
                    if adversarial_training is not None:
                        x.requires_grad = True

                    avg_loss, tot_loss, losses, n_samples = self._train_step(
                        x, y, loss, adversarial_training,
                        transformation=transformation
                    )
                    avg_loss.backward()
                    optimizer.step()

                    # Log training step.
                    if hasattr(training_data, "__len__"):
                        of = len(training_data)
                    else:
                        of = None
                    if n_samples > 0:
                        tot_loss /= n_samples
                    logger.training_step(
                        tot_loss, n_samples, of=of, losses=losses
                    )

                    # Track epoch error.
                    n += get_batch_size(x)
                    epoch_error += tot_loss * n

                    # Perform adversarial training step if required.
                    if adversarial_training is not None and x.requires_grad:
                        x_adv = self._make_adversarial_samples(x, adversarial_training)
                        self.optimizer.zero_grad(**_ZERO_GRAD_ARGS)
                        tot_loss, _ = self._train_step(
                            x_adv,
                            y,
                            loss,
                            adversarial_training,
                            transformation=transformation,
                        )
                        tot_loss.backward()
                        optimizer.step()

                # Save training error
                training_losses.append(epoch_error / n)

                lr = [group["lr"] for group in self.optimizer.param_groups][0]

                errors = {}

                if validation_data is not None:
                    n = 0
                    epoch_error = 0
                    self.eval()
                    with torch.no_grad():
                        for j, data in enumerate(validation_data):

                            self.optimizer.zero_grad(**_ZERO_GRAD_ARGS)

                            x, y = _get_x_y(data, keys)
                            if not isinstance(x, torch.Tensor):
                                if isinstance(x, Iterable):
                                    x = [x_i.float().to(device) for x_i in x]
                                else:
                                    raise ValueError(
                                        "Batch input 'x' should be a torch.Tensor or"
                                        " an Iterable of tensors."
                                    )
                            else:
                                x = x.float().to(device)
                            if isinstance(y, dict):
                                for k in y:
                                    y[k] = y[k].to(device)
                            else:
                                y = y.to(device)

                            avg_loss, tot_loss, losses, n_samples = self._train_step(
                                x,
                                y,
                                loss,
                                None,
                                metrics=metrics,
                                transformation=transformation,
                            )

                            # Log validation step.
                            if hasattr(validation_data, "__len__"):
                                of = len(validation_data)
                            else:
                                of = None
                            if n_samples > 0:
                                tot_loss /= n_samples

                            logger.validation_step(
                                tot_loss, n_samples, of=of, losses=losses
                            )

                            # Update running validation errors.
                            n += get_batch_size(x)
                            epoch_error = tot_loss * n

                        validation_losses.append(epoch_error / n)

                        for m in self.modules():
                            m.training = state[m]

                # Finally update scheduler.
                if scheduler:
                    if len(scheduler_sig.parameters) == 1:
                        scheduler.step()
                    else:
                        scheduler.step(epoch_error)

                logger.epoch(learning_rate=lr, metrics=metrics)
        logger.training_end()

        self.eval()

        Train the network.

        This trains the network for the given number of epochs using the
        provided training and validation data.

        If desired, the training can be augmented using adversarial training.
        In this case the network is additionally trained with an adversarial
        batch of examples in each step of the training.

        Arguments:
            training_data: pytorch dataloader providing the training data
            validation_data: pytorch dataloader providing the validation data
            n_epochs: the number of epochs to train the network for
            adversarial_training: whether or not to use adversarial training
            eps_adv: The scaling factor to use for adversarial training.
        
Train the network.
This trains the network for the given number of epochs using the
provided training and validation data.

If desired, the training can be augmented using adversarial training.
In this case the network is additionally trained with an adversarial
batch of examples in each step of the training.
Train the network . This trains the network for the given number of epochs using the provided training and validation data . If desired the training can be augmented using adversarial training . In this case the network is additionally trained with an adversarial batch of examples in each step of the training .
def train(
        self,
        training_data,
        validation_data=None,
        loss=None,
        optimizer=None,
        scheduler="default",
        n_epochs=None,
        adversarial_training=None,
        batch_size=None,
        device="cpu",
        logger=None,
        metrics=None,
        keys=None,
        transformation=None,
    ):
        if type(training_data) == bool:
            return nn.Module.train(self, training_data)
        if logger is None:
            logger = TrainingLogger(n_epochs)
        if torch.cuda.is_available() and device in ["gpu", "cuda"]:
            device = torch.device("cuda")
        elif device == "cpu":
            device = torch.device("cpu")
        else:
            device = torch.device(device)
        try:
            x, y = handle_input(training_data, device)
            training_data = BatchedDataset((x, y), batch_size=batch_size)
        except:
            pass
        try:
            x, y = handle_input(validation_data, device)
            validation_data = BatchedDataset((x, y), batch_size=batch_size)
        except Exception:
            pass
        if not optimizer:
            optimizer = _get_default_optimizer(self)
        self.optimizer = optimizer
        if scheduler == "default":
            scheduler = _get_default_scheduler(optimizer)
        if scheduler is not None:
            scheduler_sig = signature(scheduler.step)
        else:
            scheduler_sig = None
        self.to(device)
        if isinstance(loss, dict):
            {k: loss[k].to(device) for k in loss}
        else:
            loss.to(device)
        if metrics is None:
            metrics = []
        training_losses = []
        validation_losses = []
        state = {}
        for m in self.modules():
            state[m] = m.training
        with logger:
            for i in range(n_epochs):
                for m in metrics:
                    m.reset()
                epoch_error = 0.0
                n = 0
                logger.epoch_begin(self)
                for j, data in enumerate(training_data):
                    self.optimizer.zero_grad(**_ZERO_GRAD_ARGS)
                    x, y = _get_x_y(data, keys)
                    if not isinstance(x, torch.Tensor):
                        if isinstance(x, Iterable):
                            x = [x_i.float().to(device) for x_i in x]
                        else:
                            raise ValueError(
                                "Batch input 'x' should be a torch.Tensor or"
                                " an Iterable of tensors."
                            )
                    else:
                        x = x.float().to(device)
                    if isinstance(y, dict):
                        for k in y:
                            y[k] = y[k].to(device)
                    else:
                        y = y.to(device)
                    if adversarial_training is not None:
                        x.requires_grad = True
                    avg_loss, tot_loss, losses, n_samples = self._train_step(
                        x, y, loss, adversarial_training,
                        transformation=transformation
                    )
                    avg_loss.backward()
                    optimizer.step()
                    if hasattr(training_data, "__len__"):
                        of = len(training_data)
                    else:
                        of = None
                    if n_samples > 0:
                        tot_loss /= n_samples
                    logger.training_step(
                        tot_loss, n_samples, of=of, losses=losses
                    )
                    n += get_batch_size(x)
                    epoch_error += tot_loss * n
                    if adversarial_training is not None and x.requires_grad:
                        x_adv = self._make_adversarial_samples(x, adversarial_training)
                        self.optimizer.zero_grad(**_ZERO_GRAD_ARGS)
                        tot_loss, _ = self._train_step(
                            x_adv,
                            y,
                            loss,
                            adversarial_training,
                            transformation=transformation,
                        )
                        tot_loss.backward()
                        optimizer.step()
                training_losses.append(epoch_error / n)
                lr = [group["lr"] for group in self.optimizer.param_groups][0]
                errors = {}
                if validation_data is not None:
                    n = 0
                    epoch_error = 0
                    self.eval()
                    with torch.no_grad():
                        for j, data in enumerate(validation_data):
                            self.optimizer.zero_grad(**_ZERO_GRAD_ARGS)
                            x, y = _get_x_y(data, keys)
                            if not isinstance(x, torch.Tensor):
                                if isinstance(x, Iterable):
                                    x = [x_i.float().to(device) for x_i in x]
                                else:
                                    raise ValueError(
                                        "Batch input 'x' should be a torch.Tensor or"
                                        " an Iterable of tensors."
                                    )
                            else:
                                x = x.float().to(device)
                            if isinstance(y, dict):
                                for k in y:
                                    y[k] = y[k].to(device)
                            else:
                                y = y.to(device)
                            avg_loss, tot_loss, losses, n_samples = self._train_step(
                                x,
                                y,
                                loss,
                                None,
                                metrics=metrics,
                                transformation=transformation,
                            )
                            if hasattr(validation_data, "__len__"):
                                of = len(validation_data)
                            else:
                                of = None
                            if n_samples > 0:
                                tot_loss /= n_samples
                            logger.validation_step(
                                tot_loss, n_samples, of=of, losses=losses
                            )
                            n += get_batch_size(x)
                            epoch_error = tot_loss * n
                        validation_losses.append(epoch_error / n)
                        for m in self.modules():
                            m.training = state[m]
                if scheduler:
                    if len(scheduler_sig.parameters) == 1:
                        scheduler.step()
                    else:
                        scheduler.step(epoch_error)
                logger.epoch(learning_rate=lr, metrics=metrics)
        logger.training_end()
        self.eval()
def train ( self , training_data , validation_data = None , loss = None , optimizer = None , scheduler = "default" , n_epochs = None , adversarial_training = None , batch_size = None , device = "cpu" , logger = None , metrics = None , keys = None , transformation = None , ) : if type ( training_data ) == bool : return nn . Module . train ( self , training_data ) if logger is None : logger = TrainingLogger ( n_epochs ) if torch . cuda . is_available ( ) and device in [ "gpu" , "cuda" ] : device = torch . device ( "cuda" ) elif device == "cpu" : device = torch . device ( "cpu" ) else : device = torch . device ( device ) try : x , y = handle_input ( training_data , device ) training_data = BatchedDataset ( ( x , y ) , batch_size = batch_size ) except : pass try : x , y = handle_input ( validation_data , device ) validation_data = BatchedDataset ( ( x , y ) , batch_size = batch_size ) except Exception : pass if not optimizer : optimizer = _get_default_optimizer ( self ) self . optimizer = optimizer if scheduler == "default" : scheduler = _get_default_scheduler ( optimizer ) if scheduler is not None : scheduler_sig = signature ( scheduler . step ) else : scheduler_sig = None self . to ( device ) if isinstance ( loss , dict ) : { k : loss [ k ] . to ( device ) for k in loss } else : loss . to ( device ) if metrics is None : metrics = [ ] training_losses = [ ] validation_losses = [ ] state = { } for m in self . modules ( ) : state [ m ] = m . training with logger : for i in range ( n_epochs ) : for m in metrics : m . reset ( ) epoch_error = 0.0 n = 0 logger . epoch_begin ( self ) for j , data in enumerate ( training_data ) : self . optimizer . zero_grad ( ** _ZERO_GRAD_ARGS ) x , y = _get_x_y ( data , keys ) if not isinstance ( x , torch . Tensor ) : if isinstance ( x , Iterable ) : x = [ x_i . float ( ) . to ( device ) for x_i in x ] else : raise ValueError ( "Batch input 'x' should be a torch.Tensor or" " an Iterable of tensors." ) else : x = x . float ( ) . to ( device ) if isinstance ( y , dict ) : for k in y : y [ k ] = y [ k ] . to ( device ) else : y = y . to ( device ) if adversarial_training is not None : x . requires_grad = True avg_loss , tot_loss , losses , n_samples = self . _train_step ( x , y , loss , adversarial_training , transformation = transformation ) avg_loss . backward ( ) optimizer . step ( ) if hasattr ( training_data , "__len__" ) : of = len ( training_data ) else : of = None if n_samples > 0 : tot_loss /= n_samples logger . training_step ( tot_loss , n_samples , of = of , losses = losses ) n += get_batch_size ( x ) epoch_error += tot_loss * n if adversarial_training is not None and x . requires_grad : x_adv = self . _make_adversarial_samples ( x , adversarial_training ) self . optimizer . zero_grad ( ** _ZERO_GRAD_ARGS ) tot_loss , _ = self . _train_step ( x_adv , y , loss , adversarial_training , transformation = transformation , ) tot_loss . backward ( ) optimizer . step ( ) training_losses . append ( epoch_error / n ) lr = [ group [ "lr" ] for group in self . optimizer . param_groups ] [ 0 ] errors = { } if validation_data is not None : n = 0 epoch_error = 0 self . eval ( ) with torch . no_grad ( ) : for j , data in enumerate ( validation_data ) : self . optimizer . zero_grad ( ** _ZERO_GRAD_ARGS ) x , y = _get_x_y ( data , keys ) if not isinstance ( x , torch . Tensor ) : if isinstance ( x , Iterable ) : x = [ x_i . float ( ) . to ( device ) for x_i in x ] else : raise ValueError ( "Batch input 'x' should be a torch.Tensor or" " an Iterable of tensors." ) else : x = x . float ( ) . to ( device ) if isinstance ( y , dict ) : for k in y : y [ k ] = y [ k ] . to ( device ) else : y = y . to ( device ) avg_loss , tot_loss , losses , n_samples = self . _train_step ( x , y , loss , None , metrics = metrics , transformation = transformation , ) if hasattr ( validation_data , "__len__" ) : of = len ( validation_data ) else : of = None if n_samples > 0 : tot_loss /= n_samples logger . validation_step ( tot_loss , n_samples , of = of , losses = losses ) n += get_batch_size ( x ) epoch_error = tot_loss * n validation_losses . append ( epoch_error / n ) for m in self . modules ( ) : m . training = state [ m ] if scheduler : if len ( scheduler_sig . parameters ) == 1 : scheduler . step ( ) else : scheduler . step ( epoch_error ) logger . epoch ( learning_rate = lr , metrics = metrics ) logger . training_end ( ) self . eval ( )
Train the network.
Train the network .
"""
        Train the network.

        This trains the network for the given number of epochs using the
        provided training and validation data.

        If desired, the training can be augmented using adversarial training.
        In this case the network is additionally trained with an adversarial
        batch of examples in each step of the training.

        Arguments:
            training_data: pytorch dataloader providing the training data
            validation_data: pytorch dataloader providing the validation data
            n_epochs: the number of epochs to train the network for
            adversarial_training: whether or not to use adversarial training
            eps_adv: The scaling factor to use for adversarial training.
        """ # Avoid nameclash with PyTorch train method. # Determine device to use # Handle input data # Optimizer # Training scheduler # metrics # Training loop # Log training step. # Track epoch error. # Perform adversarial training step if required. # Save training error # Log validation step. # Update running validation errors. # Finally update scheduler.
self
training_data
pytorch dataloader providing the training data
pytorch dataloader providing the training data
validation_data
pytorch dataloader providing the validation data
pytorch dataloader providing the validation data
loss
optimizer
scheduler
n_epochs
the number of epochs to train the network for
the number of epochs to train the network for
adversarial_training
whether or not to use adversarial training
whether or not to use adversarial training
batch_size
device
logger
metrics
keys
transformation
eps_adv
The scaling factor to use for adversarial training.
The scaling factor to use for adversarial training .