pyprobound.layers.layer.Layer

class Layer(layer_spec, input_shape, min_input_length, max_input_length, name='')

Bases: Transform, LengthManager

Experiment-specific LayerSpec container.

layer_spec

The specification of the layer.

Type:

LayerSpec

_modes

Tuples of every binding mode and respective index that a layer appears in.

Type:

set[tuple[Mode, int]]

__init__(layer_spec, input_shape, min_input_length, max_input_length, name='')

Initializes the layer.

Parameters:
  • layer_spec (LayerSpec) – The specification of the layer.

  • input_shape (int) – The number of elements in an input sequence.

  • min_input_length (int) – The minimum number of finite elements in an input sequence.

  • max_input_length (int) – The maximum number of finite elements in an input sequence.

  • name (str) – A string used to describe the layer.

Methods

cache(fun)

Decorator for a function to cache its output.

check_length_consistency()

Checks that input lengths of Binding components are consistent.

components()

Iterator of child components.

forward(seqs)

A transformation applied to a sequence tensor.

freeze()

Turns off gradient calculation for all parameters.

in_len(length[, mode])

Calculates the receptive field.

lengths(seqs)

Counts the number of finite elements in each sequence.

max_embedding_size()

The maximum number of bytes needed to encode a sequence.

optim_procedure([ancestry, current_order])

The sequential optimization procedure for all Binding components.

out_len(length[, mode])

Calculates the number of elements in the output length dimension.

reload(checkpoint)

Loads the model from a checkpoint file.

reload_from_state_dict(state_dict)

Loads the model from a state dict.

save(checkpoint[, flank_lengths])

Saves the model to a file with "state_dict" and "metadata" fields.

unfreeze([parameter])

Turns on gradient calculation for the specified parameter.

update_binding_optim(binding_optim)

Updates a BindingOptim with the specification's optimization steps.

update_input_length([left_shift, ...])

Updates input shapes, called by a child LayerSpec after its update.

Attributes

in_channels

The number of input channels.

input_shape

The number of elements in an input sequence.

max_input_length

The maximum number of finite elements in an input sequence.

min_input_length

The minimum number of finite elements in an input sequence.

out_channels

The number of output channels.

unfreezable

alias of Literal['all']

Non-Inherited Members

property out_channels: int

The number of output channels.

property in_channels: int

The number of input channels.

property input_shape: int

The number of elements in an input sequence.

property min_input_length: int

The minimum number of finite elements in an input sequence.

property max_input_length: int

The maximum number of finite elements in an input sequence.

out_len(length, mode='shape')

Calculates the number of elements in the output length dimension.

Parameters:
  • length (TypeVar(T, int, Tensor)) – The input length.

  • mode (Literal['min', 'max', 'shape']) – Either shape, which returns the number of elements, or min or max, which return the minimum or maximum number of finite elements.

Return type:

TypeVar(T, int, Tensor)

Returns:

The number of elements in the output length dimension, according to the specified mode.

in_len(length, mode='max')

Calculates the receptive field.

Parameters:
  • length (TypeVar(T, int, Tensor)) – The output length.

  • mode (Literal['min', 'max']) – Either min or max, representing the minimum or maximum number of positions contributing to the output length.

Return type:

Optional[TypeVar(T, int, Tensor)]

Returns:

The number of input positions that contribute to the values of the corresponding number of output positions. Outputs None if the max receptive field is undefined.

components()

Iterator of child components.

Return type:

Iterator[Component]

check_length_consistency()

Checks that input lengths of Binding components are consistent.

Raises:

RuntimeError – There is an input mismatch between components.

Return type:

None

update_binding_optim(binding_optim)

Updates a BindingOptim with the specification’s optimization steps.

Parameters:

binding_optim (BindingOptim) – The parent BindingOptim to be updated.

Return type:

BindingOptim

Returns:

The updated BindingOptim.

update_input_length(left_shift=0, right_shift=0, min_len_shift=0, max_len_shift=0, new_min_len=None, new_max_len=None)

Updates input shapes, called by a child LayerSpec after its update.

Parameters:
  • left_shift (int) – The change in size on the left side of the sequence.

  • right_shift (int) – The change in size on the right side of the sequence.

  • min_len_shift (int) – The change in the number of short input lengths.

  • max_len_shift (int) – The change in the number of long input lengths.

  • new_min_len (int | None) – The new min_input_length.

  • new_max_len (int | None) – The new max_input_length.

Return type:

None

lengths(seqs)

Counts the number of finite elements in each sequence.

Parameters:

seqs (Tensor) – A sequence tensor of shape \((\text{minibatch},\text{length})\) or \((\text{minibatch},\text{in_channels},\text{in_length})\).

Return type:

Tensor

Returns:

A tensor with the number of finite elements in each sequence of shape \((\text{minibatch},)\).