Skip to content

Commit

Permalink
CircleCI update of dev docs (817).
Browse files Browse the repository at this point in the history
  • Loading branch information
Circle CI committed Dec 25, 2024
1 parent e23cff5 commit b4fbe0e
Show file tree
Hide file tree
Showing 56 changed files with 235 additions and 273 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"outputs": [],
"source": [
"# Author: Hugues Van Assel <[email protected]>\n#\n# License: BSD 3-Clause License\n\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import load_digits\n\nfrom torchdr.spectral import PCA\nfrom torchdr import AffinityMatcher, ScalarProductAffinity"
"# Author: Hugues Van Assel <[email protected]>\n#\n# License: BSD 3-Clause License\n\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import load_digits\n\nfrom torchdr import AffinityMatcher, ScalarProductAffinity\nfrom torchdr.spectral import PCA"
]
},
{
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"outputs": [],
"source": [
"# Author: Hugues Van Assel <[email protected]>\n#\n# License: BSD 3-Clause License\n\nimport torch\nimport matplotlib.pyplot as plt\nfrom matplotlib import cm\n\nfrom torchdr import (\n NormalizedGaussianAffinity,\n EntropicAffinity,\n)"
"# Author: Hugues Van Assel <[email protected]>\n#\n# License: BSD 3-Clause License\n\nimport matplotlib.pyplot as plt\nimport torch\nfrom matplotlib import cm\n\nfrom torchdr import EntropicAffinity, NormalizedGaussianAffinity"
]
},
{
Expand Down Expand Up @@ -65,7 +65,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Entropic affinity (adaptive bandwidth)\n\nTo remedy this issue, we can use an **entropic affinity**. The entropic affinity\nemploys an **adaptive bandwidth** that depends on the local density of points.\nBy controling the entropy of each row of the affinity matrix, it ensures that\n**each point has the same number of effective neighbors** (given by\nthe ``perplexity`` parameter) regardless of the local density around it.\n\nIn ``TorchDR``, this object is available here :\n:class:`torchdr.EntropicAffinity`.\n\n"
"## Entropic affinity (adaptive bandwidth)\n\nTo remedy this issue, we can use an **entropic affinity**. The entropic affinity\nemploys an **adaptive bandwidth** that depends on the local density of points.\nBy controlling the entropy of each row of the affinity matrix, it ensures that\n**each point has the same number of effective neighbors** (given by\nthe ``perplexity`` parameter) regardless of the local density around it.\n\nIn ``TorchDR``, this object is available here :\n:class:`torchdr.EntropicAffinity`.\n\n"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=t, s=50, alpha=0.8)
ax.set_title("Swiss Roll in Ambient Space")
ax.view_init(azim=-66, elev=12)
_ = ax.text2D(0.8, 0.05, s="n_samples={}".format(n_samples), transform=ax.transAxes)
_ = ax.text2D(0.8, 0.05, s=f"n_samples={n_samples}", transform=ax.transAxes)

# %%
# Compute the TSNE embedding
Expand Down Expand Up @@ -77,7 +77,7 @@
for i, perplexity in enumerate(perplexity_values):
plt.subplot(1, 4, i + 1)
plt.scatter(X_embedded[i][:, 0], X_embedded[i][:, 1], c=t, s=50, alpha=0.8)
plt.title("Perplexity = {}".format(perplexity))
plt.title(f"Perplexity = {perplexity}")

# %%
# We can observe that the perplexity parameter significantly influences the embedding.
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"outputs": [],
"source": [
"# Author: Titouan Vayer <[email protected]>\n# Hugues Van Assel <[email protected]>\n#\n# License: BSD 3-Clause License"
"# Author: Titouan Vayer <[email protected]>\n# Hugues Van Assel <[email protected]>\n#\n# License: BSD 3-Clause License\n\nimport urllib.request"
]
},
{
Expand All @@ -26,7 +26,7 @@
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nfrom torchdr import (\n AffinityMatcher,\n SNE,\n UMAP,\n TSNE,\n EntropicAffinity,\n NormalizedGaussianAffinity,\n)\nimport numpy as np\nimport urllib.request"
"import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom torchdr import (\n SNE,\n TSNE,\n UMAP,\n AffinityMatcher,\n EntropicAffinity,\n NormalizedGaussianAffinity,\n)"
]
},
{
Expand Down Expand Up @@ -62,7 +62,7 @@
},
"outputs": [],
"source": [
"params = {\n \"optimizer\": \"Adam\",\n \"optimizer_kwargs\": None,\n \"max_iter\": 100,\n \"lr\": 1e0,\n}\n\nsne = SNE(early_exaggeration=1, **params)\n\numap = UMAP(early_exaggeration=1, **params)\n\ntsne = TSNE(early_exaggeration=1, **params)\n\nall_methods = {\n \"TSNE\": tsne,\n \"SNE\": sne,\n \"UMAP\": umap,\n}\n\nfor method_name, method in all_methods.items():\n print(\"--- Computing {} ---\".format(method_name))\n method.fit(X)"
"params = {\n \"optimizer\": \"Adam\",\n \"optimizer_kwargs\": None,\n \"max_iter\": 100,\n \"lr\": 1e0,\n}\n\nsne = SNE(early_exaggeration=1, **params)\n\numap = UMAP(early_exaggeration=1, **params)\n\ntsne = TSNE(early_exaggeration=1, **params)\n\nall_methods = {\n \"TSNE\": tsne,\n \"SNE\": sne,\n \"UMAP\": umap,\n}\n\nfor method_name, method in all_methods.items():\n print(f\"--- Computing {method_name} ---\")\n method.fit(X)"
]
},
{
Expand All @@ -80,7 +80,7 @@
},
"outputs": [],
"source": [
"fig = plt.figure(figsize=(12, 4))\n\nfor i, (method_name, method) in enumerate(all_methods.items()):\n ax = fig.add_subplot(1, 3, i + 1)\n emb = method.embedding_.detach().numpy() # get the embedding\n ax.scatter(emb[:, 0], emb[:, 1], c=Y, s=10)\n ax.set_title(\"{0}\".format(method_name), fontsize=24)\n ax.set_xticks([])\n ax.set_yticks([])\nplt.tight_layout()"
"fig = plt.figure(figsize=(12, 4))\n\nfor i, (method_name, method) in enumerate(all_methods.items()):\n ax = fig.add_subplot(1, 3, i + 1)\n emb = method.embedding_.detach().numpy() # get the embedding\n ax.scatter(emb[:, 0], emb[:, 1], c=Y, s=10)\n ax.set_title(f\"{method_name}\", fontsize=24)\n ax.set_xticks([])\n ax.set_yticks([])\nplt.tight_layout()"
]
},
{
Expand All @@ -98,7 +98,7 @@
},
"outputs": [],
"source": [
"sne_affinity_matcher = AffinityMatcher(\n n_components=2,\n # SNE matches an EntropicAffinity\n affinity_in=EntropicAffinity(sparsity=False),\n # with a Gaussian kernel normalized by row\n affinity_out=NormalizedGaussianAffinity(normalization_dim=1),\n loss_fn=\"cross_entropy_loss\", # and the cross_entropy loss\n **params,\n)\nsne_affinity_matcher.fit(X)\n\nfig = plt.figure(figsize=(8, 4))\ntwo_sne_dict = {\"SNE\": sne, \"SNE (with affinity matcher)\": sne_affinity_matcher}\nfor i, (method_name, method) in enumerate(two_sne_dict.items()):\n ax = fig.add_subplot(1, 2, i + 1)\n emb = method.embedding_.detach().numpy() # get the embedding\n ax.scatter(emb[:, 0], emb[:, 1], c=Y, s=10)\n ax.set_title(\"{0}\".format(method_name), fontsize=15)\n ax.set_xticks([])\n ax.set_yticks([])\nplt.tight_layout()"
"sne_affinity_matcher = AffinityMatcher(\n n_components=2,\n # SNE matches an EntropicAffinity\n affinity_in=EntropicAffinity(sparsity=False),\n # with a Gaussian kernel normalized by row\n affinity_out=NormalizedGaussianAffinity(normalization_dim=1),\n loss_fn=\"cross_entropy_loss\", # and the cross_entropy loss\n **params,\n)\nsne_affinity_matcher.fit(X)\n\nfig = plt.figure(figsize=(8, 4))\ntwo_sne_dict = {\"SNE\": sne, \"SNE (with affinity matcher)\": sne_affinity_matcher}\nfor i, (method_name, method) in enumerate(two_sne_dict.items()):\n ax = fig.add_subplot(1, 2, i + 1)\n emb = method.embedding_.detach().numpy() # get the embedding\n ax.scatter(emb[:, 0], emb[:, 1], c=Y, s=10)\n ax.set_title(f\"{method_name}\", fontsize=15)\n ax.set_xticks([])\n ax.set_yticks([])\nplt.tight_layout()"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits

from torchdr.spectral import PCA
from torchdr import AffinityMatcher, ScalarProductAffinity
from torchdr.spectral import PCA

# %%
# Load toy images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
#
# License: BSD 3-Clause License

import urllib.request

# %%
import matplotlib.pyplot as plt
import numpy as np

from torchdr import (
AffinityMatcher,
SNE,
UMAP,
TSNE,
UMAP,
AffinityMatcher,
EntropicAffinity,
NormalizedGaussianAffinity,
)
import numpy as np
import urllib.request


# %%
# Load the SNARE-seq dataset (gene expression) with cell type labels
Expand Down Expand Up @@ -71,7 +71,7 @@ def load_numpy_from_url(url, delimiter="\t"):
}

for method_name, method in all_methods.items():
print("--- Computing {} ---".format(method_name))
print(f"--- Computing {method_name} ---")
method.fit(X)

# %%
Expand All @@ -84,7 +84,7 @@ def load_numpy_from_url(url, delimiter="\t"):
ax = fig.add_subplot(1, 3, i + 1)
emb = method.embedding_.detach().numpy() # get the embedding
ax.scatter(emb[:, 0], emb[:, 1], c=Y, s=10)
ax.set_title("{0}".format(method_name), fontsize=24)
ax.set_title(f"{method_name}", fontsize=24)
ax.set_xticks([])
ax.set_yticks([])
plt.tight_layout()
Expand Down Expand Up @@ -119,7 +119,7 @@ def load_numpy_from_url(url, delimiter="\t"):
ax = fig.add_subplot(1, 2, i + 1)
emb = method.embedding_.detach().numpy() # get the embedding
ax.scatter(emb[:, 0], emb[:, 1], c=Y, s=10)
ax.set_title("{0}".format(method_name), fontsize=15)
ax.set_title(f"{method_name}", fontsize=15)
ax.set_xticks([])
ax.set_yticks([])
plt.tight_layout()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
"outputs": [],
"source": [
"fig = plt.figure(figsize=(8, 6))\nax = fig.add_subplot(111, projection=\"3d\")\nfig.add_axes(ax)\nax.scatter(X[:, 0], X[:, 1], X[:, 2], c=t, s=50, alpha=0.8)\nax.set_title(\"Swiss Roll in Ambient Space\")\nax.view_init(azim=-66, elev=12)\n_ = ax.text2D(0.8, 0.05, s=\"n_samples={}\".format(n_samples), transform=ax.transAxes)"
"fig = plt.figure(figsize=(8, 6))\nax = fig.add_subplot(111, projection=\"3d\")\nfig.add_axes(ax)\nax.scatter(X[:, 0], X[:, 1], X[:, 2], c=t, s=50, alpha=0.8)\nax.set_title(\"Swiss Roll in Ambient Space\")\nax.view_init(azim=-66, elev=12)\n_ = ax.text2D(0.8, 0.05, s=f\"n_samples={n_samples}\", transform=ax.transAxes)"
]
},
{
Expand Down Expand Up @@ -116,7 +116,7 @@
},
"outputs": [],
"source": [
"perplexity_values = [2, 5, 10, 20]\nX_embedded = []\nfor perplexity in perplexity_values:\n if len(X_embedded) == 0:\n init = \"pca\"\n else:\n init = X_embedded[-1]\n tsne = TSNE(n_components=2, perplexity=perplexity, max_iter=200, init=init)\n X_embedded.append(tsne.fit_transform(X))\n\nplt.figure(figsize=(12, 4))\n\nfor i, perplexity in enumerate(perplexity_values):\n plt.subplot(1, 4, i + 1)\n plt.scatter(X_embedded[i][:, 0], X_embedded[i][:, 1], c=t, s=50, alpha=0.8)\n plt.title(\"Perplexity = {}\".format(perplexity))"
"perplexity_values = [2, 5, 10, 20]\nX_embedded = []\nfor perplexity in perplexity_values:\n if len(X_embedded) == 0:\n init = \"pca\"\n else:\n init = X_embedded[-1]\n tsne = TSNE(n_components=2, perplexity=perplexity, max_iter=200, init=init)\n X_embedded.append(tsne.fit_transform(X))\n\nplt.figure(figsize=(12, 4))\n\nfor i, perplexity in enumerate(perplexity_values):\n plt.subplot(1, 4, i + 1)\n plt.scatter(X_embedded[i][:, 0], X_embedded[i][:, 1], c=t, s=50, alpha=0.8)\n plt.title(f\"Perplexity = {perplexity}\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
#
# License: BSD 3-Clause License

import torch
import matplotlib.pyplot as plt
import torch
from matplotlib import cm

from torchdr import (
NormalizedGaussianAffinity,
EntropicAffinity,
)
from torchdr import EntropicAffinity, NormalizedGaussianAffinity

# %%
# We generate three Gaussian clusters of points with different standard deviations
Expand Down Expand Up @@ -95,7 +92,7 @@ def plot_affinity_graph(G):
#
# To remedy this issue, we can use an **entropic affinity**. The entropic affinity
# employs an **adaptive bandwidth** that depends on the local density of points.
# By controling the entropy of each row of the affinity matrix, it ensures that
# By controlling the entropy of each row of the affinity matrix, it ensures that
# **each point has the same number of effective neighbors** (given by
# the ``perplexity`` parameter) regardless of the local density around it.
#
Expand Down
13 changes: 6 additions & 7 deletions dev/_modules/torchdr/affinity/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,25 @@
<div itemprop="articleBody">

<h1>Source code for torchdr.affinity.base</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot;Base classes for affinity matrices.&quot;&quot;&quot;</span>
<span></span><span class="sd">&quot;&quot;&quot;Base classes for affinity matrices.&quot;&quot;&quot;</span>

<span class="c1"># Author: Hugues Van Assel &lt;[email protected]&gt;</span>
<span class="c1">#</span>
<span class="c1"># License: BSD 3-Clause License</span>

<span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABC</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">torch</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">torchdr.utils</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">LazyTensorType</span><span class="p">,</span>
<span class="n">handle_keops</span><span class="p">,</span>
<span class="n">pairwise_distances</span><span class="p">,</span>
<span class="n">pykeops</span><span class="p">,</span>
<span class="n">symmetric_pairwise_distances</span><span class="p">,</span>
<span class="n">symmetric_pairwise_distances_indices</span><span class="p">,</span>
<span class="n">pairwise_distances</span><span class="p">,</span>
<span class="n">to_torch</span><span class="p">,</span>
<span class="n">LazyTensorType</span><span class="p">,</span>
<span class="n">pykeops</span><span class="p">,</span>
<span class="n">handle_keops</span><span class="p">,</span>
<span class="p">)</span>


Expand Down
26 changes: 13 additions & 13 deletions dev/_modules/torchdr/affinity/entropic.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,36 @@
<div itemprop="articleBody">

<h1>Source code for torchdr.affinity.entropic</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot;Affinity matrices with entropic constraints.&quot;&quot;&quot;</span>
<span></span><span class="sd">&quot;&quot;&quot;Affinity matrices with entropic constraints.&quot;&quot;&quot;</span>

<span class="c1"># Author: Hugues Van Assel &lt;[email protected]&gt;</span>
<span class="c1"># Titouan Vayer &lt;[email protected]&gt;</span>
<span class="c1"># Rémi Flamary &lt;[email protected]&gt;</span>
<span class="c1">#</span>
<span class="c1"># License: BSD 3-Clause License</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="kn">import</span> <span class="n">tqdm</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">contextlib</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">tqdm</span> <span class="kn">import</span> <span class="n">tqdm</span>

<span class="kn">from</span> <span class="nn">torchdr.affinity.base</span> <span class="kn">import</span> <span class="n">LogAffinity</span><span class="p">,</span> <span class="n">SparseLogAffinity</span>
<span class="kn">from</span> <span class="nn">torchdr.utils</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">OPTIMIZERS</span><span class="p">,</span>
<span class="n">batch_transpose</span><span class="p">,</span>
<span class="n">check_NaNs</span><span class="p">,</span>
<span class="n">entropy</span><span class="p">,</span>
<span class="n">false_position</span><span class="p">,</span>
<span class="n">wrap_vectors</span><span class="p">,</span>
<span class="n">sum_matrix_vector</span><span class="p">,</span>
<span class="n">kmin</span><span class="p">,</span>
<span class="n">kmax</span><span class="p">,</span>
<span class="n">check_NaNs</span><span class="p">,</span>
<span class="n">kmin</span><span class="p">,</span>
<span class="n">logsumexp_red</span><span class="p">,</span>
<span class="n">batch_transpose</span><span class="p">,</span>
<span class="n">OPTIMIZERS</span><span class="p">,</span>
<span class="n">sum_matrix_vector</span><span class="p">,</span>
<span class="n">wrap_vectors</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">torchdr.affinity.base</span> <span class="kn">import</span> <span class="n">LogAffinity</span><span class="p">,</span> <span class="n">SparseLogAffinity</span>


<span class="nd">@wrap_vectors</span>
Expand Down
14 changes: 4 additions & 10 deletions dev/_modules/torchdr/affinity/knn_normalized.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,19 @@
<div itemprop="articleBody">

<h1>Source code for torchdr.affinity.knn_normalized</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot;Affinity matrices with normalizations using nearest neighbor distances.&quot;&quot;&quot;</span>
<span></span><span class="sd">&quot;&quot;&quot;Affinity matrices with normalizations using nearest neighbor distances.&quot;&quot;&quot;</span>

<span class="c1"># Author: Hugues Van Assel &lt;[email protected]&gt;</span>
<span class="c1"># Cédric Vincent-Cuaz &lt;[email protected]&gt;</span>
<span class="c1">#</span>
<span class="c1"># License: BSD 3-Clause License</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>

<span class="kn">import</span> <span class="nn">torch</span>

<span class="kn">from</span> <span class="nn">torchdr.affinity.base</span> <span class="kn">import</span> <span class="n">Affinity</span><span class="p">,</span> <span class="n">LogAffinity</span>
<span class="kn">from</span> <span class="nn">torchdr.utils</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">kmin</span><span class="p">,</span>
<span class="n">wrap_vectors</span><span class="p">,</span>
<span class="n">batch_transpose</span><span class="p">,</span>
<span class="n">logsumexp_red</span><span class="p">,</span>
<span class="n">sum_red</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">torchdr.utils</span> <span class="kn">import</span> <span class="n">batch_transpose</span><span class="p">,</span> <span class="n">kmin</span><span class="p">,</span> <span class="n">logsumexp_red</span><span class="p">,</span> <span class="n">sum_red</span><span class="p">,</span> <span class="n">wrap_vectors</span>


<span class="nd">@wrap_vectors</span>
Expand Down
Loading

0 comments on commit b4fbe0e

Please sign in to comment.