timesead.models.layers.kervolution ================================== .. py:module:: timesead.models.layers.kervolution Attributes ---------- .. autoapisummary:: timesead.models.layers.kervolution.kernels Classes ------- .. autoapisummary:: timesead.models.layers.kervolution.Kernel timesead.models.layers.kervolution.LinearKernel timesead.models.layers.kervolution.PolynomialKernel timesead.models.layers.kervolution.RBFKernel timesead.models.layers.kervolution.Kerv1d Module Contents --------------- .. py:class:: Kernel(learnable_parameters: bool = False) Bases: :py:obj:`torch.nn.Module`, :py:obj:`abc.ABC` 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 Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: learnable_parameters :value: False .. py:method:: forward(x1: torch.Tensor, x2: torch.Tensor, diag_only: bool = False) -> torch.Tensor Compute the kernel function for inputs x1 and x2 :param x1: A tensor of shape ([B1], D) :param x2: A tensor of shape ([B2], D) :param diag_only: Whether the entire kernel matrix should be computed or only the diagonal :return: A tensor of shape ([B1] + [B2]) if diag_only = False, else a tensor of shape ([B]), where [B] is the result of broadcasting [B1] and [B2]. .. py:class:: LinearKernel(learnable_parameters: bool = False) Bases: :py:obj:`Kernel` 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 Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:class:: PolynomialKernel(degree: int = 2, c0: float = 0.0, learnable_parameters: bool = False) Bases: :py:obj:`Kernel` 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 Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: degree :value: 2 .. py:attribute:: c0 :value: 0.0 .. py:class:: RBFKernel(gamma: float = 1.0, learnable_parameters: bool = False) Bases: :py:obj:`Kernel` 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 Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: gamma :value: 1.0 .. py:data:: kernels .. py:class:: Kerv1d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = True, padding_mode: str = 'zeros', kernel: Union[str, Kernel] = 'linear', learnable_kernel: bool = False) Bases: :py:obj:`torch.nn.Conv1d` Applies a 1D kervolution over an input signal composed of several inputplanes. :param in_channels: Number of channels in the input image :type in_channels: int :param out_channels: Number of channels produced by the convolution :type out_channels: int :param kernel_size: Size of the convolving kernel :type kernel_size: int or tuple :param stride: Stride of the convolution. Default: 1 :type stride: int or tuple, optional :param padding: Zero-padding added to both sides of the input. Default: 0 :type padding: int or tuple, optional :param padding_mode: `zeros` :type padding_mode: string, optional :param dilation: Spacing between kernel elements. Default: 1 :type dilation: int or tuple, optional :param groups: Number of blocked connections from input channels to output channels. Default: 1 :type groups: int, optional :param bias: If ``True``, adds a learnable bias to the output. Default: ``True`` :type bias: bool, optional :param kernel: 'linear' :type kernel: str or Kernel :param learnable_kernel: Learnable kernel parameters. Default: False :type learnable_kernel: bool Shape: - Input: :math:`(N, C_{in}, L_{in})` - Output: :math:`(N, C_{out}, L_{out})` where .. math:: L_{out} = \left\lfloor\frac{L_{in} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel_size} - 1) - 1}{\text{stride}} + 1\right\rfloor .. rubric:: Examples >>> m = Kerv1d(16, 33, 3, kernel='rbf') >>> input = torch.randn(20, 16, 70) >>> output = m(input) Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: unfold .. py:attribute:: kernel :value: 'linear' .. py:method:: forward(x: torch.Tensor) -> torch.Tensor