timesead.models.layers.same_pad
Classes
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Pads the input tensor boundaries with zero. |
Functions
|
|
|
Module Contents
- timesead.models.layers.same_pad.calc_causal_same_pad(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1) int
- timesead.models.layers.same_pad.calc_same_pad(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1) Tuple[int, int]
- class timesead.models.layers.same_pad.SameZeroPad1d(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1)
Bases:
torch.nn.ModuleBase class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will also have their parameters converted when you call
to(), etc.Note
As per the example above, an
__init__()call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- Parameters:
Replicates the “SAME” pad algorithm from Tensorflow. Note that Tensorflow will always assume stride = 1, whereas this implementation also takes different strides into account.
- Parameters:
- padding
- forward(x: torch.Tensor) torch.Tensor
- Parameters:
x (torch.Tensor)
- Return type:
- class timesead.models.layers.same_pad.SameCausalZeroPad1d(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1)
Bases:
SameZeroPad1dBase class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will also have their parameters converted when you call
to(), etc.Note
As per the example above, an
__init__()call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- Parameters:
Replicates the “causal” pad algorithm from Tensorflow. Note that Tensorflow will always assume stride = 1, whereas this implementation also takes different strides into account.
- Parameters:
- padding
- class timesead.models.layers.same_pad.SameZeroPad2d(kernel_size: torch.nn.common_types._size_2_t, stride: torch.nn.common_types._size_2_t = 1, in_shape: torch.nn.common_types._size_2_t = 1, dilation: torch.nn.common_types._size_2_t = 1)
Bases:
torch.nn.ZeroPad2dPads the input tensor boundaries with zero.
For N-dimensional padding, use
torch.nn.functional.pad().- Parameters:
padding (int, tuple) – the size of the padding. If is int, uses the same padding in all boundaries. If a 4-tuple, uses (\(\text{padding\_left}\), \(\text{padding\_right}\), \(\text{padding\_top}\), \(\text{padding\_bottom}\))
kernel_size (torch.nn.common_types._size_2_t)
stride (torch.nn.common_types._size_2_t)
in_shape (torch.nn.common_types._size_2_t)
dilation (torch.nn.common_types._size_2_t)
- Shape:
Input: \((N, C, H_{in}, W_{in})\) or \((C, H_{in}, W_{in})\).
Output: \((N, C, H_{out}, W_{out})\) or \((C, H_{out}, W_{out})\), where
\(H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}\)
\(W_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}\)
Examples:
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> m = nn.ZeroPad2d(2) >>> input = torch.randn(1, 1, 3, 3) >>> input tensor([[[[-0.1678, -0.4418, 1.9466], [ 0.9604, -0.4219, -0.5241], [-0.9162, -0.5436, -0.6446]]]]) >>> m(input) tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, -0.1678, -0.4418, 1.9466, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.9604, -0.4219, -0.5241, 0.0000, 0.0000], [ 0.0000, 0.0000, -0.9162, -0.5436, -0.6446, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]]) >>> # using different paddings for different sides >>> m = nn.ZeroPad2d((1, 1, 2, 0)) >>> m(input) tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [ 0.0000, -0.1678, -0.4418, 1.9466, 0.0000], [ 0.0000, 0.9604, -0.4219, -0.5241, 0.0000], [ 0.0000, -0.9162, -0.5436, -0.6446, 0.0000]]]])
Replicates the “SAME” pad algorithm from Tensorflow. Note that Tensorflow will always assume stride = 1, whereas this implementation also takes different strides into account.
- Parameters:
kernel_size (torch.nn.common_types._size_2_t) – Kernel size that will be used
stride (torch.nn.common_types._size_2_t) – Stride that will be used
in_shape (torch.nn.common_types._size_2_t) – Size of the input. This is only needed if stride != 1
dilation (torch.nn.common_types._size_2_t) – Dilation that will be used