NPE

This module contains the functions about using Neural Posterior Estimation for self-calibration.

class npe.GripNPE(wl_scale, func_model, func_args=(), func_kwargs={}, use_cuda=True)

This class is a framework to use the Neural Posterior Estimation technique with GRIP.

The idea is to simulate data and train the neural network to map these data with some parameters. During inference, real data are fed to the neural network to infer the posterior of the parameters.

Parameters

wl_scale1d-array

Wavelength scale.

func_modelfunction or callable

Function formatting the data to be compared to a model (e.g. histogram or power density spectrum).

func_argslist, optional

Arguments used or transmitted by func_model. The default is ().

func_kwargsdict, optional

Dictionary of keywords used or transmitted by func_model. The default is {}.

use_cudabool, optional

Use cuda for training the neural network or inferring, providing CUDA is available. The default is True.

Returns

None.

create_train_set(nb_batch_per_epoch, batch_size, dist_type, dist_params_theta)

Generate data set to train the neural network. It creates several batched data to use in one training epoch.

Parameters

nb_batch_per_epochint

Number of batches to train the neural network during one epoch.

batch_sizeint

Number of simulated data sequence in a batch.

dist_typelist of strings

Define the distribution to choose for each parameter. Available keywords are normal (for normal distribution) or uniform (for uniform distribution).

dist_params_thetaList of tuples

List of parameters to use to generate random values from a distribution defined by dist_type.

Returns

train_datand-array

Dataset to train the neural network of shape (nb_batch_per_epoch, batch_size, data_size).

train_paramsnd-array

Associated parameters that have created train_data of shape (nb_batch_per_epoch, batch_size, number_of_params).

draw_params(dist_type, dist_params_theta)

Generate parameters sets to simulate data. These parameters are the one to be inferred by the NN.

Parameters

dist_typelist of strings

Define the distribution to choose for each parameter. Available keywords are normal (for normal distribution) or uniform (for uniform distribution). If the keyword is not recognised, a uniform distribution is considered.

dist_params_thetaList of tuples

List of parameters to use to generate random values from a distribution defined by dist_type.

Returns

paramsarray-like

Parameters generated.

Examples

>>> dist_type = ['uniform', 'normal', 'uniform']
>>> dist_params_theta = [(0, 0.01), (300, 100), (100, 20)]
>>> draw_params(dist_type, dist_params_theta)
generate_batch(batch_size, dist_type, dist_params_theta)

Generate batch of data to train de neural network and their associated parameters.

Parameters

batch_sizeint

Number of simulated data sequence in a batch.

dist_typelist of strings

Define the distribution to choose for each parameter. Available keywords are normal (for normal distribution) or uniform (for uniform distribution).

dist_params_thetaList of tuples

List of parameters to use to generate random values from a distribution defined by dist_type.

Returns

batched_dataarray

Batched simulated data of shape (batch_size, *data.shape).

batched_thetaarray

Batched parameters to infer of shape (batch_size, number of parameters).

inference_on_data(data_to_inf, nb_sample)

Infer data to deduce the posterior of the parameters with the trained neural network.

Parameters

data_to_infnd-array

Data to infer. They must be in the same shape and format as the simulated data used to train the neural network.

nb_sample: int

Number of samples to generate from the surrogate posteriors of the parameters.

Returns

samplesnd-array

Samples of the inferred parameters, which distributions are their posteriors. Shape is (nb elements, nb parameters).

set_nn(nb_transforms, nb_layers, hidden_features)

Create the normalising flow to infer data. The flow is a masked autoregressive flow.

Parameters

nb_transformsint

Number of transformation in the normalising flow.

nb_layersint

Number of layers in a Transform component.

hidden_featuresint

Number of features out of each layer.

Attributes

self.maf_estimator : neural network estimator flow

simulator(params)

Simulate the data to train the neural network.

Parameters

paramsarray-like

Parameters to infer of the model to fit on the data.

Returns

out1d-array

Modelled data.

train_nn(nb_epoch, show_plot=False, save_fig=False, save_path='cluster.png')

Train the neural network following the Neural Posterior Estimation flow.

Parameters

nb_epochint

Number of epochs to train the neural network.

show_plotbool, optional

Show the learning curve. The default is False.

save_figbool, optional

Save the learning curve. The default is False.

save_pathstring, optional

Path to save the figure. This path must end by the name of the file and its extension. The default is ‘cluster.png’. The figure is saved as a PNG with dpi=150.

Returns

training_loss1d-array

Values of the learning curve. The size of the array is the number of epochs.