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:
descriptionstr

Description of the architecture

versionstr

Version of the architecture when it was created or re-saved

lossestuple[list[LossCT], list[LossCT]]

Architecture training and validation losses as a float or dictionary of losses for each loss function

transformsdict[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

optimiserOptimizer

Architecture optimiser

schedulerLRScheduler

Optimiser scheduler

netBaseNetwork

Neural network

Parameters:
save_numint | str

File number or name to save the architecture

states_dirstr

Directory to save the architecture

netModule | 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_freqint, Optional

Frequency of epochs to save the architecture, default = 1

learning_ratefloat | tuple[float, …], Optional

Optimiser initial learning rate, default = 1e-3

descriptionstr, 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)

transformlist[BaseTransform] | BaseTransform | NoneType, Optional

Transformation(s) of the network’s output(s)

in_transformlist[BaseTransform] | BaseTransform | NoneType, Optional

Transformation(s) for the network’s input(s)

optimiser_kwargsdict[str, Any] | NoneType, Optional

Optional keyword arguments to pass to init_optimiser

scheduler_kwargsdict[str, Any] | NoneType, Optional

Optional keyword arguments to pass to init_scheduler

batch_predict(data, **_)[source]#

Generates predictions for the given data batch.

Parameters:
dataTensorListLike

Data of shape (N,…) and type float to generate predictions for, where N is the batch size

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:
str

Architecture specific representation

get_device()[source]#

Gets the device of the architecture.

Returns:
torch.device

Device of the architecture

get_epochs()[source]#

Returns the number of epochs the architecture has been trained for.

Returns:
int

Number of epochs

get_hyperparams()[source]#

Returns the hyperparameters of the architecture.

Returns:
dict[str, Any]

Hyperparameters of the architecture

get_loader_states()[source]#

Gets the current data loader states.

Returns:
tuple[Tensor, Tensor] | NoneType

Train and validation data loader states

get_loss_weights(name='')[source]#

Gets the weights for a loss function term or all loss function term weights.

Overloads:

get_loss_weights(self, name: str) float
get_loss_weights(self) dict[str, float]
Parameters:
namestr, Optional

Name of the loss term to get the weight for, if empty string, returns all loss term weights

Returns:
float | dict[str, float]

Weight for the specified loss term or all loss term weights

get_losses()[source]#

Returns the training and validation losses as dictionaries of losses if loss is a dictionary, else returns losses.

Returns:
tuple[list[float], list[float]] | tuple[dict[str, list[float]], dict[str, list[float]]]

Training and validation losses as dictionaries of lists if loss is a dictionary, else as lists

get_param_groups(learning_rate)[source]#

Gets the parameter groups for the optimiser.

Parameters:
learning_ratefloat | tuple[float, …] | NoneType

Learning rate or learning rates for the parameter groups

Returns:
ParamsT

Parameter groups for the optimiser

get_rng_states()[source]#

Gets the current random number generator states.

Returns:
dict[str, Any]

Random number generator states for numpy, data loaders, pytorch, and cuda

get_save_path()[source]#

Gets the path to save the architecture.

Returns:
str

Path to save the architecture

predict(loader, *, inputs=False, path='', **kwargs)[source]#

Generates predictions for the architecture and can save to a file.

Parameters:
loaderDataLoader[Any]

Data loader to generate predictions for

inputsbool, Optional

If the input data should be returned and saved, default = False

pathstr, Optional

Path as pkl file to save the predictions if they should be saved

**kwargs

Optional keyword arguments to pass to batch_predict

Returns:
dict[str, NDArrayLike]

Prediction IDs, Optional inputs, target values, and predicted values of shape (N,…) and type float for dataset of size N

save()[source]#

Saves the architecture.

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_rng_states(states)[source]#

Sets the random number generator states.

Parameters:
statesdict[str, Any]

Random number generator states for numpy, data loaders, pytorch, and cuda

set_save_freq(save_freq)[source]#

Sets the frequency of saving the architecture.

Parameters:
save_freqint

Frequency of saving the architecture in epochs

set_save_path(name, states_dir='', *, overwrite=False)[source]#

Sets the save path for the architecture.

Overloads:

set_save_path(self, name: str, states_dir: str, *, overwrite: bool = ...) None
set_save_path(self, name: str, *, overwrite: bool = ...) None
Parameters:
namestr

Name to save the architecture as or full path to save the architecture

states_dirstr, 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

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:
epochsint

Number of epochs to train the network up to

loaderstuple[DataLoader[Any], DataLoader[Any]]

Train and validation data loaders

Functions#

load_arch(num, states_dir, arch_name, **kwargs)[source]#

Loads an architecture from file.

Parameters:
numint | str

File number or name of the saved state

states_dirstr

Directory to the save files

arch_namestr

Name of the architecture

**kwargs

Optional keyword arguments to pass to torch.load

Returns:
BaseArchitecture

Saved architecture object