model package¶
Submodules¶
model.deeprm_model¶
Module: deeprm.model.deeprm_model This module defines the DeepRM model architecture, including the ResNet block, Transformer model, positional encoding, and regression head.
- class deeprm.model.deeprm_model.ResNetBlock(*args, **kwargs)[source]¶
Bases:
ModuleA 1D ResNet block for 1D DeepRM.
- Parameters:
in_channels (
int) – Number of input channels.out_channels (
int) – Number of output channels.hidden_channels (
int) – Number of hidden channels. If None, set to out_channels. (optional)kernel_size (
int) – Kernel size for the middle convolutional layer. Default is 3. (optional)stride (
int) – Stride for the convolutional layers. Default is 1. (optional)activation (
str) – Activation function to use. Default is ‘gelu’. (optional)dropout (
float) – Dropout rate. Default is 0.1. (optional)groups (
int) – Number of groups for grouped convolution. Default is 1. (optional)
- bn1¶
Batch normalization layer for the first convolution.
- Type:
- activation¶
Activation function.
- Type:
typing.Callable
- conv1¶
First convolutional layer with kernel size 1.
- Type:
- bn2¶
Batch normalization layer for the second convolution.
- Type:
- conv2¶
Second convolutional layer with specified kernel size and groups.
- Type:
- bn3¶
Batch normalization layer for the third convolution.
- Type:
- dropout¶
Dropout layer.
- Type:
- conv3¶
Third convolutional layer with kernel size 1.
- Type:
- shortcut¶
Shortcut connection to match input and output dimensions.
- Type:
- forward(x)[source]¶
Forward pass through the ResNet block.
- Parameters:
x (
torch.Tensor) – Input tensor of shape (batch_size, in_channels, sequence_length).- Returns:
Output tensor of shape (batch_size, out_channels, sequence_length).
- Return type:
- class deeprm.model.deeprm_model.TransformerModel(*args, **kwargs)[source]¶
Bases:
ModuleA Transformer model for DeepRM.
- Parameters:
d_model (
int) – Dimension of the model.n_heads (
int) – Number of attention heads.d_ff (
int) – Dimension of the feed-forward network.n_layers (
int) – Number of encoder layers.encoder_dropout (
float) – Dropout rate for the encoder. Default is 0.1. (optional)lin_dropout (
float) – Dropout rate for the linear layers. Default is 0.1. (optional)kmer_size (
int) – Size of the k-mer. Default is 5. (optional)signal_size (
int) – Size of the signal input. Default is 25. (optional)block_len (
int) – Length of the block. Default is 17. (optional)seq_len (
int) – Length of the sequence. Default is 200. (optional)t_act (
str) – Activation function for the transformer. Default is ‘gelu’. (optional)lin_act (
str) – Activation function for the linear layers. Default is ‘relu’. (optional)lin_depth (
int) – Depth of the linear layers. Default is 1. (optional)signal_stride (
int) – Stride for the signal input. Default is 6. (optional)**kwargs – Additional keyword arguments.
- kmer_embedding¶
Embedding layer for k-mer sequences.
- Type:
- signal_embedding¶
Linear layer for signal input.
- Type:
- pos_encoding¶
Positional encoding layer.
- Type:
- cnn_encoder¶
Sequential container for CNN encoder blocks.
- Type:
- transformer_encoder¶
Transformer encoder.
- regression_head¶
Regression head for the model output.
- Type:
- init_weights(initrange=0.1)[source]¶
Initialize the weights of the model.
- Parameters:
initrange (
float) – Range for uniform initialization of weights. Default is 0.1. (optional)- Returns:
None
- process_kmer(src_kmer, src_seg_len_flat)[source]¶
Process the k-mer input to convert nucleotide characters to numerical indices.
- Parameters:
src_kmer (
torch.Tensor) – Input tensor of shape (batch_size, seq_len) containing nucleotide characters.src_seg_len_flat (
torch.Tensor) – Flattened segment lengths for the input sequences.
- Returns:
Processed k-mer tensor of shape (batch_size, seq_len) with numerical indices.
- Return type:
- process_signal(src_signal)[source]¶
Process the signal input by unfolding it into segments based on the signal stride and k-mer size.
- Parameters:
src_signal (
torch.Tensor) – Input tensor of shape (batch_size, seq_len, signal_size) containing signal data.- Returns:
Processed signal tensor of shape (batch_size, new_seq_len, signal_size) after unfolding.
- Return type:
- flatten_seg_len(src_seg_len)[source]¶
Flatten the segment lengths to create a single dimension for each sequence.
- Parameters:
src_seg_len (
torch.Tensor) – Input tensor of shape (batch_size, num_segments) containing segment lengths.- Returns:
Flattened segment lengths of shape (batch_size, seq_len).
- Return type:
- create_src_pad_mask(src_signal, src_seg_len)[source]¶
Create a padding mask for the source signal to ignore padded values during processing.
- Parameters:
src_signal (
torch.Tensor) – Input tensor of shape (batch_size, seq_len, signal_size) containing signal data.src_seg_len (
torch.Tensor) – Segment lengths tensor of shape (batch_size, num_segments).
- Returns:
Padding mask of shape (batch_size, seq_len) where True indicates padded positions.
- Return type:
- create_target_mask(src_seg_len, src_seg_len_flat)[source]¶
Create a target mask to identify the target positions in the sequence.
- Parameters:
src_seg_len (
torch.Tensor) – Segment lengths tensor of shape (batch_size, num_segments).src_seg_len_flat (
torch.Tensor) – Flattened segment lengths tensor of shape (batch_size, seq_len).
- Returns:
Target mask of shape (batch_size, seq_len) where True indicates target positions.
- Return type:
- process_dwell_bq(src_dwell_bq, src_seg_len_flat)[source]¶
Process the dwell time and base quality input by flattening and repeating it based on segment lengths.
- Parameters:
src_dwell_bq (
torch.Tensor) – Input tensor of shape (batch_size, seq_len, channel) containing dwell time and base quality.src_seg_len_flat (
torch.Tensor) – Flattened segment lengths for the input sequences.
- Returns:
Processed dwell time and base quality tensor of shape (batch_size, seq_len, channel).
- Return type:
- forward(src_kmer, src_signal, src_seg_len, src_dwell_bq)[source]¶
Forward pass through the Transformer model.
- Parameters:
src_kmer (
torch.Tensor) – Input tensor of shape (batch_size, seq_len) containing k-mer sequences.src_signal (
torch.Tensor) – Input tensor of shape (batch_size, seq_len, signal_size) containing signal data.src_seg_len (
torch.Tensor) – Segment lengths tensor of shape (batch_size, num_segments).src_dwell_bq (
torch.Tensor) – Input tensor of shape (batch_size, seq_len, channel) containing dwell time and base quality.
- Returns:
Output tensor of shape (batch_size, seq_len) after processing through the model.
- Return type:
- class deeprm.model.deeprm_model.PositionalEncoding(*args, **kwargs)[source]¶
Bases:
ModulePositional Encoding for Transformer models.
- pe¶
Positional encoding tensor of shape (1, seq_len, d_model).
- Type:
- class deeprm.model.deeprm_model.RegressionHead(*args, **kwargs)[source]¶
Bases:
ModuleRegression head for the Transformer model.
- Parameters:
- lin_layers¶
Sequential container for the linear layers.
- Type:
- forward(x)[source]¶
Forward pass through the regression head.
- Parameters:
x (
torch.Tensor) – Input tensor of shape (batch_size, seq_length, d_model).- Returns:
Output tensor of shape (batch_size, seq_length, 1) after processing through the linear layers.
- Return type: