paraphernalia.torch.modules module

A collection of utility PyTorch modules.

class AdaptiveMultiLoss(num_losses)[source]

Automatic loss balancing.

Parameters
  • components ([type]) – [description]

  • num_losses (int) –

forward(losses)[source]
Parameters

losses (Tensor) – The losses to balance

Returns

Combined loss

Return type

float

class Parallel(components)[source]

A module that runs a number of submodule in parallel and collects their outputs into a list.

Parameters

components (List[nn.Module]) – a list of submdodules to run in parallel

forward(*inputs)[source]

Run each submodule on input, and accumulate the outputs into a list.

Returns

the outputs of each module

Return type

List

class Constant(value)[source]

A module that returns a constant value, ignoring any inputs.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Parameters

value (torch.Tensor) –

forward(*ignored)[source]

Return the constant value, ignoring any inputs.

Parameters

ignored – any number of inputs which will be ignored

Returns

the specific constant value

Return type

Tensor

class WeightedSum(**components)[source]

More or less a weighted sum of named module outputs, but with special handling for negative weights.

In order for the weighting to make sense, the components needs outputs with the same shape and meaning.

For loss functions outputs should be in the range [0,1]

Parameters

components (torch.nn.modules.module.Module) –

set_weight(name, value)[source]

Set the weight associated with a module.

Parameters
  • name (str) – the name of the module

  • value (float) – the new weight

Return type

None

forward(x)[source]

Compute the weighted loss.

Parameters

x (torch.Tensor) –

class SimilarTo(targets)[source]

Cosine similarity test with mean pooling.

Parameters

targets (torch.Tensor) – A tensor of dimension (targets, channels)

forward(x)[source]
Parameters

x (Tensor) – A batch of vectors (batch, channels)

class SimilarToAny(targets)[source]

Cosine similarity test with max pooling.

Parameters

targets (torch.Tensor) – A tensor of dimension (targets, channels)

forward(x)[source]
Parameters

x (Tensor) – A batch of vectors (batch, channels)