xbtorch.nn.nn

Custom neural network layers and utilities compatible with XBTorch.

This module provides: - Decorated versions of standard PyTorch layers for XBTorch compatibility. - A helper layer to extract the last time-step output from sequence models (RNN/LSTM). - Sum-of-squared-errors (SSE) loss function suitable for classification tasks.

Classes

  • Linear : XBTorch-compatible fully connected layer.

  • Conv2d : XBTorch-compatible 2D convolutional layer.

  • RNN : XBTorch-compatible RNN layer.

  • LSTM : XBTorch-compatible LSTM layer.

  • SelectLastStep : Layer that extracts the last time-step output from sequence models.

Functions

  • SSE(logits, label)() : Computes sum-of-squared-errors loss for one-hot classification.

Functions

SSE(logits, label)

Compute the sum-of-squared-errors (SSE) loss for one-hot classification tasks.

Classes

Conv2d(*args, **kwargs)

LSTM()

Linear(*args, **kwargs)

RNN()

SelectLastStep(*args, **kwargs)

Helper layer to select the output of the last time step from sequence models (RNNs/LSTMs).

class xbtorch.nn.nn.Conv2d(*args, **kwargs)[source]

Bases: Conv2d

forward(input)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class xbtorch.nn.nn.LSTM(input_size: int, hidden_size: int, num_layers: int = 1, bias: bool = True, batch_first: bool = False, dropout: float = 0.0, bidirectional: bool = False, proj_size: int = 0, device=None, dtype=None)[source]
class xbtorch.nn.nn.LSTM(*args, **kwargs)

Bases: LSTM

forward(input)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class xbtorch.nn.nn.Linear(*args, **kwargs)[source]

Bases: Linear

forward(input)

Runs the forward pass.

class xbtorch.nn.nn.RNN(input_size: int, hidden_size: int, num_layers: int = 1, nonlinearity: str = 'tanh', bias: bool = True, batch_first: bool = False, dropout: float = 0.0, bidirectional: bool = False, device=None, dtype=None)[source]
class xbtorch.nn.nn.RNN(*args, **kwargs)

Bases: RNN

forward(input)

Runs the forward pass.

xbtorch.nn.nn.SSE(logits, label)[source]

Compute the sum-of-squared-errors (SSE) loss for one-hot classification tasks.

Parameters:
  • logits (torch.Tensor) – Predicted outputs from the network. Shape: (batch_size, num_classes)

  • label (torch.Tensor) – Ground truth labels. Shape: (batch_size,)

Returns:

The scalar SSE loss.

Return type:

torch.Tensor

class xbtorch.nn.nn.SelectLastStep(*args, **kwargs)[source]

Bases: Module

Helper layer to select the output of the last time step from sequence models (RNNs/LSTMs).

Forward Input

xtuple

A tuple of (output, (h_n, c_n)) as returned by RNN/LSTM layers in PyTorch.

Forward Output

torch.Tensor

The tensor corresponding to the output of the last time step. Shape: (batch_size, hidden_size)

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.