Normalising Flow Networks#
Classes#
- class NormFlow(save_num, states_dir, net, *, overwrite=False, mix_precision=False, save_freq=1, learning_rate=0.001, description='', verbose='epoch', transform=None, in_transform=None, optimiser_kwargs=None, scheduler_kwargs=None)[source]#
Bases:
BaseArchitectureTransforms a simple distribution into a distribution that reflects the input data
Requires last layer to be a normalising flow and will not pass input data through the network.
- Attributes:
- net
BaseNetwork Neural spline flow
- description
str Description of the architecture
- losses
tuple[list[LossCT],list[LossCT]] Architecture training and validation losses as a float or dictionary of losses for each loss function
- transforms
dict[str,list[BaseTransform] |BaseTransform|NoneType] Keys for the output data from predict and corresponding transforms
- idxs: ndarray | None
Training data indices with shape (N) and type int, where N is the number of elements in the training dataset
- optimiser
Optimizer Architecture optimiser
- scheduler
LRScheduler Optimiser scheduler
- net
- Parameters:
- save_num
int|str File number or name to save the architecture
- states_dir
str Directory to save the architecture
- net
Module|BaseNetwork Network to predict low-dimensional data
- overwritebool,
Optional If saving can overwrite an existing save file, if False and file with the same name exists, an error will be raised, default = False
- mix_precisionbool,
Optional If mixed precision should be used, default = False
- save_freq
int,Optional Frequency of epochs to save the architecture, default = 1
- learning_rate
float|tuple[float, …],Optional Optimiser initial learning rate, default = 1e-3
- description
str,Optional Description of the architecture
- verbose{‘epoch’, ‘full’, ‘plot’, ‘progress’,
NoneType} If details about each epoch should be printed (‘epoch’), details about epoch and epoch progress (full), details about epoch and an ASCII plot of the loss progress (‘plot’), just total progress (‘progress’), or nothing (None)
- transform
list[BaseTransform] |BaseTransform|NoneType,Optional Transformation(s) of the network’s output(s)
- in_transform
list[BaseTransform] |BaseTransform|NoneType,Optional Transformation(s) for the network’s input(s)
- optimiser_kwargs
dict[str,Any] |NoneType,Optional Optional keyword arguments to pass to init_optimiser
- scheduler_kwargs
dict[str,Any] |NoneType,Optional Optional keyword arguments to pass to init_scheduler
- save_num
- predict(loader=None, *, path='', num=None, **__)[source]#
Generates probability distributions for a dataset and can save to a file.
- Parameters:
- Returns:
dict[str,NDArrayLike]Predicted distribution, with shape (N,…,S) and type float for dataset of size N
- class NormFlowEncoder(save_num, states_dir, net, *, overwrite=False, mix_precision=False, net_checkpoint=None, description='', verbose='epoch', train_epochs=(0, -1), learning_rate=(0.001, 0.001), classes=None, loss_func=None, transform=None, in_transform=None, optimiser_kwargs=None, scheduler_kwargs=None)[source]#
Bases:
BaseEncoderCalculates the loss for a network and normalising flow that takes high-dimensional data and predicts a low-dimensional data distribution.
Requires the normalising flow to be the last layer in the network.
- Attributes:
- description
str Description of the architecture
- losses
tuple[list[LossCT],list[LossCT]] Architecture training and validation losses as a float or dictionary of losses for each loss function
- transforms
dict[str,list[BaseTransform] |BaseTransform|NoneType] Keys for the output data from predict and corresponding transforms
- idxs: ndarray | None
Training data indices with shape (N) and type int, where N is the number of elements in the training dataset
- classes
Tensor|NoneType Unique classes of shape (C) and type int/float, where C is the number of classes
- optimiser
Optimizer Architecture optimiser
- scheduler
LRScheduler Optimiser scheduler
- net
BaseNetwork Neural network
- description
- Parameters:
- save_num
int|str File number or name to save the flow
- states_dir
str Directory to save the architecture and flow
- net
Module|BaseNetwork[ModuleList] Normalising flow to predict low-dimensional data distribution
- overwritebool,
Optional If saving can overwrite an existing save file, if True and file with the same name exists, an error will be raised, default = False
- mix_precision: bool, Optional
If mixed precision should be used, default = False
- net_checkpoint
int,Optional Network checkpoint for calculating the encoder’s loss, if none, will use output from the network if output is a Tensor, else no encoder loss will be used
- description
str,Optional Description of the architecture training
- verbose{‘full’, ‘progress’,
NoneType} If details about epoch should be printed (‘full’), just a progress bar (‘progress’), or nothing (None)
- train_epochs
tuple[int,int],Optional Epoch when to start training the normalising flow and epoch when to stop training the encoder, if both are zero, then flow will be trained from the beginning and encoder will not be trained, if both are -1, then flow will never be trained and encoder will always be trained, default = (0,-1)
- learning_rate
tuple[float,float],Optional Optimiser initial learning rate for encoder and normalising flow, if None, no optimiser or scheduler will be set, default = (1e-3,1e-3)
- classes
Tensor,Optional Unique classes of shape (C) and type int/float, where C is the number of classes
- loss_func
BaseLoss|NoneType,Optional Loss function for the encoder, if None MSELoss will be used if classes is None, else CrossEntropyLoss will be used
- in_transform
BaseTransform,Optional Transformation for the input data
- optimiser_kwargs
dict[str,Any] |NoneType,Optional Optional keyword arguments to pass to init_optimiser
- scheduler_kwargs
dict[str,Any] |NoneType,Optional Optional keyword arguments to pass to init_scheduler
- save_num
- batch_predict(data, *, num=None, **_)[source]#
Generates probability distributions for the data batch
- Parameters:
- data
TensorListLike Data of shape (N,…) and type float to generate distributions for, where N is the batch size
- num
list[int] |NoneType,Optional Number of samples, S, to generate, default = [1e3]
- data
- Returns:
tuple[NDArrayListLike|NoneType,ndarray|NoneType]Architecture output with shape (N,…) and type float and samples of shape (N,S) and type float from each probability distribution
- extra_repr()[source]#
Additional representation of the architecture.
- Returns:
strArchitecture specific representation
- predict(loader, *_, inputs=False, bin_num=100, path='', num=None, **__)[source]#
Generates probability distributions for a dataset and can save to a file
- Parameters:
- loader
DataLoader Dataset to generate predictions for
- inputsbool,
Optional If the input data should be returned and saved, default = False,
- bin_num
int,Optional Number of bins for calculating the probability of the target and maximum of the distribution, higher is more precise but requires more samples, default = 100
- path
str,Optional Path as a pkl file to save the predictions if they should be saved
- num
list[int] |NoneType,Optional Number of samples, S, to generate from the predicted distribution, default = [1e3]
- loader
- Returns:
dict[str,NDArrayLike]Prediction IDs of shape (N), Optional inputs, target values, target probability, distribution maximum, distribution median of shape (N,…), and predicted distribution of shape (N,…,S) and type float for dataset of size N