easypheno.model._torch_model

Module Contents

Classes

TorchModel

Parent class based on BaseModel for all PyTorch models to share functionalities.

class easypheno.model._torch_model.TorchModel(task, optuna_trial, encoding=None, n_outputs=1, n_features=None, width_onehot=None, batch_size=None, n_epochs=None, early_stopping_point=None)

Bases: easypheno.model._base_model.BaseModel, abc.ABC

Parent class based on BaseModel for all PyTorch models to share functionalities. See BaseModel for more information.

Attributes

Inherited attributes

See BaseModel.

Additional attributes

  • n_features (int): Number of input features to the model

  • width_onehot (int): Number of input channels in case of onehot encoding

  • batch_size (int): Batch size for batch-based training

  • n_epochs (int): Number of epochs for optimization

  • optimizer (torch.optim.optimizer.Optimizer): optimizer for model fitting

  • loss_fn: loss function for model fitting

  • early_stopping_patience (int): epochs without improvement before early stopping

  • early_stopping_point (int): epoch at which early stopping occured

  • device (torch.device): device to use, e.g. GPU

Parameters
  • task (str) – ML task (regression or classification) depending on target variable

  • optuna_trial (optuna.trial.Trial) – optuna.trial.Trial : trial of optuna for optimization

  • encoding (str) – the encoding to use (standard encoding or user-defined)

  • n_outputs (int) – Number of outputs of the model

  • n_features (int) – Number of input features to the model

  • width_onehot (int) – Number of input channels in case of onehot encoding

  • batch_size (int) – Batch size for batch-based training

  • n_epochs (int) – Number of epochs for optimization

  • early_stopping_point (int) – Stop training at defined epoch

train_val_loop(self, X_train, y_train, X_val, y_val)

Implementation of a train and validation loop for PyTorch models. See BaseModel for more information

Parameters
  • X_train (numpy.array) –

  • y_train (numpy.array) –

  • X_val (numpy.array) –

  • y_val (numpy.array) –

Return type

numpy.array

train_one_epoch(self, train_loader)

Train one epoch

Parameters

train_loader (torch.utils.data.DataLoader) – DataLoader with training data

validate_one_epoch(self, val_loader)

Validate one epoch

Parameters

val_loader (torch.utils.data.DataLoader) – DataLoader with validation data

Returns

loss based on loss-criterion

Return type

float

retrain(self, X_retrain, y_retrain)

Implementation of the retraining for PyTorch models. See BaseModel for more information

Parameters
  • X_retrain (numpy.array) –

  • y_retrain (numpy.array) –

predict(self, X_in)

Implementation of a prediction based on input features for PyTorch models. See BaseModel for more information

Parameters

X_in (numpy.array) –

Return type

numpy.array

get_loss(self, outputs, targets)

Calculate the loss based on the outputs and targets

Parameters
  • outputs (torch.Tensor) – outputs of the model

  • targets (torch.Tensor) – targets of the dataset

Returns

loss

Return type

torch.Tensor

get_dataloader(self, X, y=None, shuffle=True)

Get a Pytorch DataLoader using the specified data and batch size

Parameters
  • X (numpy.array) – feature matrix to use

  • y (numpy.array) – optional target vector to use

  • shuffle (bool) – shuffle parameter for DataLoader

Returns

Pytorch DataLoader

Return type

torch.utils.data.DataLoader

static common_hyperparams()

Add hyperparameters that are common for PyTorch models. Do not need to be included in optimization for every child model. Also See BaseModel for more information

static get_torch_object_for_string(string_to_get)

Get the torch object for a specific string, e.g. when suggesting to optuna as hyperparameter

Parameters

string_to_get (str) – string to retrieve the torch object

Returns

torch object