Neural Network

class Hyperparameters(pc_exponent: float, n_train: int, hidden_layer_sizes: tuple[int, ...], activation: str, alpha: float, batch_size: int, learning_rate_init: float, tol: float, validation_fraction: float, n_iter_no_change: float, max_iter: int = 1000)[source]

Dataclass containing the parameters which are passed to the neural network for training, as well as a few more (pc_exponent) and (n_train).

Parameters
  • pc_exponent (float) – Exponent to be used in the normalization of the principal components: the network learns to reconstruct \(x_i \lambda_i^\alpha\), where \(x_i\) are the principal-component representation of a waveform, while \(\lambda_i\) are the eigenvalues of the PCA and finally \(\alpha\) is this parameter.

  • n_train (float) – Number of waveforms to use in the training.

  • hidden_layer_sizes (tuple[int, ...]) – Sizes of the layers in the neural network. For more details, refer to the documentation of the MLPRegressor.

  • activation (str) – Activation function. For more details, refer to the documentation of the MLPRegressor.

  • alpha (float) – Regularization parameter. For more details, refer to the documentation of the MLPRegressor.

  • batch_size (int) – For more details, refer to the documentation of the MLPRegressor.

  • learning_rate_init (float) – For more details, refer to the documentation of the MLPRegressor.

  • tol (float) – For more details, refer to the documentation of the MLPRegressor.

  • validation_fraction (float) – For more details, refer to the documentation of the MLPRegressor.

  • n_iter_no_change (float) – For more details, refer to the documentation of the MLPRegressor.

classmethod from_trial(trial: optuna.Trial, n_train_max: int)[source]

Generate the hyperparameter set starting from an optuna.Trial.

Parameters
  • trial (optuna.Trial) – Used to generate the parameters.

  • n_train_max (int) – Upper bound for the attribute n_train.

property nn_params: dict[str, Union[int, float, str, bool, tuple[int, ...]]]

Return a dictionary which can be readily unpacked and used to initialize a MLPRegressor.

class NeuralNetwork(hyper: mlgw_bns.neural_network.Hyperparameters)[source]

Abstract class for a wrapper around a generic neural network.

abstract fit(x_data: numpy.ndarray, y_data: numpy.ndarray) None[source]

Fit the network to the data.

Parameters
  • x_data (np.ndarray) –

  • y_data (np.ndarray) –

abstract classmethod from_file(filename: Union[IO[bytes], str])[source]

Load object from file.

Parameters

filename (str) – File to read

Return type

NeuralNetwork

abstract predict(x_data: numpy.ndarray) numpy.ndarray[source]

Make a prediction for a new set of data.

Parameters

x_data (np.ndarray) –

Return type

np.ndarray

abstract save(filename: str) None[source]

Save self to file.

Parameters

filename (str) – File to save to.

class SklearnNetwork(hyper: mlgw_bns.neural_network.Hyperparameters, nn: Optional[sklearn.neural_network._multilayer_perceptron.MLPRegressor] = None, param_scaler: Optional[sklearn.preprocessing._data.StandardScaler] = None)[source]

Wrapper for a MLPRegressor, the regressor provided by the library scikit-learn.

fit(x_data: numpy.ndarray, y_data: numpy.ndarray) None[source]

Fit the network to the data.

Parameters
  • x_data (np.ndarray) –

  • y_data (np.ndarray) –

classmethod from_file(filename: Union[IO[bytes], str])[source]

Load object from file.

Parameters

filename (str) – File to read

Return type

NeuralNetwork

predict(x_data: numpy.ndarray) numpy.ndarray[source]

Make a prediction for a new set of data.

Parameters

x_data (np.ndarray) –

Return type

np.ndarray

save(filename: str)[source]

Save self to file.

Parameters

filename (str) – File to save to.

retrieve_best_trials_list() list[optuna.trial.FrozenTrial][source]

Read the list of best trials which is provided with the package.

This list’s location can be found at the location defined by TRIALS_FILE; if one wishes to modify it, a new one can be generated with the method save_best_trials_to_file() of the class mlgw_bns.hyperparameter_optimization.HyperparameterOptimization after an optimization job has been run.

The current trials provided are the result of about 30 hours of optimization on a laptop.

Returns

List of the trials in the Pareto front of an optimization.

Return type

list[optuna.trial.FrozenTrial]

best_trial_under_n(best_trials: list[optuna.trial.FrozenTrial], training_number: int) mlgw_bns.neural_network.Hyperparameters[source]

Utility function to retrieve a set of hyperparameters starting from a list of optimization trials. The best trial in terms of accuracy is returned.

Parameters
  • best_trials (list[optuna.trial.FrozenTrial]) – List of trials in the Pareto front of an optimization run. A default value for such a list is the one provided by retrieve_best_trials_list().

  • training_number (int) – Return the best trial obtained while using less than this number of training waveforms.

Returns

Hyperparameters corresponding to the best trial found.

Return type

Hyperparameters