src.superphot_plus.sfd.model.classifier

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

Module Contents

Classes

SuperphotClassifier

The Multi-Layer Perceptron.

class SuperphotClassifier(config: superphot_plus.model.config.ModelConfig)[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, probs_csv_path=PROBS_FILE)[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.

  • probs_csv_path (str, optional) – Where to store the probability results.

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_single_light_curve(obj_name, fits_dir, sampler='dynesty')[source]

Given an object name, return classification probabilities based on the model fit and data.

Parameters:
  • obj_name (str) – Name of the supernova.

  • fits_dir (str) – Where model fit information is stored.

  • sampler (str) – The MCMC sampler to use. Defaults to “dynesty”.

Returns:

The average probability for each SN type across all equally-weighted sets of fit parameters.

Return type:

np.ndarray

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

return_new_classifications(test_csv, fit_dir, save_file, output_dir=None, include_labels=False)[source]

Return new classifications based on model and save probabilities to a CSV file.

Parameters:
  • test_csv (str) – Path to the CSV file containing the test data.

  • fit_dir (str) – Path to the directory containing the fit data.

  • save_file (str) – File to store the new classification outputs.

  • output_dir (str) – Path to the directory to store the classification outputs.

  • include_labels (bool, optional) – If True, labels from the test data are included in the probability saving process. Defaults to False.

save(models_dir)[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