Core Octopod¶
Core Octopod is made up of a learner and dataloader class designed for multi-task multi-dataset learning.
Multitask Learner¶
-
class
octopod.learner.
MultiInputMultiTaskLearner
(model, train_dataloader, val_dataloader, task_dict, loss_function_dict=None, metric_function_dict=None)¶ Multi Input subclass of MultiTaskLearner class
- Parameters
model (torch.nn.Module) – PyTorch model to use with the Learner
train_dataloader (MultiDatasetLoader) – dataloader for all of the training data
val_dataloader (MultiDatasetLoader) – dataloader for all of the validation data
task_dict (dict) – dictionary with all of the tasks as keys and the number of unique labels as the values
Notes
Multi-input datasets should return x’s as a tuple/list so that each element can be sent to the appropriate device before being sent to the model see octopod.vision.dataset’s OctopodImageDataset class for an example
-
class
octopod.learner.
MultiTaskLearner
(model, train_dataloader, val_dataloader, task_dict, loss_function_dict=None, metric_function_dict=None)¶ Class to encapsulate training and validation steps for a pipeline. Based off the fastai learner.
- Parameters
model (torch.nn.Module) – PyTorch model to use with the Learner
train_dataloader (MultiDatasetLoader) – dataloader for all of the training data. Set of labels must match val_dataloader or a ValueError will be thrown.
val_dataloader (MultiDatasetLoader) – dataloader for all of the validation data. Set of labels must match train_dataloader or a ValueError will be thrown.
task_dict (dict) – dictionary with all of the tasks as keys and the number of unique labels as the values
loss_function_dict (dict) –
dictionary where keys are task names (str) and values specify loss functions. A loss function can be specified using the special string value ‘categorical_cross_entropy’ for a multi-class task or ‘bce_logits’ for a multi-label task. Alternatively, it can be specified using a Callable that takes the predicted values and the target values as positional PyTorch tensor arguments and returns a float.
Take care to ensure that the loss function includes a final activation function if needed – for instance, if your model is being used for classification but does not include a softmax layer then calculating cross-entropy properly requires performing the softmax classification within the loss function (this is handled by nn.CrossEntropyLoss() loss function). For our exisiting model architecture examples we do not apply final layer activation functions. Instead the desired activation functions are applied when needed. So we use ‘categorical_cross_entropy’ and ‘bce_logits’ loss functions which apply softmax and sigmoid activations to their inputs before calculating the loss.
metric_function_dict (dict) –
dictionary where keys are task names and values are metric calculation functions. If the input is a string matching a supported metric function multi_class_acc for multi-class tasks or multi_label_acc for multi-label tasks the loss will be filled in. A user can also input a custom metric function as a function for a given task key.
custom metric functions must take in a y_true and raw_y_pred and output some score and y_preds. y_preds are the raw_y_pred values after an activation function has been applied and score is the output of whatever custom metrics the user wants to calculate for that task.
See learner_utils.loss_utils_config for examples.
-
fit
(num_epochs, scheduler, step_scheduler_on_batch, optimizer, device='cuda:0', best_model=False, smooth_loss_alpha=0.2)¶ Fit the PyTorch model
- Parameters
num_epochs (int) – number of epochs to train
scheduler (torch.optim.lr_scheduler) – PyTorch learning rate scheduler
step_scheduler_on_batch (bool) – flag of whether to step scheduler on batch (if True) or on epoch (if False)
optimizer (torch.optim) – PyTorch optimzer
device (str (defaults to 'cuda:0')) – device to run calculations on
best_model (bool (defaults to False)) – flag to save best model from a single fit training run based on validation loss The default is False, which will keep the final model from the training run. True will keep the best model from the training run instead of the model from the final epoch of the training cycle.
smooth_loss_alpha (float) – Training loss values displayed during fitting and at the end of each epoch are exponentially weighted moving averages over batches. Using an exponentially weighted average over batches is a compromise between reporting the value from the most recent batch, which is highly relevant but noisy, and reporting a simple average over batches, which is more stable but reflects the value of the loss at the beginning of the epoch as much as at the end. smooth_loss_alpha controls how much weight is given to the current batch. It must be in the (0, 1] interval. Higher values are more like reporting only the most recent batch, while lower values are more like giving all batches equal weight, so this value controls the tradeoff between stability and relevance.
-
get_val_preds
(device='cuda:0')¶ Return true labels and predictions for data in self.val_dataloaders
- Parameters
device (str (defaults to 'cuda:0')) – device to run calculations on
- Returns
‘y_true’: numpy array of true labels, shape: (num_rows,) ‘y_pred’: numpy of array of predicted probabilities: shape (num_rows, num_labels)
- Return type
Dictionary with dictionary for each task type
-
validate
(device='cuda:0', pbar=None)¶ Evaluate the model on a validation set
- Parameters
loss_function (function) – function to calculate loss with in model
device (str (defaults to 'cuda:0')) – device to run calculations on
pbar (fast_progress progress bar (defaults to None)) – parent progress bar for all epochs
- Returns
overall_val_loss (float) – overall validation loss for all tasks
val_loss_dict (dict) – dictionary of validation losses for individual tasks
metrics_scores (dict) – scores for individual tasks