{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# KernelSolve reduction (with LazyTensors)\n\nLet's see how to solve discrete deconvolution problems\nusing the **conjugate gradient solver** provided by\nthe :meth:`pykeops.numpy.LazyTensor.solve` method of KeOps :class:`pykeops.numpy.LazyTensor`.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n\nStandard imports:\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import time\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom pykeops.numpy import Vi, Vj, Pm\nimport pykeops.config" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define our dataset:\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "N = 5000 if pykeops.config.gpu_available else 500 # Number of points\nD = 2 # Dimension of the ambient space\nDv = 2 # Dimension of the vectors (= number of linear problems to solve)\nsigma = 0.1 # Radius of our RBF kernel\n\nx = np.random.rand(N, D)\nb = np.random.rand(N, Dv)\ng = np.array([0.5 / sigma**2]) # Parameter of the Gaussian RBF kernel\n\nalpha = 0.01" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## KeOps internal conjugate gradient solver\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"Solving a Gaussian linear system, with {} points in dimension {}.\".format(N, D))\nstart = time.time()\nKxx = (-Pm(g) * Vi(x).sqdist(Vj(x))).exp()\nc = Kxx.solve(Vi(b), alpha=alpha)\nend = time.time()\nprint(\"Timing (KeOps implementation):\", round(end - start, 5), \"s\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
The :meth:`pykeops.numpy.LazyTensor.solve` method uses a conjugate gradient solver and assumes\n that **Kxx** defines a **symmetric**, positive and definite\n **linear** reduction with respect to the alias ``\"b\"``\n specified trough the third argument.