Layers
CustomPeakEncoder(dim_model, dim_intensity=None, min_wavelength=0.001, max_wavelength=10000, partial_encode=1.0)
Bases: MassEncoder
Encode m/z values in a mass spectrum using sine and cosine waves.
Parameters
dim_model : int The number of features to output. dim_intensity : int, optional The number of features to use for intensity. The remaining features will be used to encode the m/z values. min_wavelength : float, optional The minimum wavelength to use. max_wavelength : float, optional The maximum wavelength to use.
Initialize the MzEncoder.
Source code in instanovo/diffusion/layers.py
forward(x, mass, precursor_mass)
Encode m/z values and intensities.
Note that we expect intensities to fall within the interval [0, 1].
Parameters
x : torch.Tensor of shape (n_spectra, n_peaks, 2) The spectra to embed. Axis 0 represents a mass spectrum, axis 1 contains the peaks in the mass spectrum, and axis 2 is essentially a 2-tuple specifying the m/z-intensity pair for each peak. These should be zero-padded, such that all of the spectra in the batch are the same length. mass : optional torch.Tensor of shape (n_spectra, ) The mass of the sequence decoded so far precursor_mass : optional torch.Tensor of shape (n_spectra, ) The mass of the parent ion
Returns
torch.Tensor of shape (n_spectr, n_peaks, dim_model) The encoded features for the mass spectra.
Source code in instanovo/diffusion/layers.py
CustomSpectrumEncoder(dim_model=128, n_head=8, dim_feedforward=1024, n_layers=1, dropout=0.0, peak_encoder=True, dim_intensity=None, mass_encoding='linear')
Bases: SpectrumEncoder
A Transformer encoder for input mass spectra.
Parameters
dim_model : int, optional
The latent dimensionality to represent peaks in the mass spectrum.
n_head : int, optional
The number of attention heads in each layer. dim_model
must be
divisible by n_head
.
dim_feedforward : int, optional
The dimensionality of the fully connected layers in the Transformer
layers of the model.
n_layers : int, optional
The number of Transformer layers.
dropout : float, optional
The dropout probability for all layers.
peak_encoder : bool, optional
Use positional encodings m/z values of each peak.
dim_intensity: int or None, optional
The number of features to use for encoding peak intensity.
The remaining (dim_model - dim_intensity
) are reserved for
encoding the m/z value.
Initialize a CustomSpectrumEncoder.
Source code in instanovo/diffusion/layers.py
forward(spectra, spectra_padding_mask=None, mass=None, precursor_mass=None)
The forward pass.
Parameters
spectra : torch.Tensor of shape (n_spectra, n_peaks, 2) The spectra to embed. Axis 0 represents a mass spectrum, axis 1 contains the peaks in the mass spectrum, and axis 2 is essentially a 2-tuple specifying the m/z-intensity pair for each peak. These should be zero-padded, such that all of the spectra in the batch are the same length. mass : torch.Tensor of shape (n_spectra, ) The mass of the sequence decoded so far precursor_mass : torch.Tensor of shape (n_spectra, ) The mass of the parent ion
Returns
latent : torch.Tensor of shape (n_spectra, n_peaks + 1, dim_model) The latent representations for the spectrum and each of its peaks. mem_mask : torch.Tensor The memory mask specifying which elements were padding in X.
Source code in instanovo/diffusion/layers.py
LocalisedEncoderLayer(d_model, nhead, dim_feedforward=2048, dropout=0.1, activation=F.relu, layer_norm_eps=1e-05, batch_first=False, norm_first=False, mass_encoding='linear', device=None, dtype=None)
Bases: TransformerEncoderLayer
Layer in a localised transformer.
Source code in instanovo/diffusion/layers.py
forward(src, mass=None, src_mask=None, src_key_padding_mask=None)
Compute localised transformer encoding for one layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src |
Tensor
|
The source tensor. |
required |
mass |
Tensor | None
|
Masses of the batch. Defaults to None. |
None
|
src_mask |
Tensor | None
|
The source self-attention mask. Defaults to None. |
None
|
src_key_padding_mask |
Tensor | None
|
The source padding mask. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The encoding representation for one layer. |
Source code in instanovo/diffusion/layers.py
LocalisedEncoding(d_model, casanovo_style=False, window_size=100, min_wavelength=0.001, device=None)
Bases: Module
LocalisedEncoding module.
Custom localised positional encoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d_model |
int
|
|
required |
window_size |
int
|
Defaults to 100. |
100
|
min_wavelength |
float
|
Defaults to 0.001. |
0.001
|
Source code in instanovo/diffusion/layers.py
forward(mass)
Defines the computation performed at every call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mass |
Tensor
|
shape [batch_size, seq_len, 1] |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tensor |
Source code in instanovo/diffusion/layers.py
LocalisedSpectrumEncoder(dim_model=128, n_head=8, dim_feedforward=1024, n_layers=1, dropout=0, peak_encoder=True, dim_intensity=None, window_size=400, device=None, mass_encoding='linear')
Bases: Module
A Transformer encoder for input mass spectra.
Parameters
dim_model : int, optional
The latent dimensionality to represent peaks in the mass spectrum.
n_head : int, optional
The number of attention heads in each layer. dim_model
must be
divisible by n_head
.
dim_feedforward : int, optional
The dimensionality of the fully connected layers in the Transformer
layers of the model.
n_layers : int, optional
The number of Transformer layers.
dropout : float, optional
The dropout probability for all layers.
peak_encoder : bool, optional
Use positional encodings m/z values of each peak.
dim_intensity: int or None, optional
The number of features to use for encoding peak intensity.
The remaining (dim_model - dim_intensity
) are reserved for
encoding the m/z value.
Initialize a SpectrumEncoder.
Source code in instanovo/diffusion/layers.py
device: str
property
The current device for the model.
forward(spectra, spectra_padding_mask=None)
The forward pass.
Parameters
spectra : torch.Tensor of shape (n_spectra, n_peaks, 2) The spectra to embed. Axis 0 represents a mass spectrum, axis 1 contains the peaks in the mass spectrum, and axis 2 is essentially a 2-tuple specifying the m/z-intensity pair for each peak. These should be zero-padded, such that all of the spectra in the batch are the same length.
Returns
latent : torch.Tensor of shape (n_spectra, n_peaks + 1, dim_model) The latent representations for the spectrum and each of its peaks. mem_mask : torch.Tensor The memory mask specifying which elements were padding in X.
Source code in instanovo/diffusion/layers.py
LocalisedTransformerEncoder
Bases: TransformerEncoder
Localised transformer encoder.
forward(src, mass=None, mask=None, src_key_padding_mask=None)
Compute representations using localised transformer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src |
Tensor
|
The source tensor. |
required |
mass |
Tensor | None
|
Masses of the batch. Defaults to None. |
None
|
mask |
Tensor | None
|
The self-attention mask. Defaults to None. |
None
|
src_key_padding_mask |
Tensor | None
|
The padding mask for th source sequence. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The encoding representation. |