NDEs¶
colfi.element¶
- class colfi.element.ELU_1[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.element.Sigmoid_1[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.element.Softplus_1[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.element.Softplus_2[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- colfi.element.activation(activation_name='RReLU')[source]¶
Activation functions.
- Parameters:
activation_name (str, optional) – The name of activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’. Default: ‘RReLU’
- Returns:
Activation function.
- Return type:
colfi.nodeframe¶
- colfi.nodeframe.decreasingNode(node_in=1970, node_out=5, hidden_layer=3, get_allNode=True)[source]¶
A network structure that the number of neurons in each hidden layer is decreased proportionally.
- Parameters:
node_in (int) – The number of nodes in the input layer.
node_out (int) – The number of nodes in the output layer.
hidden_layer (int) – The number of the hidden layers.
get_allNode (bool) – If True, return the number of nodes of all layers, otherwise, only return the number of nodes of hidden layers. Default: True
- Returns:
A list that contains the number of nodes in each layer.
- Return type:
colfi.sequence¶
- class colfi.sequence.Activation[source]¶
Bases:
objectActivation functions, to be used by class
LinearSeq.
- class colfi.sequence.BatchNorm[source]¶
Bases:
objectBatch Normalization, to be used by class
LinearSeq.
- class colfi.sequence.LinearSeq(nodes, mainBN=True, finalBN=False, mainActive='rrelu', finalActive='None', mainDropout='None', finalDropout='None')[source]¶
Bases:
SeqName,BatchNorm,Activation,DropoutSequence of Linear.
- Parameters:
nodes (list) – A list that contains the number of nodes in each layer.
mainBN (bool, optional) – If True, the network will contain batch normalization layer in its main part. Default: True
finalBN (bool, optional) – If True, the network will contain batch normalization layer in its last layer. Default: False
mainActive (str, optional) – The activation function used in the main part of the network. See
activation()for the available activation functions. Default: ‘rrelu’finalActive (str, optional) – The activation function used in the last layer of the network. See
activation()for the available activation functions. Default: ‘None’mainDropout (str, optional) – The dropout used in the main part of the network, ‘None’ or ‘dropout’. Default: ‘None’
finalDropout (str, optional) – The dropout used in the final layer of the network, ‘None’ or ‘dropout’. Default: ‘None’
colfi.fcnet_ann¶
- class colfi.fcnet_ann.FcNet(node_in=2000, node_out=6, hidden_layer=3, nodes=None, activation_func='RReLU')[source]¶
Bases:
ModuleGet a fully connected network.
- Parameters:
node_in (int) – The number of the input nodes.
node_out (int) – The number of the output nodes.
hidden_layer (int) – The number of the hidden layers.
nodes (None or list, optional) – If list, it should be a collection of nodes of the network, e.g. [node_in, node_hidden1, node_hidden2, …, node_out]
activation_func (str, optional) – Activation function. See
activation(). Default: ‘RReLU’
- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_ann.MultiBranchFcNet(nodes_in=[100, 100, 20], node_out=6, branch_hiddenLayer=1, trunk_hiddenLayer=3, nodes_all=None, activation_func='RReLU')[source]¶
Bases:
ModuleGet a multibranch network.
- Parameters:
nodes_in (list) – The number of the input nodes for each branch. e.g. [node_in_branch1, node_in_branch2, …]
node_out (int) – The number of the output nodes.
branch_hiddenLayer (int) – The number of the hidden layers for the branch part.
trunk_hiddenLayer (int) – The number of the hidden layers for the trunk part.
nodes_all (list, optional) – The number of nodes of the multibranch network. e.g. [nodes_branch1, nodes_branch2, …, nodes_trunk]
activation_func (str, optional) – Activation function. See
activation(). Default: ‘RReLU’
- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_ann.MultiBranchFcNet_MC(node_in=6, nodes_out=[100, 100, 20], trunk_hiddenLayer=3, branch_hiddenLayer=1, nodes_all=None, activation_func='RReLU')[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_ann.MultiBranchFcNet_test(nodes_in=[100, 100, 20], node_out=6, branch_hiddenLayer=1, trunk_hiddenLayer=3, nodes_all=None, activation_func='RReLU')[source]¶
Bases:
ModuleGet a multibranch network.
- Parameters:
nodes_in (list) – The number of the input nodes for each branch. e.g. [node_in_branch1, node_in_branch2, …]
node_out (int) – The number of the output nodes.
branch_hiddenLayer (int) – The number of the hidden layers for the branch part.
trunk_hiddenLayer (int) – The number of the hidden layers for the trunk part.
nodes_all (list, optional) – The number of nodes of the multibranch network. e.g. [nodes_branch1, nodes_branch2, …, nodes_trunk]
activation_func (str, optional) – Activation function. See
activation(). Default: ‘RReLU’
- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
colfi.fcnet_mdn¶
- class colfi.fcnet_mdn.BetaMDN(node_in=100, node_out=1, hidden_layer=3, comp_n=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.GaussianMDN(node_in=100, node_out=1, hidden_layer=3, comp_n=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.MultiBranchBetaMDN(nodes_in=[100, 100, 100], node_out=2, branch_hiddenLayer=1, trunk_hiddenLayer=1, comp_n=3, nodes_all=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.MultiBranchGaussianMDN(nodes_in=[100, 100, 100], node_out=2, branch_hiddenLayer=1, trunk_hiddenLayer=1, comp_n=3, nodes_all=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.MultiBranchMultivariateGaussianMDN(nodes_in=[100, 100, 100], node_out=2, branch_hiddenLayer=1, trunk_hiddenLayer=1, comp_n=3, nodes_all=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.MultivariateGaussianMDN(node_in=100, node_out=2, hidden_layer=3, comp_n=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.MultivariateGaussianMDN_AvgMu(node_in=100, node_out=2, hidden_layer=3, comp_n=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
ModuleThe difference between this class and MultivariateGaussianMDN_AvgMultiNoise is that only the mu and w of the mixture model are averaged.
- forward(x, multi_noise=1)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_mdn.MultivariateGaussianMDN_AvgMultiNoise(node_in=100, node_out=2, hidden_layer=3, comp_n=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
ModuleThe difference between this class and MultivariateGaussianMDN is the forward function, where outputs caused by multiple noises are averaged.
- forward(x, multi_noise=1)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- colfi.fcnet_mdn.beta_PDF(alpha, beta, target, log=True)[source]¶
https://zh.wikipedia.org/wiki/%CE%92%E5%88%86%E5%B8%83
return: x^(alpha-1) (1-x)^(beta-1) Gamma(alpha+beta) / Gamma(alpha)/Gamma(beta), where Gamma is the Gamma function
Note: target > 0 & 1-target > 0, so, 0 < target < 1, so, should use minmax normalization
- colfi.fcnet_mdn.gaussian_PDF(mu, sigma, target, log=True)[source]¶
https://en.wikipedia.org/wiki/Normal_distribution
return:
- colfi.fcnet_mdn.gaussian_loss(omega, mu, sigma, target, logsumexp=True)[source]¶
torch.logsumexpwill help us to avoid a lot of numerical instabilities in the training process, see: https://deep-and-shallow.com/2021/03/20/mixture-density-networks-probabilistic-regression-for-uncertainty-estimation/Note: It is better to set logsumexp=True
- colfi.fcnet_mdn.gaussian_sampler(omega, mu, sigma, chain_leng=10000)[source]¶
Draw samples from a MoG.
- colfi.fcnet_mdn.multivariateGaussian_loss(omega, mu, cholesky_factor, target, logsumexp=True)[source]¶
Calculates the error, given the MoG parameters and the target
The loss is the negative log likelihood of the data given the MoG parameters.
torch.logsumexpwill help us to avoid a lot of numerical instabilities in training see: https://deep-and-shallow.com/2021/03/20/mixture-density-networks-probabilistic-regression-for-uncertainty-estimation/
colfi.fcnet_g¶
- class colfi.fcnet_g.MLPGaussian(node_in=100, node_out=1, hidden_layer=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_g.MLPMultivariateGaussian(node_in=100, node_out=2, hidden_layer=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_g.MLPMultivariateGaussian_AvgMultiNoise(node_in=100, node_out=2, hidden_layer=3, nodes=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x, multi_noise=1)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_g.MultiBranchMLPGaussian(nodes_in=[100, 100, 100], node_out=2, branch_hiddenLayer=1, trunk_hiddenLayer=1, nodes_all=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class colfi.fcnet_g.MultiBranchMLPMultivariateGaussian(nodes_in=[100, 100, 100], node_out=2, branch_hiddenLayer=1, trunk_hiddenLayer=1, nodes_all=None, activation_func='Softplus')[source]¶
Bases:
Module- forward(x_all)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- colfi.fcnet_g.gaussian_loss(params, sigma, target)[source]¶
https://en.wikipedia.org/wiki/Normal_distribution
return:
colfi.models_ann¶
- class colfi.models_ann.Loader(path='ann', randn_num=0.123)[source]¶
Bases:
objectLoad the saved networks.
- class colfi.models_ann.MultiBranchMLP(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, branch_hiddenLayer=1, trunk_hiddenLayer=2, activation_func='Softplus', loss_name='L1', noise_type='multiNormal', factor_sigma=0.2, multi_noise=5)[source]¶
Bases:
OneBranchMLPPredict cosmological parameters with multibranch network for multiple sets of datasets.
- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. The validation set can also be set to None. i.e. [observations, parameters] or [None, None]. Default: [None, None]
obs_errors (None or list, optional) – Observational errors, it is a list of errors with shape [(obs_length_1,), (obs_length_2,), …]. If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or list, optional) – A list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonebranch_hiddenLayer (int, optional) – The number of the hidden layer for the branch part of the network. Default: 1
trunk_hiddenLayer (int, optional) – The number of the hidden layer for the trunk part of the network. Default: 2
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’loss_name (str, optional) – The name of loss function used in the network, which can be ‘L1’, ‘MSE’, or ‘SmoothL1’. See
loss_funcs(). Default: ‘L1’noise_type (str, optional) – The type of Gaussian noise added to the training set, which can be ‘singleNormal’ or ‘multiNormal’. Default: ‘multiNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. For the case ofnoise_type= ‘multiNormal’, it is the standard deviation of the coefficient of the observational error (standard deviation). Default: 0.2multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_branch (float, optional) – The learning rate setting of the branch part. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
epoch_branch (int, optional) – The number of epoch for the branch part. This only works when training the branch part. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: True
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘ANN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
Note
It is suggested to set lr and lr_branch the same value.
- train(repeat_n=3, showEpoch_n=100, train_branch=False, parallel=True, train_trunk=False, fix_lr=0.0001, reduce_fix_lr=False, fast_training=False)[source]¶
Train the network.
- Parameters:
repeat_n (int, optional) – The number of repeat feed to the network for each batch size data, which will increase the number of iterations in each epoch. Default: 3
showEpoch_n (int, optional) – The number of epoch that show the training information. Default: 100
fast_training (bool, optional) – If True, the batch size will be set to
batch_size*multi_noiseand the network will be trained fast. If False, the network will be slowly trained to get better. Default: False
- Returns:
object – The network object.
array-like – The losses of training set and validation set.
- class colfi.models_ann.OneBranchMLP(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, hidden_layer=3, activation_func='Softplus', loss_name='L1', noise_type='multiNormal', factor_sigma=0.2, multi_noise=5)[source]¶
Bases:
Transfer,DataPreprocessing,CutParamsPredict cosmological parameters with multilayer perceptron (MLP) for one set of datasets.
- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model, i.e. [observations, parameters]. The validation set can also be set to None, i.e. [None, None]. Default: [None, None]
obs_errors (None or array-like, optional) – Observational errors with shape (obs_length,). If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or array-like, optional) – Covariance matrix of the observational data. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonehidden_layer (int, optional) – The number of the hidden layer of the network. Default: 3
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’loss_name (str, optional) – The name of loss function used in the network, which can be ‘L1’, ‘MSE’, or ‘SmoothL1’. See
loss_funcs(). Default: ‘L1’noise_type (str, optional) – The type of Gaussian noise added to the training set, which can be ‘singleNormal’ or ‘multiNormal’. Default: ‘multiNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. For the case ofnoise_type= ‘multiNormal’, it is the standard deviation of the coefficient of the observational error (standard deviation). Default: 0.2multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: False
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘ANN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- property statistic_dim_obs¶
- property statistic_dim_params¶
- train(repeat_n=3, showEpoch_n=100, fast_training=False)[source]¶
Train the network.
- Parameters:
repeat_n (int, optional) – The number of repeat feed to the network for each batch size data, which will increase the number of iterations in each epoch. Default: 3
showEpoch_n (int, optional) – The number of epoch that show the training information. Default: 100
fast_training (bool, optional) – If True, the batch size will be set to
batch_size*multi_noiseand the network will be trained fast. If False, the network will be slowly trained to get better. Default: False
- Returns:
object – The network object.
array-like – The losses of training set and validation set.
- class colfi.models_ann.OptimizeMLP(sim_data, obs_data, param_names, cov_matrix=None, params_dict=None)[source]¶
Bases:
objectTrain networks and predict cosmological parameters using the given simulated data.
- Parameters:
sim_data (array-like or list) – The simulated data.
obs_data (array-like or list) – The observational data.
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
cov_matrix (array-like, list, or None, optional) – Covariance matrix of the observational data. It should be an array with shape (obs_length, obs_length), or a list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. Default: None
params_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: None
- Variables:
chain_leng (int, optional) – The length of each ANN chain. Default: 10000
Note
A lot of networks can be trained using the given simulated data, therefore, this class can be used to test the effect of hyperparameters of the network.
- auto_train(repeat_net=100, procs_n=5, path='ann', save_details=True, **hparams)[source]¶
Automatically train several networks for the given hyperparameters.
- Parameters:
repeat_net (int, optional) – The number of times a network is repeatedly trained with the same data and hyperparameters.
procs_n (int, optional) – The number of processes.
path (str, optional) – The path of the results to be saved. Default: ‘ann’
save_details (bool, optional) – If True, will save the items for each network. Default: True
**hparams (dict) – A dictionary with only one item, whose value is a list that contains several hyperparameters used to train several networks.
- Return type:
None.
- property cov_copy¶
- property obs_errors¶
- class colfi.models_ann.PredictMBMLP(path='ann', randn_num='0.123')[source]¶
Bases:
MultiBranchMLP,LoaderRepredict cosmological parameters using the saved networks.
- class colfi.models_ann.PredictOBMLP(path='ann', randn_num='0.123')[source]¶
Bases:
OneBranchMLP,LoaderRepredict cosmological parameters using the saved networks.
colfi.models_mdn¶
- class colfi.models_mdn.CheckNormType(comp_type='Gaussian', norm_params='z_score')[source]¶
Bases:
object
- class colfi.models_mdn.MultiBranchMDN(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, comp_type='Gaussian', comp_n=3, branch_hiddenLayer=1, trunk_hiddenLayer=2, activation_func='Softplus', noise_type='singleNormal', factor_sigma=1, multi_noise=5)[source]¶
Bases:
MultiBranchMLP,CheckNormTypePredict cosmological parameters with multibranch MDN for multiple sets of datasets.
- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. The validation set can also be set to None. i.e. [observations, parameters] or [None, None]. Default: [None, None]
obs_errors (None or list, optional) – Observational errors, it is a list of errors with shape [(obs_length_1,), (obs_length_2,), …]. If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or list, optional) – A list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonecomp_type (str, optional) – The name of component used in the
MDNmethod, which can be ‘Gaussian’ or ‘Beta’. Default: ‘Gaussian’comp_n (int, optional) – The number of components used in the
MDNmethod. Default: 3branch_hiddenLayer (int, optional) – The number of the hidden layer for the branch part of the network. Default: 1
trunk_hiddenLayer (int, optional) – The number of the hidden layer for the trunk part of the network. Default: 2
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’noise_type (str, optional) – The type of Gaussian noise added to the training set, which should be ‘singleNormal’. Default: ‘singleNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. Default: 1multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_branch (float, optional) – The learning rate setting of the branch part. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
epoch_branch (int, optional) – The number of epoch for the branch part. This only works when training the branch part. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: True
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘MDN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- property sampler¶
- train(repeat_n=3, showEpoch_n=100, train_branch=False, parallel=True, train_trunk=False, fix_lr=0.0001, reduce_fix_lr=False, fast_training=False)[source]¶
Train the network.
- Parameters:
repeat_n (int, optional) – The number of repeat feed to the network for each batch size data, which will increase the number of iterations in each epoch. Default: 3
showEpoch_n (int, optional) – The number of epoch that show the training information. Default: 100
fast_training (bool, optional) – If True, the batch size will be set to
batch_size*multi_noiseand the network will be trained fast. If False, the network will be slowly trained to get better. Default: False
- Returns:
object – The network object.
array-like – The losses of training set and validation set.
- class colfi.models_mdn.OneBranchMDN(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, comp_type='Gaussian', comp_n=3, hidden_layer=3, activation_func='Softplus', noise_type='singleNormal', factor_sigma=1, multi_noise=5)[source]¶
Bases:
OneBranchMLP,CheckNormTypePredict cosmological parameters with mixture density network (MDN) for one set of datasets.
- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model, i.e. [observations, parameters]. The validation set can also be set to None, i.e. [None, None]. Default: [None, None]
obs_errors (None or array-like, optional) – Observational errors with shape (obs_length,). If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or array-like, optional) – Covariance matrix of the observational data. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonecomp_type (str, optional) – The name of component used in the
MDNmethod, which can be ‘Gaussian’ or ‘Beta’. Default: ‘Gaussian’comp_n (int, optional) – The number of components used in the
MDNmethod. Default: 3hidden_layer (int, optional) – The number of the hidden layer of the network. Default: 3
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’noise_type (str, optional) – The type of Gaussian noise added to the training set, which should be ‘singleNormal’. Default: ‘singleNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. Default: 1multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: False
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘MDN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- property sampler¶
- train(repeat_n=3, showEpoch_n=100, fast_training=False)[source]¶
Train the network.
- Parameters:
repeat_n (int, optional) – The number of repeat feed to the network for each batch size data, which will increase the number of iterations in each epoch. Default: 3
showEpoch_n (int, optional) – The number of epoch that show the training information. Default: 100
fast_training (bool, optional) – If True, the batch size will be set to
batch_size*multi_noiseand the network will be trained fast. If False, the network will be slowly trained to get better. Default: False
- Returns:
object – The network object.
array-like – The losses of training set and validation set.
- class colfi.models_mdn.OptimizeMDN(sim_data, obs_data, param_names, cov_matrix=None, params_dict=None)[source]¶
Bases:
OptimizeMLPTrain MDNs and predict cosmological parameters using the given simulated data.
- Parameters:
sim_data (array-like or list) – The simulated data.
obs_data (array-like or list) – The observational data.
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
cov_matrix (array-like, list, or None, optional) – Covariance matrix of the observational data. It should be an array with shape (obs_length, obs_length), or a list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. Default: None
params_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: None
- Variables:
chain_leng (int, optional) – The length of each ANN chain. Default: 10000
Note
A lot of MDNs can be trained using the given simulated data, therefore, this class can be used to test the effect of hyperparameters of the MDN.
- class colfi.models_mdn.PredictMBMDN(path='ann', randn_num='0.123')[source]¶
Bases:
MultiBranchMDN,LoaderRepredict cosmological parameters using the saved networks.
- class colfi.models_mdn.PredictOBMDN(path='ann', randn_num='0.123')[source]¶
Bases:
OneBranchMDN,LoaderRepredict cosmological parameters using the saved networks.
colfi.models_g¶
- class colfi.models_g.MultiBranchMLP_G(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, branch_hiddenLayer=1, trunk_hiddenLayer=2, activation_func='Softplus', noise_type='multiNormal', factor_sigma=0.2, multi_noise=5)[source]¶
Bases:
MultiBranchMLPPredict cosmological parameters with multibranch MNN for multiple sets of datasets.
This is just a special case of
MultiBranchMLP_MGwith one component (comp_n=1).- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. The validation set can also be set to None. i.e. [observations, parameters] or [None, None]. Default: [None, None]
obs_errors (None or list, optional) – Observational errors, it is a list of errors with shape [(obs_length_1,), (obs_length_2,), …]. If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or list, optional) – A list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonebranch_hiddenLayer (int, optional) – The number of the hidden layer for the branch part of the network. Default: 1
trunk_hiddenLayer (int, optional) – The number of the hidden layer for the trunk part of the network. Default: 2
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’noise_type (str, optional) – The type of Gaussian noise added to the training set, which can be ‘singleNormal’ or ‘multiNormal’. Default: ‘multiNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. For the case ofnoise_type= ‘multiNormal’, it is the standard deviation of the coefficient of the observational error (standard deviation). Default: 0.2multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_branch (float, optional) – The learning rate setting of the branch part. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
epoch_branch (int, optional) – The number of epoch for the branch part. This only works when training the branch part. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: True
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘MNN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- train(repeat_n=3, showEpoch_n=100, train_branch=False, parallel=True, train_trunk=False, fix_lr=0.0001, reduce_fix_lr=False, fast_training=False)[source]¶
Train the network.
- Parameters:
repeat_n (int, optional) – The number of repeat feed to the network for each batch size data, which will increase the number of iterations in each epoch. Default: 3
showEpoch_n (int, optional) – The number of epoch that show the training information. Default: 100
fast_training (bool, optional) – If True, the batch size will be set to
batch_size*multi_noiseand the network will be trained fast. If False, the network will be slowly trained to get better. Default: False
- Returns:
object – The network object.
array-like – The losses of training set and validation set.
- class colfi.models_g.OneBranchMLP_G(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, hidden_layer=3, activation_func='Softplus', noise_type='multiNormal', factor_sigma=0.2, multi_noise=5)[source]¶
Bases:
OneBranchMLPPredict cosmological parameters with mixture neural network (MNN) for one set of datasets.
This is just a special case of
OneBranchMLP_MGwith one component (comp_n=1).- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model, i.e. [observations, parameters]. The validation set can also be set to None, i.e. [None, None]. Default: [None, None]
obs_errors (None or array-like, optional) – Observational errors with shape (obs_length,). If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or array-like, optional) – Covariance matrix of the observational data. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonehidden_layer (int, optional) – The number of the hidden layer of the network. Default: 3
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’noise_type (str, optional) – The type of Gaussian noise added to the training set, which can be ‘singleNormal’ or ‘multiNormal’. Default: ‘multiNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. For the case ofnoise_type= ‘multiNormal’, it is the standard deviation of the coefficient of the observational error (standard deviation). Default: 0.2multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: False
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘MNN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- train(repeat_n=3, showEpoch_n=100, fast_training=False)[source]¶
Train the network.
- Parameters:
repeat_n (int, optional) – The number of repeat feed to the network for each batch size data, which will increase the number of iterations in each epoch. Default: 3
showEpoch_n (int, optional) – The number of epoch that show the training information. Default: 100
fast_training (bool, optional) – If True, the batch size will be set to
batch_size*multi_noiseand the network will be trained fast. If False, the network will be slowly trained to get better. Default: False
- Returns:
object – The network object.
array-like – The losses of training set and validation set.
- class colfi.models_g.OptimizeMLP_G(sim_data, obs_data, param_names, cov_matrix=None, params_dict=None)[source]¶
Bases:
OptimizeMLPTrain MNNs and predict cosmological parameters using the given simulated data.
- Parameters:
sim_data (array-like or list) – The simulated data.
obs_data (array-like or list) – The observational data.
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
cov_matrix (array-like, list, or None, optional) – Covariance matrix of the observational data. It should be an array with shape (obs_length, obs_length), or a list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. Default: None
params_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: None
- Variables:
chain_leng (int, optional) – The length of each ANN chain. Default: 10000
Note
A lot of MNNs can be trained using the given simulated data, therefore, this class can be used to test the effect of hyperparameters of the MNN.
- class colfi.models_g.PredictMBMLP_G(path='ann', randn_num='0.123')[source]¶
Bases:
MultiBranchMLP_G,LoaderRepredict cosmological parameters using the saved networks.
- class colfi.models_g.PredictOBMLP_G(path='ann', randn_num='0.123')[source]¶
Bases:
OneBranchMLP_G,LoaderRepredict cosmological parameters using the saved networks.
colfi.models_mg¶
- class colfi.models_mg.MultiBranchMLP_MG(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, comp_type='Gaussian', comp_n=3, branch_hiddenLayer=1, trunk_hiddenLayer=2, activation_func='Softplus', noise_type='multiNormal', factor_sigma=0.2, multi_noise=5)[source]¶
Bases:
MultiBranchMDNPredict cosmological parameters with multibranch MNN for multiple sets of datasets.
- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) which is a list observations with shape [(N,obs_length_1), (N,obs_length_2), …] and simulated parameters of a specific cosmological (or theoretical) model. The validation set can also be set to None. i.e. [observations, parameters] or [None, None]. Default: [None, None]
obs_errors (None or list, optional) – Observational errors, it is a list of errors with shape [(obs_length_1,), (obs_length_2,), …]. If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or list, optional) – A list of covariance matrix with shape [(obs_length_1, obs_length_1), (obs_length_2, obs_length_2), …]. If there is no covariance for some observations, the covariance matrix should be set to None. e.g. [cov_matrix_1, None, cov_matrix_3]. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonecomp_type (str, optional) – The name of component used in the
MDNmethod, which should be ‘Gaussian’. Since the loss function ofMNNis similar to that ofMDNwith Gaussian mixture model, we are using the loss function ofMDN. Default: ‘Gaussian’comp_n (int, optional) – The number of components used in the
MNNmethod. Default: 3branch_hiddenLayer (int, optional) – The number of the hidden layer for the branch part of the network. Default: 1
trunk_hiddenLayer (int, optional) – The number of the hidden layer for the trunk part of the network. Default: 2
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’noise_type (str, optional) – The type of Gaussian noise added to the training set, which can be ‘singleNormal’ or ‘multiNormal’. Default: ‘multiNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. For the case ofnoise_type= ‘multiNormal’, it is the standard deviation of the coefficient of the observational error (standard deviation). Default: 0.2multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_branch (float, optional) – The learning rate setting of the branch part. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
epoch_branch (int, optional) – The number of epoch for the branch part. This only works when training the branch part. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: True
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘MNN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- property sampler¶
- class colfi.models_mg.OneBranchMLP_MG(train_set, param_names, vali_set=[None, None], obs_errors=None, cov_matrix=None, params_dict=None, comp_type='Gaussian', comp_n=3, hidden_layer=3, activation_func='Softplus', noise_type='multiNormal', factor_sigma=0.2, multi_noise=5)[source]¶
Bases:
OneBranchMDNPredict cosmological parameters with mixture neural network (MNN) for one set of datasets.
- Parameters:
train_set (list) – The training set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model. i.e. [observations, parameters]
param_names (list) – A list which contains the parameter names, e.g. [‘H0’,’ombh2’,’omch2’].
vali_set (list, optional) – The validation set that contains simulated observations (measurements) with shape (N, obs_length) and simulated parameters of a specific cosmological (or theoretical) model, i.e. [observations, parameters]. The validation set can also be set to None, i.e. [None, None]. Default: [None, None]
obs_errors (None or array-like, optional) – Observational errors with shape (obs_length,). If
cov_matrixis set to None, the observational errors should be given. Default: Nonecov_matrix (None or array-like, optional) – Covariance matrix of the observational data. If a covariance matrix is given,
obs_errorswill be ignored. Default: Noneparams_dict (dict or None, optional) – Information of cosmological parameters that include the labels, the minimum values, and the maximum values. See
params_dict_zoo(). Default: Nonecomp_type (str, optional) – The name of component used in the
MDNmethod, which should be ‘Gaussian’. Since the loss function ofMNNis similar to that ofMDNwith Gaussian mixture model, we are using the loss function ofMDN. Default: ‘Gaussian’comp_n (int, optional) – The number of components used in the
MNNmethod. Default: 3hidden_layer (int, optional) – The number of the hidden layer of the network. Default: 3
activation_func (str, optional) – Activation function, which can be ‘ReLU’, ‘LeakyReLU’, ‘PReLU’, ‘RReLU’, ‘ReLU6’, ‘ELU’, ‘CELU’, ‘SELU’, ‘SiLU’, ‘Sigmoid’, ‘LogSigmoid’, ‘Tanh’, ‘Tanhshrink’, ‘Softsign’, or ‘Softplus’ (see
activation()). Default: ‘Softplus’noise_type (str, optional) – The type of Gaussian noise added to the training set, which can be ‘singleNormal’ or ‘multiNormal’. Default: ‘multiNormal’
factor_sigma (float, optional) – For the case of
noise_type= ‘singleNormal’,factor_sigmashould be set to 1. For the case ofnoise_type= ‘multiNormal’, it is the standard deviation of the coefficient of the observational error (standard deviation). Default: 0.2multi_noise (int, optional) – The number of realization of noise added to the measurement in one epoch. Default: 5
- Variables:
obs_base (array-like, optional) – The base value of observations that is used for data normalization when training the network to ensure that the scaled observations are ~ 1., it is suggested to set the mean of the simulated observations. The default is the mean of the simulated observations.
params_base (array-like, optional) – The base value of parameters that is used for data normalization when training the network to ensure that the scaled parameters are ~ 1., it is suggested to set the mean of the posterior distribution (or the simulated parameters). The default is the mean of the simulated parameters.
params_space (array-like) – The parameter space with the shape of (n, 2), where n is the number of parameters. For each parameter, it is: [lower_limit, upper_limit].
lr (float, optional) – The learning rate setting of the network. Default: 1e-2
lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
batch_size (int, optional) – The batch size setting of the network. Default: 1250
auto_batchSize (bool, optional) – If True, the batch size will be set automatically in the training process, otherwise, use the setting of
batch_size. Default: Falseepoch (int, optional) – The number of epoch of the training process. Default: 2000
base_epoch (int, optional) – The base number (or the minimum number) of epoch. Default: 1000
auto_epoch (bool, optional) – If True, the epoch will be set automatically in the training process, otherwise, use the setting of
epoch. Default: Falseprint_info (bool, optional) – If True, will print the information of the network. Default: False
scale_obs (bool, optional) – If True, the input data (measurements) will be scaled based on the base values of the data. Default: False
scale_params (bool, optional) – If True, the target data (cosmological parameters) will be scaled based on the base values of parameters. See
DataPreprocessing. Default: Truenorm_obs (bool, optional) – If True, the input data (measurements) of the network will be normalized. Default: True
norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized. Default: True
independent_norm_obs (bool, optional) – If True, the measurements will be normalized independently. This only works when
norm_obs=True. Default: Falseindependent_norm_params (bool, optional) – If True, the target data (cosmological parameters) will be normalized independently. This only works when
norm_params=True. Default: Truenorm_type (str, optional) – The method of normalization, which can be ‘z_score’, ‘minmax’, or ‘mean’ (see
Normalize). Default: ‘z_score’spaceSigma_min (int, optional) – The minimum parameter space to be learned, e.g. for spaceSigma_min=5, the parameter space to be learned is \([-5\sigma, +5\sigma]\). Default: 5
burnInEnd (bool, optional) – If True, it is the end of the burn-in phase, which means the ANN chain has reached a stable state. Default: False
burnInEnd_step (None or int, optional) – The burn-in end step. If None, it means the burn-in phase not end. Default: None
transfer_learning (bool, optional) – If True, the network will be initialized using the well-trained network of the previous step. Default: False
randn_num (float or str, optional) – A random number that identifies the saved results. Default: float
nde_type (str, optional) – A string that indicate which NDE is used, which should be ‘MNN’.
file_identity_str (str, optional) – A string that identifies the files saved to the disk, which is useful to identify the saved files. Default: ‘’
- property sampler¶
- class colfi.models_mg.PredictMBMLP_MG(path='ann', randn_num='0.123')[source]¶
Bases:
MultiBranchMLP_MG,LoaderRepredict cosmological parameters using the saved networks.
- class colfi.models_mg.PredictOBMLP_MG(path='ann', randn_num='0.123')[source]¶
Bases:
OneBranchMLP_MG,LoaderRepredict cosmological parameters using the saved networks.
..colfi.models_annmc ..——————
…. automodule:: colfi.models_annmc .. :members: .. :undoc-members: .. :show-inheritance: