Skip to content

Instantly share code, notes, and snippets.

@NTT123
Created September 21, 2019 08:59
Show Gist options
  • Save NTT123/143a4b7cadc38fa1656c0732c1dcf3c5 to your computer and use it in GitHub Desktop.
Save NTT123/143a4b7cadc38fa1656c0732c1dcf3c5 to your computer and use it in GitHub Desktop.
two linked tori generator.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "two linked tori generator.ipynb",
"provenance": [],
"private_outputs": true,
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/NTT123/143a4b7cadc38fa1656c0732c1dcf3c5/two-linked-tori-generator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UvZCGwhe57oE",
"colab_type": "text"
},
"source": [
"## Two Linked Tori\n",
"\n",
"Our aim is to generate a dataset of 3d points with the following distribution\n",
"\n",
"<img src=\"http://colah.github.io/posts/2014-03-NN-Manifolds-Topology/img/link.png\" alt=\"drawing\" width=\"400\"/>\n",
"\n",
"Source: Christopher Olah, http://colah.github.io/posts/2014-03-NN-Manifolds-Topology"
]
},
{
"cell_type": "code",
"metadata": {
"id": "kaQB-l115tWB",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np\n",
"import torch\n",
"\n",
"\n",
"def gen_torus(N, R=5., r=0.5):\n",
" import math\n",
" theta = np.random.rand(N) * 2. * math.pi\n",
" phi = np.random.rand(N) * 2. * math.pi\n",
"\n",
" data = np.empty((N, 3))\n",
" # https://en.m.wikipedia.org/wiki/Torus\n",
" data[:, 0] = (r * np.cos(theta) + R) * np.cos(phi)\n",
" data[:, 1] = (r * np.cos(theta) + R) * np.sin(phi)\n",
" data[:, 2] = r * np.sin(theta)\n",
" return data\n",
"\n",
"\n",
"def make_two_linked_tori(n: int) -> np.ndarray :\n",
"\n",
" data1 = gen_torus(n)\n",
" data1[:, 1] -= 2.5\n",
" data2 = gen_torus(n)\n",
" data2[:, 0], data2[:, 2] = np.copy(data2[:, 2]), np.copy(data2[:, 0])\n",
" data2[:, 1] += 2.5\n",
" label = np.concatenate((np.ones(n), np.zeros(n)))\n",
" data = np.concatenate((data1, data2))\n",
" \n",
" return data, label"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "01pzhoLMBPVc",
"colab_type": "code",
"colab": {}
},
"source": [
"\n",
"data, label = make_two_linked_tori(1000)\n",
"\n",
"from mpl_toolkits.mplot3d import Axes3D\n",
"import matplotlib.pyplot as plt\n",
"\n",
"fig = plt.figure()\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"\n",
"_ = ax.scatter(data[:, 0], data[:, 1], data[:, 2], c = label)\n",
"ax.axis(\"equal\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "lXqPEh_w-_Tm",
"colab_type": "code",
"colab": {}
},
"source": [
"from torch.utils.tensorboard import SummaryWriter\n",
"wrt = SummaryWriter(log_dir=\"./logs\")\n",
"wrt.add_embedding(data)\n",
"wrt.close()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "g_HggZ9qKTPQ",
"colab_type": "code",
"colab": {}
},
"source": [
"%load_ext tensorboard"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_6T-M2y9KWca",
"colab_type": "code",
"colab": {}
},
"source": [
"%tensorboard --logdir ./logs --port 1246"
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment