fusionlab.layers.selfattention.selfattention module#

class fusionlab.layers.selfattention.selfattention.SRAttention(dim, head, sr_ratio)[source]#

Bases: Module

Spatial Reduction Attention (SR-Attention) block, based on “Wang et al., Pyramid Vision Transformer: A Versatile Backbone for Dense Prediction without Convolutions <https://arxiv.org/abs/2102.12122v2>”

source code: sithu31296/semantic-segmentation

__init__(dim, head, sr_ratio)[source]#
Parameters:
  • dim (int) – input dimension

  • head (int) – number of attention heads

  • sr_ratio (int) – spatial reduction ratio

forward(x, H, W)[source]#

Defines 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.

Return type:

Tensor

training: bool#
class fusionlab.layers.selfattention.selfattention.SelfAttention(hidden_size, num_heads, dropout_rate=0.0, qkv_bias=False, save_attn=False)[source]#

Bases: Module

A self-attention block, based on: “Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale <https://arxiv.org/abs/2010.11929>”

source code: Project-MONAI/MONAI

__init__(hidden_size, num_heads, dropout_rate=0.0, qkv_bias=False, save_attn=False)[source]#
Parameters:
  • hidden_size (int) – dimension of hidden layer.

  • num_heads (int) – number of attention heads.

  • dropout_rate (float, optional) – faction of the input units to drop. Defaults to 0.0.

  • qkv_bias (bool, optional) – bias term for the qkv linear layer. Defaults to False.

  • save_attn (bool, optional) – to make accessible the attention matrix. Defaults to False.

forward(x)[source]#

Defines 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.

training: bool#