Network#
Classes#
- class BaseNetwork[source]#
Bases:
Module,ABC,Generic[ModuleListT]Base class for all networks in NetLoader
- Attributes:
- name
str Name of the network, used for saving
- version
str NetLoader version string
- checkpoints
list[TensorListLike] Outputs from each checkpoint with each Tensor having shape (N,…) and type float, where N is the batch size
- kl_loss
Tensor KL divergence loss on the latent space of shape (1) and type float, if using a sample layer
- layers
ModuleListT Network construction
- name
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- abstractmethod forward(x)[source]#
Forward pass of the network.
- Parameters:
- x
TensorListLike Input tensor with shape (N,…) and type float, where N is the batch size
- x
- Returns:
TensorListLikeOutput tensor from the network with shape (N,…) and type float
- get_config(dict_=True)[source]#
Returns the network configuration.
- Overloads:
- get_config(self, dict_: Literal[False] = ...) Config
- to(*args, **kwargs)[source]#
See
torch.nn.Module.to().
- class CompatibleNetwork(net, *, name='')[source]#
Bases:
BaseNetwork[Module|ModuleList]A wrapper for Module that ensures compatibility with BaseArchitecture by adding required attributes.
- Attributes:
- name
str Name of the network, used for saving
- version
str NetLoader version string
- checkpoints
list[TensorListLike] Outputs from each checkpoint with each Tensor having shape (N,…) and type float, where N is the batch size
- kl_loss
Tensor KL divergence loss on the latent space of shape (1) and type float, if using a sample layer
- layers
Module|ModuleList Network construction
- name
- Parameters:
- net
Module|ModuleList The neural network module to wrap or list of layers in the network
- name
str,Optional Name of the network, used for saving
- net
- forward(x)[source]#
Forward pass of the network.
- Parameters:
- x
TensorListLike Input tensor with shape (N,…) and type float, where N is the batch size
- x
- Returns:
TensorListLikeOutput tensor from the network with shape (N,…) and type float
- to(*args, **kwargs)[source]#
See
torch.nn.Module.to().
- class Network(name, config, in_shape, out_shape, *, suppress_warning=False, root='', defaults=None)[source]#
Bases:
BaseNetwork[TypedModuleList[BaseLayer]]Constructs a neural network from a configuration file.
- Attributes:
- group
int Which group is the active group if a layer has the group attribute
- layer_num
int|NoneType Number of layers to use, if None use all layers
- name
str Name of the network, used for saving
- version
str NetLoader version string
- checkpoints
list[TensorListLike] Outputs from each checkpoint with each Tensor having shape (N,…) and type float, where N is the batch size
- kl_loss
Tensor KL divergence loss on the latent space of shape (1) and type float, if using a sample layer
- layers
TypedModuleList[BaseLayer] Network layers
- shapes
Shapes Layer output shapes
- check_shapes
Shapes Checkpoint output shapes
- group
- Parameters:
- name
str Name of the network configuration file
- config
str|Config Path to the network config directory or configuration dictionary
- in_shape
list[int] |list[list[int]] |tuple[int, …] Shape of the input tensor(s), excluding batch size
- out_shape
list[int] Shape of the output tensor, excluding batch size
- suppress_warningbool,
Optional If output shape mismatch warning should be suppressed, default = False
- root
str,Optional Root directory to prepend to config file path
- defaults
dict[str,Any] |NoneType,Optional Default values for the parameters for each type of layer
- name
- __getitem__(idx)[source]#
Returns the layer at the specified index.
- Overloads:
- __getitem__(self, idx: slice) TypedModuleList[BaseLayer]
- Parameters:
- Returns:
Module|TypedModuleList[BaseLayer]Layer(s) at the specified index or slice
- __len__()[source]#
Returns the number of layers in the network.
- Returns:
intNumber of layers in the network
- forward(x)[source]#
Forward pass of the network.
- Parameters:
- x
TensorListLike Input tensor(s) with shape (N,…) and type float, where N is the batch size
- x
- Returns:
NormalizingFlow|TensorListLikeOutput tensor from the network with shape (N,…) and type float, or NormalizingFlow if the last layer is from layers.flows
- get_config(dict_=True)[source]#
Returns the network configuration.
- Overloads:
- get_config(self, dict_: Literal[False] = ...) Config
- to(*args, **kwargs)[source]#
See
torch.nn.Module.to().
- class Branch(net_check, branches, shapes, *, checkpoint=True, dim=0, channels=None, root='', method='concat', shape=None, defaults=None, **kwargs)[source]#
Bases:
BaseLayerCreates a branching layer that processes the input through multiple branches and combines the outputs.
- Attributes:
- group
int Layer group, if 0 it will always be used, else it will only be used if its group matches the Networks
- description
str Description of the layer
- branches
list[BaseLayer] List of subnetworks for each branch
- head
Concatenate|Pack|Shortcut Layer to combine the outputs from each branch, either concatenation, packing or summation
- group
- Parameters:
- net_checkbool
If layer index should be relative to checkpoint layers
- branches
list[list[dict[str,Any]]] List of layer configurations for each branch
- shapes
Shapes Shape of the outputs from each layer
- checkpointbool,
Optional If layer index should be relative to checkpoint layers, default = True
- dim
int,Optional Dimension to concatenate along if method is ‘concat’, default = 0
- channels
int,Optional Number of output channels, won’t be used if shape is provided, if channels and shape aren’t provided, the input dimensions will be preserved
- root
str,Optional Root directory to prepend to config file path
- method
Literal[‘concat’, ‘pack’, ‘sum’],Optional Method to combine the outputs from each branch, either concatenation, packing or summation, default = ‘concat’
- shape
list[int] |NoneType,Optional Output shape of the block, will be used if provided; otherwise, channels will be used
- defaults
dict[str,Any] |NoneType,Optional Default values for the parameters for each type of layer
- **kwargs
Leftover parameters to pass to base layer for checking
- extra_repr()[source]#
Displays layer parameters when printing the network.
- Returns:
strLayer parameters
- forward(x, *_, **__)[source]#
Forward pass for the Branch layer.
- Parameters:
- x
TensorListT Input tensor(s) with dtype float32 and shape (N,…), where N is batch size
- x
- Returns:
TensorListTOutput tensor(s) from the subnetwork with dtype float32 and shape (N,…)
- to(*args, **kwargs)[source]#
See
torch.nn.Module.to().
- class Composite(net_check, name, shapes, *, checkpoint=True, channels=None, root='', config_dir='', shape=None, defaults=None, config=None, **kwargs)[source]#
Bases:
BaseLayerCreates a subnetwork from a configuration file.
- Attributes:
- Parameters:
- net_checkbool
If layer index should be relative to checkpoint layers
- name
str Name of the subnetwork
- shapes
Shapes Shape of the outputs from each layer
- checkpointbool,
Optional If layer index should be relative to checkpoint layers, default = True
- channels
int,Optional Number of output channels, won’t be used if shape is provided, if channels and shape aren’t provided, the input dimensions will be preserved
- root
str,Optional Root directory to prepend to config file path
- config_dir
str,Optional Path to the directory with the network configuration file, won’t be used if config is provided
- shape
list[int] |NoneType,Optional Output shape of the block, will be used if provided; otherwise, channels will be used
- defaults
dict[str,Any] |NoneType,Optional Default values for the parameters for each type of layer
- config
dict[str,Any] |Config|NoneType,Optional Network configuration dictionary
- **kwargs
Leftover parameters to pass to base layer for checking
- extra_repr()[source]#
Displays layer parameters when printing the network.
- Returns:
strLayer parameters
- forward(x, *_, **__)[source]#
Forward pass for the Composite layer.
- Parameters:
- x
TensorListT Input tensor(s) with dtype float32 and shape (N,…), where N is batch size
- x
- Returns:
TensorListTOutput tensor(s) from the subnetwork with dtype float32 and shape (N,…)
- to(*args, **kwargs)[source]#
See
torch.nn.Module.to().