Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • def392a
  • /
  • kernels
  • /
  • linears.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:40f233337dd4f6e74b0ae2d884b903856cb8c4a3
directory badge Iframe embedding
swh:1:dir:712d3755cf138dc276ef3dbfeebf02f2a872d6c0

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
linears.py
import tensorflow as tf
from ..base import Parameter, positive
from .base import Kernel


class Linear(Kernel):
    """
    The linear kernel.  Functions drawn from a GP with this kernel are linear, i.e. f(x) = cx.
    The kernel equation is

        k(x, y) = σ²xy

    where σ²  is the variance parameter.
    """

    def __init__(self, variance=1.0, active_dims=None, ard=None):
        """
        - input_dim is the dimension of the input to the kernel
        - variance is the (initial) value for the variance parameter(s)
          if ard=True, there is one variance per input
        - active_dims is a list of length input_dim which controls
          which columns of X are used.
        """
        super().__init__(active_dims)

        # variance, self.ard = self._validate_ard_shape("variance", variance, ard)
        self.ard = ard
        self.variance = Parameter(variance, transform=positive())

    def K(self, X, X2=None, presliced=False):
        if not presliced:
            X, X2 = self.slice(X, X2)

        if X2 is None:
            return tf.linalg.matmul(X * self.variance, X, transpose_b=True)

        return tf.linalg.matmul(X * self.variance, X2, transpose_b=True)

    def K_diag(self, X, presliced=False):
        if not presliced:
            X, _ = self.slice(X, None)
        return tf.reduce_sum(tf.square(X) * self.variance, 1)


class Polynomial(Linear):
    """
    The Polynomial kernel. Functions drawn from a GP with this kernel are
    polynomials of degree `d`. The kernel equation is

        k(x, y) = (σ²xy + γ)ᵈ

    where:
    σ² is the variance parameter,
    γ is the offset parameter,
    d is the degree parameter.
    """

    def __init__(self,
                 degree=3.0,
                 variance=1.0,
                 offset=1.0,
                 active_dims=None,
                 ard=None):
        """
        :param input_dim: the dimension of the input to the kernel
        :param variance: the (initial) value for the variance parameter(s)
                         if ard=True, there is one variance per input
        :param degree: the degree of the polynomial
        :param active_dims: a list of length input_dim which controls
                            which columns of X are used.
        :param ard: use variance as described
        """
        super().__init__(variance, active_dims, ard)
        self.degree = degree
        self.offset = Parameter(offset, transform=positive())

    def K(self, X, X2=None, presliced=False):
        return (super().K(X, X2, presliced=presliced) +
                self.offset)**self.degree

    def K_diag(self, X, presliced=False):
        return (super().K_diag(X, presliced=presliced) +
                self.offset)**self.degree

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API