Example #1
0
    def __init__(self, manifold, sigma, label):
        super().__init__(manifold, sigma, label)

        self.__keops_dtype = str(manifold.gd.dtype).split(".")[1]
        self.__keops_backend = 'CPU'
        if str(self.device) != 'cpu':
            self.__keops_backend = 'GPU'

        self.__keops_invsigmasq = torch.tensor([1. / sigma / sigma],
                                               dtype=manifold.gd.dtype,
                                               device=manifold.device)

        formula_cost = "(Exp(-S*SqNorm2(x - y)/IntCst(2))*px | py)/IntCst(2)"
        alias_cost = [
            "x=Vi(" + str(self.dim) + ")", "y=Vj(" + str(self.dim) + ")",
            "px=Vi(" + str(self.dim) + ")", "py=Vj(" + str(self.dim) + ")",
            "S=Pm(1)"
        ]
        self.reduction_cost = Genred(formula_cost,
                                     alias_cost,
                                     reduction_op='Sum',
                                     axis=0,
                                     dtype=self.__keops_dtype)

        formula_cgc = "Exp(-S*SqNorm2(x - y)/IntCst(2))*X"
        alias_cgc = [
            "x=Vi(" + str(self.dim) + ")", "y=Vj(" + str(self.dim) + ")",
            "X=Vj(" + str(self.dim) + ")", "S=Pm(1)"
        ]
        self.solve_cgc = KernelSolve(formula_cgc,
                                     alias_cgc,
                                     "X",
                                     axis=1,
                                     dtype=self.__keops_dtype)
Example #2
0
    def __init__(self, manifold, sigma, C, nu, coeff, label):
        super().__init__(manifold, sigma, C, nu, coeff, label)

        self.__keops_dtype = str(manifold.gd[0].dtype).split(".")[1]
        self.__keops_backend = 'CPU'
        if str(self.device) != 'cpu':
            self.__keops_backend = 'GPU'

        self.__keops_invsigmasq = torch.tensor([1. / sigma / sigma],
                                               dtype=manifold.dtype,
                                               device=self.device)
        self.__keops_eye = torch.eye(self.dim,
                                     device=self.device,
                                     dtype=manifold.dtype).flatten()

        self.__keops_A = A(self.dim, device=self.device,
                           dtype=manifold.dtype).flatten()

        formula_solve_sks = "TensorDot(TensorDot((-S*Exp(-S*SqNorm2(x_i - y_j)*IntInv(2))*(S*TensorDot(x_i - y_j, x_i - y_j, Ind({dim}), Ind({dim}), Ind(), Ind()) - eye)), A, Ind({dim}, {dim}), Ind({dim}, {dim}, {symdim}, {symdim}), Ind(0, 1), Ind(0, 1)), X, Ind({symdim}, {symdim}), Ind({symdim}), Ind(0), Ind(0))".format(
            dim=self.dim, symdim=self.sym_dim)

        alias_solve_sks = [
            "x_i=Vi({dim})".format(dim=self.dim),
            "y_j=Vj({dim})".format(dim=self.dim),
            "X=Vj({symdim})".format(symdim=self.sym_dim),
            "eye=Pm({dimsq})".format(dimsq=self.dim * self.dim), "S=Pm(1)",
            "A=Pm({dima})".format(dima=self.__keops_A.numel())
        ]

        self.solve_sks = KernelSolve(formula_solve_sks,
                                     alias_solve_sks,
                                     "X",
                                     axis=1,
                                     dtype=self.__keops_dtype)

        self.eps = 1e-6
#

formula = 'Exp(- g * SqDist(x,y)) * b'
aliases = [
    'x = Vi(' + str(D) + ')',  # First arg:  i-variable of size D
    'y = Vj(' + str(D) + ')',  # Second arg: j-variable of size D
    'b = Vj(' + str(Dv) + ')',  # Third arg:  j-variable of size Dv
    'g = Pm(1)'
]  # Fourth arg: scalar parameter

###############################################################################
# Define the inverse kernel operation, with a ridge regularization **alpha**:
#

alpha = 0.01
Kinv = KernelSolve(formula, aliases, "b", axis=1)

###############################################################################
# .. note::
#   This operator uses a conjugate gradient solver and assumes
#   that **formula** defines a **symmetric**, positive and definite
#   **linear** reduction with respect to the alias ``"b"``
#   specified trough the third argument.
#
# Apply our solver on arbitrary point clouds:
#

print(
    "Solving a Gaussian linear system, with {} points in dimension {}.".format(
        N, D))
sync()
Example #4
0
def Kinv_keops(x, b, gamma, alpha):
    Kinv = KernelSolve(formula, aliases, "a", axis=1)
    res = Kinv(x, x, b, gamma, alpha=alpha)
    return res