Base Networks#
Classes#
- class BaseArchitecture(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:
UtilityMixin,ABC,Generic[LossCT,TensorLossCT]Base architecture class that other types of architectures build from
- Attributes:
- description
str Description of the architecture
- version
str Version of the architecture when it was created or re-saved
- 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
BaseNetwork Neural network
- description
- 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
- batch_predict(data, **_)[source]#
Generates predictions for the given data batch.
- Parameters:
- data
TensorListLike Data of shape (N,…) and type float to generate predictions for, where N is the batch size
- data
- Returns:
tuple[NDArrayListLike|NoneType, …]Predictions of shape (N,…) and type float for the given data
- compile(level='net', **kwargs)[source]#
Compiles the network using torch.compile for faster training and prediction.
- Parameters:
- level{‘net’, ‘loss’}
If ‘net’, compiles the network, if ‘loss’, compiles the loss function
- **kwargs
Optional keyword arguments to pass to torch.compile
- extra_repr()[source]#
Additional representation of the architecture.
- Returns:
strArchitecture specific representation
- get_device()[source]#
Gets the device of the architecture.
- Returns:
torch.deviceDevice of the architecture
- get_epochs()[source]#
Returns the number of epochs the architecture has been trained for.
- Returns:
intNumber of epochs
- get_loss_weights(name='')[source]#
Gets the weights for a loss function term or all loss function term weights.
- Overloads:
- get_losses()[source]#
Returns the training and validation losses as dictionaries of losses if loss is a dictionary, else returns losses.
- get_save_path()[source]#
Gets the path to save the architecture.
- Returns:
strPath to save the architecture
- predict(loader, *, inputs=False, path='', **kwargs)[source]#
Generates predictions for the architecture and can save to a file.
- Parameters:
- loader
DataLoader[Any] Data loader to generate predictions for
- inputsbool,
Optional If the input data should be returned and saved, default = False
- path
str,Optional Path as pkl file to save the predictions if they should be saved
- **kwargs
Optional keyword arguments to pass to batch_predict
- loader
- Returns:
dict[str,NDArrayLike]Prediction IDs, Optional inputs, target values, and predicted values of shape (N,…) and type float for dataset of size N
- set_loss_weights(*args, **kwargs)[source]#
Sets the weights for the loss function terms. Loss weights passed as positional arguments are set in the order of the architecture loss weight keys, while keyword arguments are set by name and will override positional arguments.
- Parameters:
- *args, **kwargs
Weights for the loss function terms
- set_save_freq(save_freq)[source]#
Sets the frequency of saving the architecture.
- Parameters:
- save_freq
int Frequency of saving the architecture in epochs
- save_freq
- set_save_path(name, states_dir='', *, overwrite=False)[source]#
Sets the save path for the architecture.
- Overloads:
- Parameters:
- name
str Name to save the architecture as or full path to save the architecture
- states_dir
str,Optional Directory to save the architecture, if empty name is treated as full path
- 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
- name
- to(*args, **kwargs)[source]#
See
torch.nn.Module.to().
- train(train)[source]#
Changes the train/eval state of the architecture.
- Parameters:
- trainbool
If the architecture should be in the train state
- training(epochs, loaders)[source]#
Trains & validates the network for each epoch.
- Parameters:
- epochs
int Number of epochs to train the network up to
- loaders
tuple[DataLoader[Any],DataLoader[Any]] Train and validation data loaders
- epochs
Functions#
- load_arch(num, states_dir, arch_name, **kwargs)[source]#
Loads an architecture from file.
- Parameters:
- Returns:
BaseArchitectureSaved architecture object