timesead.models.layers.same_pad =============================== .. py:module:: timesead.models.layers.same_pad Classes ------- .. autoapisummary:: timesead.models.layers.same_pad.SameZeroPad1d timesead.models.layers.same_pad.SameCausalZeroPad1d timesead.models.layers.same_pad.SameZeroPad2d Functions --------- .. autoapisummary:: timesead.models.layers.same_pad.calc_causal_same_pad timesead.models.layers.same_pad.calc_same_pad Module Contents --------------- .. py:function:: calc_causal_same_pad(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1) -> int .. py:function:: calc_same_pad(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1) -> Tuple[int, int] .. py:class:: SameZeroPad1d(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1) Bases: :py:obj:`torch.nn.Module` Base 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 :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Replicates the "SAME" pad algorithm from Tensorflow. Note that Tensorflow will always assume stride = 1, whereas this implementation also takes different strides into account. :param kernel_size: Kernel size that will be used :param stride: Stride that will be used :param in_shape: Size of the input. This is only needed if stride != 1 :param dilation: Dilation that will be used .. py:attribute:: padding .. py:method:: forward(x: torch.Tensor) -> torch.Tensor .. py:class:: SameCausalZeroPad1d(kernel_size: int, stride: int = 1, in_shape: int = 1, dilation: int = 1) Bases: :py:obj:`SameZeroPad1d` Base 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 :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Replicates the "causal" pad algorithm from Tensorflow. Note that Tensorflow will always assume stride = 1, whereas this implementation also takes different strides into account. :param kernel_size: Kernel size that will be used :param stride: Stride that will be used :param in_shape: Size of the input. This is only needed if stride != 1 :param dilation: Dilation that will be used .. py:attribute:: padding .. py:class:: 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: :py:obj:`torch.nn.ZeroPad2d` Pads the input tensor boundaries with zero. For `N`-dimensional padding, use :func:`torch.nn.functional.pad()`. :param padding: the size of the padding. If is `int`, uses the same padding in all boundaries. If a 4-`tuple`, uses (:math:`\text{padding\_left}`, :math:`\text{padding\_right}`, :math:`\text{padding\_top}`, :math:`\text{padding\_bottom}`) :type padding: int, tuple Shape: - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`. - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where :math:`H_{out} = H_{in} + \text{padding\_top} + \text{padding\_bottom}` :math:`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. :param kernel_size: Kernel size that will be used :param stride: Stride that will be used :param in_shape: Size of the input. This is only needed if stride != 1 :param dilation: Dilation that will be used