src.superphot_plus.model.mlp

This module implements the Multi-Layer Perceptron (MLP) model for classification.

Module Contents

Classes

SuperphotMLP

The Multi-Layer Perceptron.

class SuperphotMLP(config: superphot_plus.config.SuperphotConfig)[source]

Bases: torch.nn.Module

The Multi-Layer Perceptron.

Parameters:

config (ModelConfig) – The MLP architecture configuration.

forward(x)[source]

Forward pass of the Multi-Layer Perceptron model.

Parameters:

x (torch.Tensor) – The input tensor.

Returns:

A tuple containing the predicted output tensor and the hidden tensor.

Return type:

tuple

train_and_validate(train_data, num_epochs=EPOCHS, rng_seed=None)[source]

Run the MLP initialization and training.

Closely follows the demo https://colab.research.google.com/github/bentrevett/pytorch-image-classification/blob/master/1_mlp.ipynb

Parameters:
  • train_data (TrainData) – The training and validation datasets.

  • num_epochs (int, optional) – The number of epochs. Defaults to EPOCHS.

  • rng_seed (int, optional) – Random state that is seeded. if none, use machine entropy.

Returns:

A tuple containing arrays of metrics for each epoch (training accuracies and losses, validation accuracies and losses).

Return type:

tuple

train_epoch(iterator)[source]

Does one epoch of training for a given torch model.

Parameters:

iterator (torch.utils.DataLoader) – The data iterator.

Returns:

A tuple containing the epoch loss and epoch accuracy.

Return type:

tuple

evaluate_epoch(iterator)[source]

Evaluates the model for the validation set.

Parameters:

iterator (torch.utils.DataLoader) – The data iterator.

Returns:

A tuple containing the epoch loss and epoch accuracy.

Return type:

tuple

evaluate(test_data)[source]

Runs model over a group of test samples.

Parameters:

test_data (TestData) – The data to evaluate the model. Consists of test features, test classes, test names and a list of grouped indices, respectively.

Returns:

A tuple containing the labels, names, predicted labels and maximum probabilities.

Return type:

tuple

get_predictions(iterator)[source]

Given a trained model, returns the test images, test labels, and prediction probabilities across all the test labels.

Parameters:

iterator (torch.utils.DataLoader) – The data iterator.

Returns:

A tuple containing the test images, test labels, sample indices, and prediction probabilities.

Return type:

tuple

get_predictions_from_fit_params(iterator)[source]

Given a trained model, returns the test images, test labels, and prediction probabilities across all the test labels.

Parameters:

iterator (torch.utils.DataLoader) – The data iterator.

Returns:

A tuple containing the test images and prediction probabilities.

Return type:

tuple

classify_from_fit_params(fit_params)[source]

Classify one or multiple light curves solely from the fit parameters used in the classifier. Excludes t0 and, for redshift-exclusive classifier, A. Includes chi-squared value.

Parameters:

fit_params (np.ndarray) – Set of model fit parameters.

Returns:

Probability of each light curve being each SN type. Sums to 1 along each row.

Return type:

np.ndarray

save(models_dir, suffix='')[source]

Stores the trained model and respective configuration.

Parameters:

models_dir (str, optional) – Where to store pretrained models and their configurations.

classmethod create(config)[source]

Creates an MLP instance, optimizer and respective criterion.

Parameters:

config (ModelConfig) – Includes (in order): input_size, output_size, n_neurons, n_hidden. Also includes normalization means and standard deviations.

Returns:

The MLP object.

Return type:

torch.nn.Module

classmethod load(filename, config_filename)[source]

Load a trained MLP for subsequent classification of new objects.

Parameters:
  • filename (str) – The path to the pre-trained model.

  • config_filename (str) – The file that includes the model training configuration.

Returns:

The pre-trained classifier object and the respective model config.

Return type:

tuple