Application#

class deeplay.applications.application.Application(*args, **kwargs)#

Bases: DeeplayModule, LightningModule

Attributes Summary

Methods Summary

__call__(*args, **kwargs)

Call self as a function.

build(*args, **kwargs)

Modifies the current instance of the module in place, finalizing its setup.

clone_metrics(metrics)

compute_loss(y_hat, y)

configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization.

create_data(data[, batch_size, ...])

Create a torch Dataset from data.

create_optimizer_with_params(optimizer, params)

fit(train_data[, val_data, max_epochs, ...])

Train the model on the training data.

forward(*args, **kwargs)

Same as torch.nn.Module.forward().

log(name, value, **kwargs)

Log a key, value pair.

log_metrics(kind, y_hat, y, **logger_kwargs)

metrics_preprocess(y_hat, y)

named_children()

Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

predict_step(batch, batch_idx[, dataloader_idx])

Step function called during predict().

test(data[, metrics, batch_size, reset_metrics])

Test the model on the given data.

test_preprocess(batch)

test_step(batch, batch_idx)

Operates on a single batch of data from the test set.

train_preprocess(batch)

training_step(batch, batch_idx)

Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.

val_preprocess(batch)

validation_step(batch, batch_idx)

Operates on a single batch of data from the validation set.

Attributes Documentation

trainer#

Methods Documentation

__call__(*args, **kwargs)#

Call self as a function.

build(*args, **kwargs)#

Modifies the current instance of the module in place, finalizing its setup.

The build method is essential for completing the initialization of the module. It applies the necessary configurations and adjustments directly to the existing instance. Unlike create, which generates a new module instance, build works on the current module instance. This method is particularly crucial for subclasses of dl.External, as it triggers the instantiation of actual torch layers (like Linear, Sigmoid, ReLU, etc.) within the module. For most other objects, build primarily serves to finalize their configuration.

Note that build is automatically called within the create method, ensuring that newly created instances are fully initialized and ready for use.

Parameters#

*args, **kwargsAny

Example input to the forward method used to

Returns#

DeeplayModule

The current instance of the module after applying all configurations and adjustments.

Example Usage#

Finalizing the setup of a module instance with build: ` module = ExampleModule(a=0) module.configure(a=1) built_module = module.build() # `built_module` is the same instance as `module`, now fully configured and initialized `

static clone_metrics(metrics: T) T#
compute_loss(y_hat, y) Tensor | Dict[str, Tensor]#
configure_optimizers()#

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple. Optimization with multiple optimizers only works in the manual optimization mode.

Return:

Any of these 6 options.

  • Single optimizer.

  • List or Tuple of optimizers.

  • Two lists - The first list has multiple optimizers, and the second has multiple LR schedulers (or multiple lr_scheduler_config).

  • Dictionary, with an "optimizer" key, and (optionally) a "lr_scheduler" key whose value is a single LR scheduler or lr_scheduler_config.

  • None - Fit will run without any optimizer.

The lr_scheduler_config is a dictionary which contains the scheduler and its associated configuration. The default configuration is shown below.

lr_scheduler_config = {
    # REQUIRED: The scheduler instance
    "scheduler": lr_scheduler,
    # The unit of the scheduler's step size, could also be 'step'.
    # 'epoch' updates the scheduler on epoch end whereas 'step'
    # updates it after a optimizer update.
    "interval": "epoch",
    # How many epochs/steps should pass between calls to
    # `scheduler.step()`. 1 corresponds to updating the learning
    # rate after every epoch/step.
    "frequency": 1,
    # Metric to monitor for schedulers like `ReduceLROnPlateau`
    "monitor": "val_loss",
    # If set to `True`, will enforce that the value specified 'monitor'
    # is available when the scheduler is updated, thus stopping
    # training if not found. If set to `False`, it will only produce a warning
    "strict": True,
    # If using the `LearningRateMonitor` callback to monitor the
    # learning rate progress, this keyword can be used to specify
    # a custom logged name
    "name": None,
}

When there are schedulers in which the .step() method is conditioned on a value, such as the torch.optim.lr_scheduler.ReduceLROnPlateau scheduler, Lightning requires that the lr_scheduler_config contains the keyword "monitor" set to the metric name that the scheduler should be conditioned on.

Metrics can be made available to monitor by simply logging it using self.log('metric_to_track', metric_val) in your LightningModule.

Note:

Some things to know:

  • Lightning calls .backward() and .step() automatically in case of automatic optimization.

  • If a learning rate scheduler is specified in configure_optimizers() with key "interval" (default “epoch”) in the scheduler configuration, Lightning will call the scheduler’s .step() method automatically in case of automatic optimization.

  • If you use 16-bit precision (precision=16), Lightning will automatically handle the optimizer.

  • If you use torch.optim.LBFGS, Lightning handles the closure function automatically for you.

  • If you use multiple optimizers, you will have to switch to ‘manual optimization’ mode and step them yourself.

  • If you need to control how often the optimizer steps, override the optimizer_step() hook.

create_data(data, batch_size=32, steps_per_epoch=100, replace=False, **kwargs)#

Create a torch Dataset from data. If data is a Feature, it will create a Dataset with the feature.

create_optimizer_with_params(optimizer, params)#
fit(train_data, val_data=None, max_epochs=None, batch_size=32, steps_per_epoch=100, replace=False, val_batch_size=None, val_steps_per_epoch=10, callbacks=[], **kwargs) LogHistory#

Train the model on the training data.

Train the model on the training data, with optional validation data.

Parameters#

max_epochsint

The maximum number of epochs to train the model.

batch_sizeint

The batch size to use for training.

steps_per_epochint

The number of steps per epoch (used if train_data is a Feature).

replacebool or float

Whether to replace the data after each epoch (used if train_data is a Feature). If a float, the data is replaced with the given probability.

val_batch_sizeint

The batch size to use for validation. If None, the training batch size is used.

val_steps_per_epochint

The number of steps per epoch for validation.

callbackslist

A list of callbacks to use during training.

permute_target_channelsbool or “auto”

Whether to permute the target channels to channel first. If “auto”, the model will attempt to infer the correct permutation based on the input and target shapes.

**kwargs

Additional keyword arguments to pass to the trainer.

forward(*args, **kwargs)#

Same as torch.nn.Module.forward().

Args:

*args: Whatever you decide to pass into the forward method. **kwargs: Keyword arguments are also possible.

Return:

Your model’s output

log(name, value, **kwargs)#

Log a key, value pair.

Example:

self.log('train_loss', loss)

The default behavior per hook is documented here: extensions/logging:Automatic Logging.

Args:

name: key to log. Must be identical across all processes if using DDP or any other distributed strategy. value: value to log. Can be a float, Tensor, or a Metric. prog_bar: if True logs to the progress bar. logger: if True logs to the logger. on_step: if True logs at this step. The default value is determined by the hook.

See extensions/logging:Automatic Logging for details.

on_epoch: if True logs epoch accumulated metrics. The default value is determined by the hook.

See extensions/logging:Automatic Logging for details.

reduce_fx: reduction function over step values for end of epoch. torch.mean() by default. enable_graph: if True, will not auto detach the graph. sync_dist: if True, reduces the metric across devices. Use with care as this may lead to a significant

communication overhead.

sync_dist_group: the DDP group to sync across. add_dataloader_idx: if True, appends the index of the current dataloader to

the name (when using multiple dataloaders). If False, user needs to give unique names for each dataloader to not mix the values.

batch_size: Current batch_size. This will be directly inferred from the loaded batch,

but for some data structures you might need to explicitly provide it.

metric_attribute: To restore the metric state, Lightning requires the reference of the

torchmetrics.Metric in your model. This is found automatically if it is a model attribute.

rank_zero_only: Tells Lightning if you are calling self.log from every process (default) or only from

rank 0. If True, you won’t be able to use this metric as a monitor in callbacks (e.g., early stopping). Warning: Improper use can lead to deadlocks! See Advanced Logging for more details.

log_metrics(kind: Literal['train', 'val', 'test'], y_hat, y, **logger_kwargs)#
metrics_preprocess(y_hat, y) Tuple[Tensor, Tensor]#
named_children() Iterator[Tuple[str, Module]]#

Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

Yields:

(str, Module): Tuple containing a name and child module

Example:

>>> # xdoctest: +SKIP("undefined vars")
>>> for name, module in model.named_children():
>>>     if name in ['conv4', 'conv5']:
>>>         print(module)
predict_step(batch, batch_idx, dataloader_idx=None)#

Step function called during predict(). By default, it calls forward(). Override to add any processing logic.

The predict_step() is used to scale inference on multi-devices.

To prevent an OOM error, it is possible to use BasePredictionWriter callback to write the predictions to disk or database after each batch or on epoch end.

The BasePredictionWriter should be used while using a spawn based accelerator. This happens for Trainer(strategy="ddp_spawn") or training on 8 TPU cores with Trainer(accelerator="tpu", devices=8) as predictions won’t be returned.

Args:

batch: The output of your data iterable, normally a DataLoader. batch_idx: The index of this batch. dataloader_idx: The index of the dataloader that produced this batch.

(only if multiple dataloaders used)

Return:

Predicted output (optional).

Example

class MyModel(LightningModule):

    def predict_step(self, batch, batch_idx, dataloader_idx=0):
        return self(batch)

dm = ...
model = MyModel()
trainer = Trainer(accelerator="gpu", devices=2)
predictions = trainer.predict(model, dm)
test(data, metrics: Metric | Tuple[str, Metric] | Sequence[Metric | Tuple[str, Metric]] | Dict[str, Metric] | None = None, batch_size: int = 32, reset_metrics: bool = True)#

Test the model on the given data.

Test the model on the given data, using the given metrics. Metrics can be given as a single metric, a tuple of name and metric, a sequence of metrics (or tuples of name and metric) or a dictionary of metrics. In the case of tuples, the name is used as the key in the returned dictionary. In the case of metrics, the name of the metric is used as the key in the returned dictionary.

Parameters#

datadata-like

The data to test the model on. Can be a Feature, a torch.utils.data.Dataset, a tuple of tensors, a tensor or a numpy array.

metricsmetric-like

The metrics to use for testing. Can be a single metric, a tuple of name and metric, a sequence of metrics (or tuples of name and metric) or a dictionary of metrics.

batch_sizeint

The batch size to use for testing.

test_preprocess(batch)#
test_step(batch, batch_idx)#

Operates on a single batch of data from the test set. In this step you’d normally generate examples or calculate anything of interest such as accuracy.

Args:

batch: The output of your data iterable, normally a DataLoader. batch_idx: The index of this batch. dataloader_idx: The index of the dataloader that produced this batch.

(only if multiple dataloaders used)

Return:
  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one test dataloader:
def test_step(self, batch, batch_idx): ...


# if you have multiple test dataloaders:
def test_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single test dataset
def test_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'test_loss': loss, 'test_acc': test_acc})

If you pass in multiple test dataloaders, test_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple test dataloaders
def test_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    ...
Note:

If you don’t need to test you don’t need to implement this method.

Note:

When the test_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of the test epoch, the model goes back to training mode and gradients are enabled.

train_preprocess(batch)#
training_step(batch, batch_idx)#

Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.

Args:

batch: The output of your data iterable, normally a DataLoader. batch_idx: The index of this batch. dataloader_idx: The index of the dataloader that produced this batch.

(only if multiple dataloaders used)

Return:
  • Tensor - The loss tensor

  • dict - A dictionary which can include any keys, but must include the key 'loss' in the case of automatic optimization.

  • None - In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.

In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.

Example:

def training_step(self, batch, batch_idx):
    x, y, z = batch
    out = self.encoder(x)
    loss = self.loss(out, x)
    return loss

To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:

def __init__(self):
    super().__init__()
    self.automatic_optimization = False


# Multiple optimizers (e.g.: GANs)
def training_step(self, batch, batch_idx):
    opt1, opt2 = self.optimizers()

    # do training_step with encoder
    ...
    opt1.step()
    # do training_step with decoder
    ...
    opt2.step()
Note:

When accumulate_grad_batches > 1, the loss returned here will be automatically normalized by accumulate_grad_batches internally.

val_preprocess(batch)#
validation_step(batch, batch_idx)#

Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.

Args:

batch: The output of your data iterable, normally a DataLoader. batch_idx: The index of this batch. dataloader_idx: The index of the dataloader that produced this batch.

(only if multiple dataloaders used)

Return:
  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one val dataloader:
def validation_step(self, batch, batch_idx): ...


# if you have multiple val dataloaders:
def validation_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single validation dataset
def validation_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'val_loss': loss, 'val_acc': val_acc})

If you pass in multiple val dataloaders, validation_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple validation dataloaders
def validation_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    ...
Note:

If you don’t need to validate you don’t need to implement this method.

Note:

When the validation_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.