Backbones#

Backbones constitute the underlying architecture of the network. They are responsible for extracting features from the input. Once loaded by the model (Models), the backbone is accessible as the self.net attribute.

The specific choice of the backbone depends on the benchmark (i.e, the dataset - see Datasets), which defines the backbone from the get_backbone method.

Features and logits#

Models may exploit the features extracted by the backbone at different levels. For example, DER uses the soft targets produced after the classification layer of the backbone (i.e., the logits), while iCaRL uses the features extracted by the backbone before the classification layer to compute the class means. In order to allow for this flexibility, all backbones must accept the returnt argument, which specifies the level at which the features are extracted. The possible values are:

  • returnt='out': the backbone returns the logits produced after the classification layer.

  • returnt='features': the backbone returns the features extracted immediately before the classification layer.

  • returnt='both': the backbone returns both the logits and the features (a tuple (logits, feature)).

In addition, some models require the output of all the layers of the backbone (e.g, TwF). In this case, the returnt argument can be set to:

  • returnt='full': the backbone returns the output of all the layers (a list of tensors).

Note

Other values of returnt may be supported by the backbone, but they are not guaranteed to work with all the models.

Mammoth backbone base class#

Backbones should inherit from the MammothBackbone class (below), which provides some useful methods. This is not a strict requirement, but it is strongly recommended for compatibility with the existing models.

The MammothBackbone class provides the following methods:

  • features: returns the features extracted by the backbone (before the classification layer). This is equivalent to calling self.net(x, returnt='features').

  • get_grads: returns the gradients of the backbone with respect to the loss, concatenated in a single tensor.

  • set_grads: sets the gradients of the backbone from a single (concatenated) tensor.

  • get_params: returns all the parameters of the backbone concatenated in a single tensor.

  • set_params: sets the parameters of the backbone from a single (concatenated) tensor.

Module attributes and functions#

class backbone.MammothBackbone(**kwargs)[source]#

A backbone module for the Mammoth model.

Parameters:

**kwargs – additional keyword arguments

forward()[source]#

Compute a forward pass.

Return type:

Tensor

features()[source]#

Get the features of the input tensor (same as forward but with returnt=’features’).

Return type:

Tensor

get_params()[source]#

Returns all the parameters concatenated in a single tensor.

Return type:

Tensor

set_params()[source]#

Sets the parameters to a given value.

get_grads()[source]#

Returns all the gradients concatenated in a single tensor.

Return type:

Tensor

get_grads_list()[source]#

Returns a list containing the gradients (a tensor for each layer).

features(x)[source]#

Compute the features of the input tensor.

Parameters:

x (Tensor) – input tensor

Returns:

features tensor

Return type:

Tensor

forward(x, returnt='out')[source]#

Compute a forward pass.

Parameters:
  • x (Tensor) – input tensor (batch_size, *input_shape)

  • returnt – return type (a string among out, features, both, or all)

Returns:

output tensor

Return type:

Tensor

get_grads()[source]#

Returns all the gradients concatenated in a single tensor.

Returns:

gradients tensor

Return type:

Tensor

get_grads_list()[source]#

Returns a list containing the gradients (a tensor for each layer).

Returns:

gradients list

get_params()[source]#

Returns all the parameters concatenated in a single tensor.

Returns:

parameters tensor

Return type:

Tensor

set_params(new_params)[source]#

Sets the parameters to a given value.

Parameters:

new_params (Tensor) – concatenated values to be set

backbone.num_flat_features(x)[source]#

Computes the total number of items except the first (batch) dimension.

Parameters:

x (Tensor) – input tensor

Returns:

number of item from the second dimension onward

Return type:

int

backbone.xavier(m)[source]#

Applies Xavier initialization to linear modules.

Parameters:

m (Module) – the module to be initialized

Example::
>>> net = nn.Sequential(nn.Linear(10, 10), nn.ReLU())
>>> net.apply(xavier)