nntoolbox.vision.components.attention module¶
-
class
nntoolbox.vision.components.attention.
SAGANAttention
(in_channels: int, reduction_ratio: int = 8)[source]¶ Bases:
torch.nn.modules.module.Module
Implement SAGAN attention module.
References:
Han Zhang, Ian Goodfellow, Dimitris Metaxas, Augustus Odena. “Self-Attention Generative Adversarial Networks.” https://arxiv.org/pdf/1805.08318.pdf
-
forward
(input: torch.Tensor) → torch.Tensor[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¶
-
-
class
nntoolbox.vision.components.attention.
StandAloneMultiheadAttention
(num_heads: int, in_channels: int, out_channels: int, kernel_size, stride=1, padding: int = 0, dilation: int = 1, bias: bool = True, padding_mode: str = 'zeros')[source]¶ Bases:
torch.nn.modules.module.Module
Stand-Alone Multihead Self-Attention for Vision Model
References:
Prajit Ramachandran, Niki Parmar, Ashish Vaswani, Irwan Bello, Anselm Levskaya, Jonathon Shlens. “Stand-Alone Self-Attention in Vision Models.” https://arxiv.org/pdf/1906.05909.pdf.
-
forward
(input: torch.Tensor) → torch.Tensor[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¶
-
-
class
nntoolbox.vision.components.attention.
StandAloneSelfAttention
(in_channels: int, out_channels: int, kernel_size, stride=1, padding: int = 0, dilation: int = 1, bias: bool = True, padding_mode: str = 'zeros')[source]¶ Bases:
torch.nn.modules.conv.Conv2d
A single head of Stand-Alone Self-Attention for Vision Model
References:
Prajit Ramachandran, Niki Parmar, Ashish Vaswani, Irwan Bello, Anselm Levskaya, Jonathon Shlens. “Stand-Alone Self-Attention in Vision Models.” https://arxiv.org/pdf/1906.05909.pdf.
-
bias
: Optional[torch.Tensor]¶
-
dilation
: Tuple[int, …]¶
-
forward
(input: torch.Tensor) → torch.Tensor[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.
-
groups
: int¶
-
kernel_size
: Tuple[int, …]¶
-
out_channels
: int¶
-
output_padding
: Tuple[int, …]¶
-
padding
: Tuple[int, …]¶
-
padding_mode
: str¶
-
stride
: Tuple[int, …]¶
-
to
(*args, **kwargs)[source]¶ Moves and/or casts the parameters and buffers.
This can be called as
Its signature is similar to
torch.Tensor.to()
, but only accepts floating point desireddtype
s. In addition, this method will only cast the floating point parameters and buffers todtype
(if given). The integral parameters and buffers will be moveddevice
, if that is given, but with dtypes unchanged. Whennon_blocking
is set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Args:
- device (
torch.device
): the desired device of the parameters and buffers in this module
- dtype (
torch.dtype
): the desired floating point type of the floating point parameters and buffers in this module
- tensor (torch.Tensor): Tensor whose dtype and device are the desired
dtype and device for all parameters and buffers in this module
- memory_format (
torch.memory_format
): the desired memory format for 4D parameters and buffers in this module (keyword only argument)
- device (
- Returns:
Module: self
Example:
>>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16)
-
transposed
: bool¶
-
weight
: torch.Tensor¶
-