Models

class DLL.DeepLearning.Model.Model(input_shape, data_type=torch.float32, device=device(type='cpu'))[source]

Bases: object

The base model for a sequantial deep learning model. Uses a linear stack of layers to do forward- and backpropagation.

Parameters:
  • input_shape (tuple[int] | int) – A tuple or an int containing the input shape of the model. The batch size should not be given as the first member of the tuple. For instance, if the input is of shape (n_sample, n_features), the input_shape should be n_features or if the input is of shape (n_samples, n_channels, width, heigth), the input_shape should be (n_channels, width, heigth).

  • data_type (torch.dtype, optional) – The data type used by the model. Defaults to torch.float32.

  • device (torch.device, optional) – The device of the model. Determines if the computation is made using the gpu or the cpu. Defaults to torch.device(“cpu”).

add(layer)[source]

Adds and initializes a layer to the model.

Parameters:

layer (Core layers) – The layer that is added to the model.

clone()[source]

Returns a copy of the same model.

compile(optimiser=None, loss=None, metrics=('loss',), callbacks=())[source]

Configures the model for training. Sets the optimiser and the loss function.

Parameters:
  • optimiser (Optimisers | None, optional) – The optimiser used for training the model. If None, the ADAM optimiser is used.

  • loss (Losses | None, optional) – The loss function used for training the model. If None, the MSE loss is used.

  • metrics (tuple[str], optional) – The metrics that will be tracked during training. Defaults to (“loss”).

  • callbacks (tuple[Callbacks], optional) – The callbacks used by the model. Defaults to ().

Raises:

TypeError – If the optimiser is not from DLL.DeepLearning.Optimisers, the loss is not from DLL.DeepLearning.Losses or the metrics is not a tuple or a list of strings.

eval()[source]

Change the state of the model to evaluation state. Effects some layers, but other layers remain the same.

fit(X, Y, val_data=None, epochs=10, callback_frequency=1, batch_size=None, shuffle_every_epoch=True, shuffle_data=True, verbose=False)[source]

Fits the LogisticRegression model to the input data by minimizing the cross entropy loss (logistic loss).

Parameters:
  • X (torch.Tensor of shape (n_samples, *first_layer.input_shape)) – The input data, of correct shape determined by the input_shape of the model.

  • y (torch.Tensor) – The targets corresponding to each sample.

  • val_data (tuple[X_val, y_val] | None, optional) – Optional validation samples. Must have the same remaining dimensions than X and y apart from n_samples. If None, no validation data is used. Defaults to None.

  • epochs (int, optional) – The number of training iterations. Must be a positive integer. Defaults to 10.

  • callback_frequency (int, optional) – The number of iterations between printing info from training. Must be a positive integer. Defaults to 1, which means that every iteration, info is printed assuming verbose=True.

  • batch_size (int | None, optional) – The batch size used in training. Must be a positive integer. If None, every sample is used for every gradient calculation. Defaults to None.

  • shuffle_every_epoch (bool, optional) – If True, shuffles the order of the samples every epoch. Defaults to True.

  • shuffle_data (bool, optional) – If True, shuffles data before the training.

  • verbose (bool, optional) – If True, prints info of the chosen metrics during training. Defaults to False.

Returns:

A dictionary tracking the evolution of selected metrics at intervals defined by callback_frequency. If training was not stopped early, each metric is floor(epochs / callback_frequency) long.

Return type:

history (dict[str, list])

Raises:
  • TypeError – If the input matrix or the target matrix is not a PyTorch tensor or if other parameters are of wrong type.

  • ValueError – If the input matrix or the target matrix is not the correct shape or if other parameters have incorrect values.

predict(X)[source]

Applies the fitted Model to the input data, predicting wanted values by forward propagation.

Parameters:

X (torch.Tensor of shape (n_samples, *input_shape)) – The input data that goes through the model by forward propagation.

Raises:

NotCompiledError – If the Model has not been compiled before predicting.

Returns:

The predictions made by the model.

Return type:

torch.Tensor of shape (n_samples, *last_layer.output_shape))

summary()[source]

Prints the summary of the model containing its architecture and the number of parameters of the model.

train()[source]

Change the state of the model to training state. Effects some layers, but other layers remain the same.

Saving and loading models

DLL.DeepLearning.Model.save_model(model, filepath='./model.pkl')[source]

Saves a model using pickle serialization.

DLL.DeepLearning.Model.load_model(filepath='./model.pkl')[source]

Loads a model from a pickle file.