pyprobound.mode.Mode

class Mode(layers, train_hill=False, name='')

Bases: Binding, LengthManager

Scores sequences with a series of layers applied sequentially.

\[\frac{1}{K^{rel}_{\text{D}, a} (S_i)} = \sum_x \frac{1}{K^{rel}_{\text{D}, a} (S_{i, x})}\]
layers

The layers to applied sequentially to an input sequence.

Type:

TModuleList[Layer]

log_hill

The Hill coeffient in log space.

Type:

Tensor

__init__(layers, train_hill=False, name='')

Initializes the binding mode.

Parameters:
  • layers (Iterable[Layer]) – The layers to be applied sequentially to an input sequence.

  • train_hill (bool) – Whether to train a Hill coefficient.

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

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.

expected_log_score()

Calculates the expected log score.

expected_sequence()

Uninformative prior of input, used for calculating expectations.

forward(seqs)

Calculates the log score of each sequence.

freeze()

Turns off gradient calculation for all parameters.

from_nonspecific(nonspecific, prev[, ...])

Creates a new instance from a specification and an input component.

from_psam(psam, prev[, train_posbias, ...])

Creates a new instance from a PSAM and an input component.

in_len(length[, mode])

Calculates the receptive field.

key()

The specification of a Binding component.

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.

score_windows(seqs)

Calculates the log score of each window before summing over them.

unfreeze([parameter])

Turns on gradient calculation for the specified parameter.

update_read_length([left_shift, ...])

Updates the input shape as part of a flank 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', 'hill']

Non-Inherited Members

unfreezable

alias of Literal[‘all’, ‘hill’]

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.

classmethod from_psam(psam, prev, train_posbias=False, bias_mode='channel', bias_bin=1, length_specific_bias=True, out_channel_indexing=None, one_hot=False, unfold=False, normalize=False, train_hill=False, name='')

Creates a new instance from a PSAM and an input component.

Parameters:
  • psam (PSAM) – The specification of the 1d convolution layer.

  • prev (Union[Table[Any], Layer]) – If used as the first layer, the table that will be passed as an input; otherwise, the layer that precedes it.

  • train_posbias (bool) – Whether to train a bias \(\omega(x)\) for each output position and channel.

  • bias_mode (Literal['channel', 'same', 'reverse']) – Whether to train a separate bias for each output channel, use the same bias across all output channels, or (if score_reverse) flip it for the reverse output channels.

  • bias_bin (int) – Applies the constraint \(\omega(x_{i\times\text{bias_bin}}) = \cdots = \omega(x_{(i+1)\times\text{bias_bin}-1})\).

  • length_specific_bias (bool) – Whether to train a separate bias parameter for each input length.

  • out_channel_indexing (Sequence[int] | None) – Output channel indexing, equivalent to Conv1d(seqs)[:,out_channel_indexing].

  • one_hot (bool) – Whether to use one-hot scoring instead of dense.

  • unfold (bool) – Whether to score using unfold or conv1d (if one_hot).

  • normalize (bool) – Whether to mean-center log_posbias over all windows.

  • train_hill (bool) – Whether to train a Hill coefficient.

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

Return type:

Self

classmethod from_nonspecific(nonspecific, prev, train_posbias=False, name='')

Creates a new instance from a specification and an input component.

Parameters:
  • spec – The specification of the 0d convolution layer.

  • prev (Union[Table[Any], Layer]) – If used as the first layer, the table that will be passed as an input; otherwise, the layer that precedes it.

  • train_posbias (bool) – Whether to train a bias for each input length.

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

Return type:

Self

key()

The specification of a Binding component.

All Binding components with the same specification will be optimized together in the sequential optimization procedure.

Return type:

ModeKey

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[Layer]

check_length_consistency()

Checks that input lengths of Binding components are consistent.

Raises:

RuntimeError – There is an input mismatch between components.

Return type:

None

unfreeze(parameter='all')

Turns on gradient calculation for the specified parameter.

Parameters:

parameter (unfreezable) – Parameter to be unfrozen, defaults to all parameters.

Return type:

None

optim_procedure(ancestry=None, current_order=None)

The sequential optimization procedure for all Binding components.

The optimization procedure is generated recursively through iteration over the child components of each module. All Binding components with the same specification returned from key() are trained jointly.

Parameters:
  • ancestry (tuple[Component, ...] | None) – The parent components from the root for which the procedure is being generated to the current component.

  • current_order (dict[tuple[Spec, ...], BindingOptim] | None) – Mapping of Binding component specifications to the sequential optimization procedure for those Binding components.

Return type:

dict[tuple[Spec, ...], BindingOptim]

Returns:

The current_order updated with the optimization of the current component’s children.

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

Updates the input shape as part of a flank 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

expected_sequence()

Uninformative prior of input, used for calculating expectations.

Return type:

Tensor

score_windows(seqs)

Calculates the log score of each window before summing over them.

\[\log \frac{1}{K^{rel}_{\text{D}, a} (S_{i, x})}\]
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 log score of each window of shape \((\text{minibatch},\text{out_channels},\text{out_length})\).

forward(seqs)

Calculates the log score of each sequence.

\[\log \frac{1}{K^{rel}_{\text{D}, a} (S_i)} = \log \sum_x \frac{1}{K^{rel}_{\text{D}, a} (S_{i, x})}\]
Parameters:

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

Return type:

Tensor

Returns:

The log score tensor of shape \((\text{minibatch},)\).