{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot Fused-Gromov-Wasserstein\n\nThis example first illustrates the computation of FGW for 1D measures estimated\nusing a Conditional Gradient solver [24].\n\n[24] Vayer Titouan, Chapel Laetitia, Flamary R\u00e9mi, Tavenard Romain\nand Courty Nicolas\n\"Optimal Transport for structured data with application on graphs\"\nInternational Conference on Machine Learning (ICML). 2019.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Titouan Vayer \n#\n# License: MIT License\n\n# sphinx_gallery_thumbnail_number = 3\n\nimport matplotlib.pyplot as pl\nimport numpy as np\nimport ot\nfrom ot.gromov import gromov_wasserstein, fused_gromov_wasserstein" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# parameters\n\n# We create two 1D random measures\nn = 20 # number of points in the first distribution\nn2 = 30 # number of points in the second distribution\nsig = 1 # std of first distribution\nsig2 = 0.1 # std of second distribution\n\nnp.random.seed(0)\n\nphi = np.arange(n)[:, None]\nxs = phi + sig * np.random.randn(n, 1)\nys = np.vstack(\n (np.ones((n // 2, 1)), 0 * np.ones((n // 2, 1)))\n) + sig2 * np.random.randn(n, 1)\n\nphi2 = np.arange(n2)[:, None]\nxt = phi2 + sig * np.random.randn(n2, 1)\nyt = np.vstack(\n (np.ones((n2 // 2, 1)), 0 * np.ones((n2 // 2, 1)))\n) + sig2 * np.random.randn(n2, 1)\nyt = yt[::-1, :]\n\np = ot.unif(n)\nq = ot.unif(n2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plot the distributions\n\npl.figure(1, (7, 7))\n\npl.subplot(2, 1, 1)\n\npl.scatter(ys, xs, c=phi, s=70)\npl.ylabel(\"Feature value a\", fontsize=20)\npl.title(\"$\\mu=\\sum_i \\delta_{x_i,a_i}$\", fontsize=25, y=1)\npl.xticks(())\npl.yticks(())\npl.subplot(2, 1, 2)\npl.scatter(yt, xt, c=phi2, s=70)\npl.xlabel(\"coordinates x/y\", fontsize=25)\npl.ylabel(\"Feature value b\", fontsize=20)\npl.title(\"$\\\\nu=\\sum_j \\delta_{y_j,b_j}$\", fontsize=25, y=1)\npl.yticks(())\npl.tight_layout()\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create structure matrices and across-feature distance matrix\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Structure matrices and across-features distance matrix\nC1 = ot.dist(xs)\nC2 = ot.dist(xt)\nM = ot.dist(ys, yt)\nw1 = ot.unif(C1.shape[0])\nw2 = ot.unif(C2.shape[0])\nGot = ot.emd([], [], M)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot matrices\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cmap = \"Reds\"\n\npl.figure(2, (5, 5))\nfs = 15\nl_x = [0, 5, 10, 15]\nl_y = [0, 5, 10, 15, 20, 25]\ngs = pl.GridSpec(5, 5)\n\nax1 = pl.subplot(gs[3:, :2])\n\npl.imshow(C1, cmap=cmap, interpolation=\"nearest\")\npl.title(\"$C_1$\", fontsize=fs)\npl.xlabel(\"$k$\", fontsize=fs)\npl.ylabel(\"$i$\", fontsize=fs)\npl.xticks(l_x)\npl.yticks(l_x)\n\nax2 = pl.subplot(gs[:3, 2:])\n\npl.imshow(C2, cmap=cmap, interpolation=\"nearest\")\npl.title(\"$C_2$\", fontsize=fs)\npl.ylabel(\"$l$\", fontsize=fs)\npl.xticks(())\npl.yticks(l_y)\nax2.set_aspect(\"auto\")\n\nax3 = pl.subplot(gs[3:, 2:], sharex=ax2, sharey=ax1)\npl.imshow(M, cmap=cmap, interpolation=\"nearest\")\npl.yticks(l_x)\npl.xticks(l_y)\npl.ylabel(\"$i$\", fontsize=fs)\npl.title(\"$M_{AB}$\", fontsize=fs)\npl.xlabel(\"$j$\", fontsize=fs)\npl.tight_layout()\nax3.set_aspect(\"auto\")\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute FGW/GW\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Computing FGW and GW\nalpha = 1e-3\n\not.tic()\nGwg, logw = fused_gromov_wasserstein(\n M, C1, C2, p, q, loss_fun=\"square_loss\", alpha=alpha, verbose=True, log=True\n)\not.toc()\n\n# reload_ext WGW\nGg, log = gromov_wasserstein(\n C1, C2, p, q, loss_fun=\"square_loss\", verbose=True, log=True\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize transport matrices\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# visu OT matrix\ncmap = \"Blues\"\nfs = 15\npl.figure(3, (13, 5))\npl.clf()\npl.subplot(1, 3, 1)\npl.imshow(Got, cmap=cmap, interpolation=\"nearest\")\npl.ylabel(\"$i$\", fontsize=fs)\npl.xticks(())\n\npl.title(\"Wasserstein ($M$ only)\")\n\npl.subplot(1, 3, 2)\npl.imshow(Gg, cmap=cmap, interpolation=\"nearest\")\npl.title(\"Gromov ($C_1,C_2$ only)\")\npl.xticks(())\npl.subplot(1, 3, 3)\npl.imshow(Gwg, cmap=cmap, interpolation=\"nearest\")\npl.title(\"FGW ($M+C_1,C_2$)\")\n\npl.xlabel(\"$j$\", fontsize=fs)\npl.ylabel(\"$i$\", fontsize=fs)\n\npl.tight_layout()\npl.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.18" } }, "nbformat": 4, "nbformat_minor": 0 }