Skip to content

cnn

CNN with residual blocks.

Classes:

Name Description
ResidualBlock1D

Residual block with 1D convolutions.

ResidualBlock2D

Residual block with 2D convolutions.

ResidualCNN1D

Cnn with 1D residual blocks.

ResidualCNN2D

Cnn with 2D residual blocks.

ResidualBlock1D(in_channels: int, out_channels: int, stride: int = 1, kernel_size: int = 3, padding: int = 1) #

Residual block with 1D convolutions.

Init residual block.

Methods:

Name Description
forward

Forward function.

Attributes:

Name Type Description
conv1
conv2
cast_layer
relu
Source code in src/xpdeep/model/zoo/cnn.py
def __init__(self, in_channels: int, out_channels: int, stride: int = 1, kernel_size: int = 3, padding: int = 1):
    """Init residual block."""
    super().__init__()
    self.conv1 = nn.Sequential(
        nn.Conv1d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding), nn.ReLU()
    )
    self.conv2 = nn.Sequential(
        nn.Conv1d(out_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding),
    )

    self.cast_layer = nn.Linear(in_channels, out_channels)
    self.relu = nn.ReLU()

conv1 = nn.Sequential(nn.Conv1d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding), nn.ReLU()) #

conv2 = nn.Sequential(nn.Conv1d(out_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding)) #

cast_layer = nn.Linear(in_channels, out_channels) #

relu = nn.ReLU() #

forward(x: torch.Tensor) -> torch.Tensor #

Forward function.

Source code in src/xpdeep/model/zoo/cnn.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """Forward function."""
    # cast number of channels to number of out channels.
    # It should be called as a CNN, with channels in 2nd dimension
    residual = self.cast_layer(x.transpose(1, -1)).transpose(1, -1)
    out = self.conv1(x)
    out = self.conv2(out)
    out += residual
    return self.relu(out)

ResidualBlock2D(in_channels: int, out_channels: int, stride: int | tuple[int, int] = 1, kernel_size: int | tuple[int, int] = 3, padding: int | tuple[int, int] = 1) #

Residual block with 2D convolutions.

Init residual block.

Attributes:

Name Type Description
conv1
conv2
Source code in src/xpdeep/model/zoo/cnn.py
def __init__(
    self,
    in_channels: int,
    out_channels: int,
    stride: int | tuple[int, int] = 1,
    kernel_size: int | tuple[int, int] = 3,
    padding: int | tuple[int, int] = 1,
):
    """Init residual block."""
    super().__init__(in_channels=in_channels, out_channels=out_channels)
    self.conv1 = nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding), nn.ReLU()
    )
    self.conv2 = nn.Sequential(
        nn.Conv2d(out_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding)
    )

conv1 = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding), nn.ReLU()) #

conv2 = nn.Sequential(nn.Conv2d(out_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding)) #

ResidualCNN1D(in_channels: int, *, dropout: float = 0.0, out_channels: tuple[int, ...] = (32, 64, 128), max_pool_size: int = 2, output_size: int | None = 50, with_softmax: bool = False, transpose_input: bool = False) #

Cnn with 1D residual blocks.

Initialize a basic CNN model for classification.

Methods:

Name Description
forward

Forward pass.

Attributes:

Name Type Description
cnn
mlp Sequential | None
transpose_input
Source code in src/xpdeep/model/zoo/cnn.py
def __init__(  # noqa: PLR0913=7
    self,
    in_channels: int,
    *,
    dropout: float = 0.0,
    out_channels: tuple[int, ...] = (32, 64, 128),
    max_pool_size: int = 2,
    output_size: int | None = 50,
    with_softmax: bool = False,
    transpose_input: bool = False,
):
    """Initialize a basic CNN model for classification."""
    super().__init__()

    layers = []
    for channel_dim in out_channels:
        layers.extend([
            ResidualBlock1D(in_channels=in_channels, out_channels=channel_dim),
            nn.MaxPool1d(max_pool_size),
            nn.Dropout(p=dropout),
        ])
        in_channels = channel_dim
    self.cnn = nn.Sequential(*layers)

    self.mlp: nn.Sequential | None = None

    if output_size is not None:
        mlp_layers = [nn.Flatten(), nn.LazyLinear(output_size)]

        if with_softmax:
            mlp_layers.append(nn.Softmax(dim=-1))
        self.mlp = nn.Sequential(*mlp_layers)

    self.transpose_input = transpose_input

cnn = nn.Sequential(*layers) #

mlp: nn.Sequential | None = None #

transpose_input = transpose_input #

forward(x: Tensor) -> Tensor #

Forward pass.

Source code in src/xpdeep/model/zoo/cnn.py
def forward(self, x: Tensor) -> Tensor:
    """Forward pass."""
    if hasattr(self, "transpose_input") and self.transpose_input:  # retro compatibility
        features = self.cnn(x.permute(*(0, x.dim() - 1, *range(1, x.dim() - 1))))
    else:
        features = self.cnn(x)
    if self.mlp is not None:
        return self.mlp(features)
    return features

ResidualCNN2D(in_channels: int, *, dropout: float = 0.0, out_channels: tuple[int, ...] = (32, 64, 128), max_pool_size: int | tuple[int, ...] = 2, output_size: int | None = 50, with_softmax: bool = False) #

Cnn with 2D residual blocks.

Attributes:

Name Type Description
cnn
Source code in src/xpdeep/model/zoo/cnn.py
def __init__(  # noqa: PLR0913=6
    self,
    in_channels: int,
    *,
    dropout: float = 0.0,
    out_channels: tuple[int, ...] = (32, 64, 128),
    max_pool_size: int | tuple[int, ...] = 2,
    output_size: int | None = 50,
    with_softmax: bool = False,
):
    super().__init__(output_size=output_size, with_softmax=with_softmax, in_channels=in_channels)

    layers = []
    for channel_dim in out_channels:
        layers.extend([
            ResidualBlock2D(in_channels=in_channels, out_channels=channel_dim),
            nn.MaxPool2d(max_pool_size),
            nn.Dropout(p=dropout),
        ])
        in_channels = channel_dim

    # overwrite 1d cnn with 2d cnn
    self.cnn = nn.Sequential(*layers)

cnn = nn.Sequential(*layers) #