Helper Module for Deep Learning.
Core classes.
-
class
pynet.core.Base(optimizer_name='Adam', learning_rate=0.001, loss_name='NLLLoss', metrics=None, use_cuda=False, pretrained=None, resume=False, add_labels=False, **kwargs)[source]¶ Base class for perform Deep Learning training.
-
__init__(optimizer_name='Adam', learning_rate=0.001, loss_name='NLLLoss', metrics=None, use_cuda=False, pretrained=None, resume=False, add_labels=False, **kwargs)[source]¶ Class instantiation.
Observers will be notified, allowed signals are: - ‘before_epoch’ - ‘after_epoch’ - ‘kernel_regularizer’
- Parameters
optimizer_name: str, default ‘Adam’
the name of the optimizer: see ‘torch.optim’ for a description of available optimizer.
learning_rate: float, default 1e-3
the optimizer learning rate.
loss_name: str, default ‘NLLLoss’
the name of the loss: see ‘torch.nn’ for a description of available loss.
metrics: list of str
a list of extra metrics that will be computed.
use_cuda: bool, default False
wether to use GPU or CPU.
pretrained: path, default None
path to the pretrained model or weights.
resume: bool, default False
if set to true, the code will restore the weights of the model but also restore the optimizer’s state, as well as the hyperparameters used, and the scheduler.
add_labels: bool, default False
if set and labels are specified in the data manager, add the labels to the forward function parameters.
kwargs: dict
specify directly a custom ‘model’, ‘optimizer’ or ‘loss’. Can also be used to set specific optimizer parameters.
-
test(loader, with_logit=False, logit_function='softmax', predict=False, concat_layer_outputs=None, is_validation=False)[source]¶ Evaluate the model on the test or validation data.
- Parameters
loader: a pytorch Dataset
the data laoder.
with_logit: bool, default False
apply the logit function to the result.
logit_funtction: str, default ‘softmax’
choose the logit function.
predict: bool, default False
take the argmax over the channels.
concat_layer_outputs: list of str, default None
the outputs of the intermediate layers to be merged with the predicted data (must be the same size).
is_validation: bool default False
specify if we are in the validation phase.
- Returns
y: array-like
the predicted data.
loss: float
the value of the loss function.
values: dict
the values of the metrics.
-
testing(manager, with_logit=False, logit_function='softmax', predict=False, concat_layer_outputs=None)[source]¶ Evaluate the model.
- Parameters
manager: a pynet DataManager
a manager containing the test data.
with_logit: bool, default False
apply the logit function to the result.
logit_function: str, default ‘softmax’
choose the logit function.
predict: bool, default False
take the argmax over the channels.
concat_layer_outputs: list of str, default None
the outputs of the intermediate layers to be merged with the predicted data (must be the same size).
- Returns
y: array-like
the predicted data.
X: array-like
the input data.
y_true: array-like
the true data if available.
loss: float
the value of the loss function if true data availble.
values: dict
the values of the metrics if true data availble.
-
train(loader)[source]¶ Train the model on the trained data.
- Parameters
loader: a pytorch Dataset
the data laoder.
- Returns
loss: float
the value of the loss function.
values: dict
the values of the metrics.
-
training(manager, nb_epochs, checkpointdir=None, fold_index=None, scheduler=None, with_validation=True, save_after_epochs=1, add_labels=False)[source]¶ Train the model.
- Parameters
manager: a pynet DataManager
a manager containing the train and validation data.
nb_epochs: int, default 100
the number of epochs.
checkpointdir: str, default None
a destination folder where intermediate models/histories will be saved.
fold_index: int, default None
the index of the fold to use for the training, default use all the available folds.
scheduler: torch.optim.lr_scheduler, default None
a scheduler used to reduce the learning rate.
with_validation: bool, default True
if set use the validation dataset.
save_after_epochs: int, default 1
determines when the model is saved and represents the number of epochs before saving.
- Returns
train_history, valid_history: History
the train/validation history.
-
Follow us
Inspired by AZMIND template.