Function reconstruction

refann.refann

class refann.refann.ANN(data, hidden_layer=1, mid_node=4096, hp_model='rec_1', loss_func='L1')[source]

Bases: refann.train.Train

Reconstruct functions with Artificial Neural Network.

Parameters:
  • data (array-like) – An array with shape of (N, 3), where N is the number of data points, each column represents \(X, Y, \sigma_Y\).
  • hidden_layer (int, optional) – The number of hidden layers. Default: 1
  • mid_node (int, optional) – The number of nodes (or neurons) of the middle layer. Default: 4096
  • hp_model (int, optional) – The hyperparameter models, ‘rec_1’ (no batch normalization) or ‘rec_2’ (with batch normalization). Default: ‘rec_1’
  • loss_func (str, optional) – The loss function, there are three loss functions in this code, L1Loss (‘L1’), MSELoss (‘MSE’), and SmoothL1Loss (‘SmoothL1’). Default: ‘L1’
Variables:
  • lr (float, optional) – The learning rate. Default: 1e-1
  • lr_min (float, optional) – The minimum of the learning rate. Default: 1e-8
  • iteration (int, optional) – The number of iterations. Default: 30000
  • batch_size_max (int, optional) – The maximum of the batch size. Defalt: 300
  • scale_inputs (bool, optional) – If True, the inputs data will be normalized, otherwise, do nothing. Defalt: True
  • scale_target (bool, optional) – If True, the outputs (or target) data will be normalized, otherwise, do nothing. Defalt: True
  • scale_type (str, optional) – The normalization method, ‘minmax’, ‘mean’, or ‘z_score’. Default: ‘z_score’
  • fix_initialize (bool, optional) – If True, the network will be initialized from a specific seed. Default: True
  • print_info (bool, optional) – If True, some information about the training process will be printed. Default: True

Note

Hyperparameters of the ANN, such as the number of hidden layers (hidden_layer), the number of neurons (mid_node), hyperparameter model (hp_model), should be optimized before reconstructing functions from data. See https://doi.org/10.3847/1538-4365/ab620b for details.

plot_func()[source]
plot_loss()[source]
predict(xpoint=None, xspace=None)[source]

Prediction

Parameters:
  • xpoint (array-like or None, optional) – An array of \(X\) points. Default: None
  • xspace (tuple, list, or None, optional) – If not None, xpoint will be ignored, and it should be (xmin, xmax, npoint) or [xmin, xmax, npoint]. Default: None
Returns:

The reconstructed function.

Return type:

array-like

save_func(path='func', obsName='Hz', file_type='npy')[source]
save_net(path='func', obsName='Hz')[source]
statistic()[source]
train()[source]
class refann.refann.OptimizeANN(truth, hidden_layers=[1, 2, 3], mid_nodes=[128, 256])[source]

Bases: object

get_optimal()[source]
get_risk(path='', obsName='Hz', file_type='npy')[source]
plot_risk()[source]
risk(predict, error)[source]
rss(predict)[source]
class refann.refann.RePredictANN(data, hidden_layer=1, mid_node=4096)[source]

Bases: refann.refann.ANN

Reconstruct function using the saved well-trained networks

Parameters:
  • data (array-like) – An array with shape of (N, 3), where N is the number of data points, each column represents \(X, Y, \sigma_Y\).
  • hidden_layer (int, optional) – The number of hidden layers. Default: 1
  • mid_node (int, optional) – The number of nodes (or neurons) of the middle layer. Default: 4096
Variables:
  • scale_inputs (bool, optional) – If True, the inputs data will be normalized, otherwise, do nothing. Defalt: True
  • scale_target (bool, optional) – If True, the outputs (or target) data will be normalized, otherwise, do nothing. Defalt: True
  • scale_type (str, optional) – The normalization method, ‘minmax’, ‘mean’, or ‘z_score’. Default: ‘z_score’
load_net(path='func', obsName='Hz')[source]

refann.evaluate

refann.evaluate.plot_loss(loss)[source]
refann.evaluate.predict(net, inputs, use_GPU=False, in_type='numpy')[source]

Make predictions using a well-trained network.

Parameters:
  • inputs (numpy array or torch tensor) – The inputs of the network.
  • use_GPU (bool) – If True, calculate using GPU, otherwise, calculate using CPU.
  • in_type (str) – The data type of the inputs, it can be ‘numpy’ or ‘torch’.

refann.save

refann.save.mkdir(path)[source]

Make a directory in a particular location if it is not exists, otherwise, do nothing.

Parameters:path (str) – The path of a file.

Examples

>>> mkdir('/home/UserName/test')
>>> mkdir('test/one')
>>> mkdir('../test/one')
refann.save.savenpy(path, FileName, File, dtype=<type 'numpy.float32'>)[source]

Save an array to a binary file in .npy format using numpy.save() function.

Parameters:
  • path (str) – The path of the file to be saved.
  • FileName (str) – The name of the file to be saved.
  • File (object) – The file to be saved.
  • dtype (str or object) – The type of the data to be saved. Default: numpy.float32.
refann.save.savetxt(path, FileName, File)[source]

Save the .txt files using numpy.savetxt() funtion.

Parameters:
  • path (str) – The path of the file to be saved.
  • FileName (str) – The name of the file to be saved.
  • File (object) – The file to be saved.