1027 lines
217 KiB
Plaintext
1027 lines
217 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 91,
|
|||
|
"id": "54634e7b",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"import numpy as np\n",
|
|||
|
"from numba import jit\n",
|
|||
|
"import itertools\n",
|
|||
|
"\n",
|
|||
|
"fontsize=14"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "8e090825",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__В pdf-файле задачи 1-4__"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 374,
|
|||
|
"id": "705fbec9",
|
|||
|
"metadata": {
|
|||
|
"scrolled": true
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/html": [
|
|||
|
"\n",
|
|||
|
" <iframe\n",
|
|||
|
" width=\"1000\"\n",
|
|||
|
" height=\"800\"\n",
|
|||
|
" src=\"HW1_14.pdf\"\n",
|
|||
|
" frameborder=\"0\"\n",
|
|||
|
" allowfullscreen\n",
|
|||
|
" \n",
|
|||
|
" ></iframe>\n",
|
|||
|
" "
|
|||
|
],
|
|||
|
"text/plain": [
|
|||
|
"<IPython.lib.display.IFrame at 0x7ff362a4af20>"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 374,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"from IPython.display import IFrame\n",
|
|||
|
"IFrame(\"HW1_14.pdf\", width=1000, height=800)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "d4960afd",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Задача 5 \n",
|
|||
|
"\n",
|
|||
|
"- сгенерируем s точек равномерно в квадрате стороной 2, оставим только те, для которых $|| x ||_p \\le 1, p=1,2,3$\n",
|
|||
|
"- сравним реализацию с jit и без"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"id": "55fc75ad",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"@jit(nopython=True)\n",
|
|||
|
"def numba_unit_disk(p=1, s=100000):\n",
|
|||
|
" X_old, Y_old = np.random.uniform(-1.0, 1.0, s), np.random.uniform(-1.0, 1.0, s)\n",
|
|||
|
" X_new, Y_new = [], []\n",
|
|||
|
" \n",
|
|||
|
" for x, y in zip(X_old, Y_old):\n",
|
|||
|
" if (abs(x)**p + abs(y)**p)**(1/p) <= 1:\n",
|
|||
|
" X_new.append(x)\n",
|
|||
|
" Y_new.append(y)\n",
|
|||
|
" return X_new, Y_new\n",
|
|||
|
"\n",
|
|||
|
"def unit_disk(p=1, s=100000):\n",
|
|||
|
" X_old, Y_old = np.random.uniform(-1.0, 1.0, s), np.random.uniform(-1.0, 1.0, s)\n",
|
|||
|
" \n",
|
|||
|
" X_new, Y_new = [], []\n",
|
|||
|
" for x, y in zip(X_old, Y_old):\n",
|
|||
|
" if np.linalg.norm([x, y], ord=p) <= 1:\n",
|
|||
|
" X_new.append(x)\n",
|
|||
|
" Y_new.append(y)\n",
|
|||
|
" return X_new, Y_new"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"id": "108af16e",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAF2CAYAAABj+Z+GAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zL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3EF5RZRzwyyLr1/POwciW88kreldhUbhFYRzx5nGXh1Vdh+XJ3IBeNg8Dm5MnjLEvHjydLlToMisNBYLNqNj15nGXv5MlkaKkVg4PAZuWLxaxXTpyAt7zFLYMiyCQIJF0u6UlJByTdOM3j10o6JOnR9OsjbY9tk/RU+uWPnQIZHPTFYtZbr78O117rMMhb10EgaTHwBeCDwAbgQ5I2TLPrVyLi0vTr9vS55wKfAn4TuAz4lKSV3dZk3WkNE/UpIeuHU6dgx468q6i3LFoElwEHIuLpiDgB3A1s7vC5HwDuj4iXI+IV4H7g8gxqsgUaHvZ1AtZ/R47AihVuGeQliyBYC/yw7f5z6bap/kDSdyXdK+mieT4XSdsljUsaP3ToUAZl21TNZnLhj1kejh71aKK89Kuz+B+A9RHxb0j+698z3xeIiF0RMRARA2vWrMm8QHPHsOXv5MlkIkPrryyC4CBwUdv9C9NtPxMRhyPieHr3duDfdvpc649m0x3DVgyHD8Nb3+qWQT9lEQQPA5dIuljSUmALsK99B0nnt929AngivX0f8H5JK9NO4ven26yPPH2EFc2RI17trJ+6DoKIOAV8jOQD/AngnoiYkPRpSVeku/25pAlJ3wH+HLg2fe7LwF+RhMnDwKfTbdYHrTWGHQJWVF7trD8UJZw7YGBgIMbHx/Muo9SazWSx8dOn867EbG5790KjkXcV5SfpkYgYmLrdVxbXkEPAymbrVvcZ9JKDoEb
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 432x432 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAF2CAYAAABj+Z+GAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAA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Tlcf325wsBB0GaDg0m7os8aNiuPycmkv68s1zJwELTR6KhHB5mV2dGjsHlz74eBg6CNfIFss/J7883e/7/uIGgjXyDbrBpeeqnoClrjIGiTXj9UNLPZi+jtk80cBG1S5cvemVVRL89J5CBog+FhzyNkVkW9ekEbB0GORkdh4cLe/WMws9ZdcknRFcydgyAn9XmEfIEZs2o7cKD3+ggdBDm54YaiKzCzbnHjjUVXMDcOghwMDsKpU0VXYWbd4uTJ3joqcBC0yGcPm1kzGzf2Thg4CFrUa4eAZtY5vTKM3EHQgtHR5BDQzKyZXhlGnksQSLpS0guSDkm6pcn6GySNS3o6vf1BZt0mSQfT26Y86umUXp9fxMwMcggCSX3AF4CPAmuBayWtbbLplyNiXXq7J33uMuAzwK8BlwGfkbS01Zo6YXTUcwmZ2cze+c7u7yvI44jgMuBQRByOiJPAg8D6WT73I8AjEfF6RLwBPAJcmUNNbTU87GsPm9nsvPUWXHddd4dBHkFwIfBy5vEr6bJGvy3pGUkPSbp4js/tGqOjPnPYzOYmArZsKbqKqXWqs/jvgFUR8W9JvvXvnOsOJG2WNCZpbHx8PPcCZ8sXoDez+Th2rOgKppZHEBwBLs48vihd9raIOBoRJ9KH9wD/brbPzexje0TUIqK2cuX
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 432x432 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAF2CAYAAABj+Z+GAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAldklEQVR4nO3dfZAc9X3n8fdHq5MwkmP0VJgnrSDmHIvLlWxPcc75KnEs2cZUHSIXYsu3MnI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LnoCsys35Whv8BB0MTISHJ9EDOzfucgaMJ9A2ZWFQ6CJtw3YGbdUvSFLB0EDRT9RzGzatm0qdg7mTkIxhkZ8QlkZtZ9Rd7W0kEwThkO5TKzatq5s5j3dRCMU4ZDuczMuslBYGZWIkX0UToIzMxK5O67u/+eDoKMMlzzw8yqLaL77+kgyPBJZGZWRQ6C1IoVPonMzMqh23snHAQkC72ow7bMzMZbs6a77+cgANavL7oCM7MzTp3q7tFDDgLgxReLrsDM7GybNnVvF5GDAJg/v+gKzMzO1a0DWHIJAknXSNonaUzS7Q2Gf07SYUm708fvZIatkbQ/fXR5z1jiJz8p4l3NzCbWrQNYZrY7A0kDwFeBjwEHgackbY+IveNG/XpE3DZu2vnAHwM1IIBd6bRvtFtXK06e7Oa7mZmVSx5bBFcDYxHxQkScBB4EVk5x2k8Aj0TE6+nK/xHgmhxqmrKrrurmu5mZlU8eQXAJ8FLm9cG0bbzflPS0pIckXdbitB2zd/x2i5lZiXTj0tTd6iz+G2BJRPxLkm/9W1udgaS1kkYljR4+fDj3As3Myqgb5zjlEQSHgMsyry9N234uIo5ExIn05T3AB6c6bWYeWyK
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 432x432 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"for n in range(1, 4):\n",
|
|||
|
" x, y = numba_unit_disk(n)\n",
|
|||
|
" \n",
|
|||
|
" plt.figure(figsize=(6,6))\n",
|
|||
|
" plt.scatter(x, y, color=\"blue\")\n",
|
|||
|
" plt.title(\"p = {}\".format(n), fontsize=fontsize)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"id": "43ba0e12",
|
|||
|
"metadata": {
|
|||
|
"scrolled": true
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"759 ms ± 74.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
|
|||
|
"6.3 ms ± 1.44 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"%timeit unit_disk()\n",
|
|||
|
"%timeit numba_unit_disk()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "0b618b90",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__Вывод:__\n",
|
|||
|
"- JIT крут, вычисления в 150-200 раз быстрее"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "9aff9c18",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Задача 6"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 92,
|
|||
|
"id": "74c4965f",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"c = 3"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 93,
|
|||
|
"id": "41852c3b",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Generate some data\n",
|
|||
|
"np.random.seed(42)\n",
|
|||
|
"lambda1 = np.random.normal(size=(c, c))\n",
|
|||
|
"lambda2 = np.random.normal(size=(c, c))\n",
|
|||
|
"lambda3 = np.random.normal(size=(c, c))\n",
|
|||
|
"G1 = np.random.normal(size=(c, c, c))\n",
|
|||
|
"G2 = np.random.normal(size=(c, c, c))\n",
|
|||
|
"U = np.random.normal(size=(c, c, c, c))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 94,
|
|||
|
"id": "c72140e1",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def Z_naive(lambda1, lambda2, lambda3, G1, G2, U):\n",
|
|||
|
" c = lambda1.shape[0]\n",
|
|||
|
" Z = np.zeros(shape=(c, c, c, c))\n",
|
|||
|
" for a, b, c, d, e, f, g, h, i, j in itertools.product(*([range(c)]*10)):\n",
|
|||
|
" Z[a, h, i, j] += lambda1[a, b]*lambda2[d, e]*lambda3[g, h]*G1[c, b, d]*G2[f, e, g]*U[i, j, c, f]\n",
|
|||
|
" return Z\n",
|
|||
|
"\n",
|
|||
|
"def Z(lambda1, lambda2, lambda3, G1, G2, U):\n",
|
|||
|
" return np.einsum(\"ab,cbd,de,feg,gh,ijcf->ahij\", lambda1, G1, lambda2, G2, lambda3, U, optimize=True)\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 95,
|
|||
|
"id": "a4ba9f23",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"615 µs ± 144 µs per loop (mean ± std. dev. of 2 runs, 10 loops each)\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# %timeit -n 2 -r 2 Z_naive(lambda1, lambda2, lambda3, G1, G2, U)\n",
|
|||
|
"%timeit -n 10 -r 2 Z(lambda1, lambda2, lambda3, G1, G2, U)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 96,
|
|||
|
"id": "19913a61",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"(3, 3, 3, 3)\n",
|
|||
|
"(3, 3, 3, 3)\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"print(Z_naive(lambda1, lambda2, lambda3, G1, G2, U).shape)\n",
|
|||
|
"print(Z(lambda1, lambda2, lambda3, G1, G2, U).shape)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 97,
|
|||
|
"id": "860ebc25",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"4.99716543697806e-26\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"print((np.sum(Z(lambda1, lambda2, lambda3, G1, G2, U) - Z_naive(lambda1, lambda2, lambda3, G1, G2, U))**2))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 98,
|
|||
|
"id": "15b20588",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"['einsum_path', (0, 1), (0, 1), (0, 3), (1, 2), (0, 1)] Complete contraction: ab,cbd,de,feg,gh,ijcf->ahij\n",
|
|||
|
" Naive scaling: 10\n",
|
|||
|
" Optimized scaling: 6\n",
|
|||
|
" Naive FLOP count: 3.543e+05\n",
|
|||
|
" Optimized FLOP count: 2.431e+03\n",
|
|||
|
" Theoretical speedup: 145.740\n",
|
|||
|
" Largest intermediate: 8.100e+01 elements\n",
|
|||
|
"--------------------------------------------------------------------------\n",
|
|||
|
"scaling current remaining\n",
|
|||
|
"--------------------------------------------------------------------------\n",
|
|||
|
" 4 cbd,ab->acd de,feg,gh,ijcf,acd->ahij\n",
|
|||
|
" 4 feg,de->dfg gh,ijcf,acd,dfg->ahij\n",
|
|||
|
" 4 dfg,gh->dfh ijcf,acd,dfh->ahij\n",
|
|||
|
" 5 dfh,acd->acfh ijcf,acfh->ahij\n",
|
|||
|
" 6 acfh,ijcf->ahij ahij->ahij\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"print(*np.einsum_path(\"ab,cbd,de,feg,gh,ijcf->ahij\", lambda1, G1, lambda2, G2, lambda3, U, optimize=True))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "87ec392f",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__np.einsum__ вычисляет свёртку тензоров, причём разбивает суммирование на несколько этапов:\n",
|
|||
|
" \n",
|
|||
|
"- вычисляет части свёртки по отдельности, создаёт промежуточные массивы(тензоры)\n",
|
|||
|
"- вычисляет свёртку от новых тензоров"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "2359e3ae",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__Наивный способ__\n",
|
|||
|
"\n",
|
|||
|
"Для $Z_{ahij}=\\underset{bcdefg}{\\sum}\\lambda^{(1)}_{ab}\\Gamma^{(1)}_{cbd}\\lambda^{(2)}_{de}\\Gamma^{(2)}_{feg}\\lambda^{(3)}_{gh}U_{ijcf}$ с размерностью индексов $\\chi$ есть 6 индексов суммирования, т.е. $Z_{ahij}$ вычисляется за $\\chi ^ 6$, весь тензор за $\\chi ^ 6 \\cdot\\chi ^ 4$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "10dd2839",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__Оптимизированный способ__\n",
|
|||
|
"\n",
|
|||
|
"Делаются свёртки \n",
|
|||
|
"\n",
|
|||
|
"- $\\lambda^{(1)}_{ab}\\Gamma^{(1)}_{cbd} = A_{acd}$ за $\\chi$, т.е. $A$ за $\\chi^4$\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"- $\\lambda^{(2)}_{de}\\Gamma^{(2)}_{feg} = B_{dfg}$, за $\\chi$, т.е. $B$ за $\\chi^4$\n",
|
|||
|
"\n",
|
|||
|
"На этом моменте у нас есть два новых тензора $A, B$, которые мы вычислили за $2 \\chi^4$\n",
|
|||
|
"\n",
|
|||
|
"- $B_{dfg}\\lambda^{(3)}_{gh}=C_{dfh}$ за $\\chi$, т.е. $C$ за $\\chi^4$\n",
|
|||
|
"\n",
|
|||
|
"Добавляется ещё один тензор $C$, который мы вычислили за $\\chi^4$, так как тензоры $B, \\lambda^{(3)}$ были уже известны. На данный момент $3 \\chi^4$ операций\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"- $C_{dfh}A_{acd}=D_{acfh}$ за $\\chi$, т.е. $D$ за $\\chi^3$\n",
|
|||
|
"\n",
|
|||
|
"Новый тензор $D$, суммарно $3 \\chi^4 + \\chi^5$ операций\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"- $D_{acfh}U_{ijcf} = Z_{ahij}$ за $\\chi^{2}$, т.е. ещё $\\chi^{6}$ операций\n",
|
|||
|
"\n",
|
|||
|
"В итоге вычислили тензор $Z$ за $O(\\chi^{6})$ операций, наивный способ совершает $O(\\chi^{10})$ операций. Оптимизация сложности происходит за счёт дополнительного расхода памяти, упрощения выражения, поэтому для небольшого числа операций лучше использовать обычное суммирование."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "2aafecb8",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Для $\\chi = 1$ получается (x0.02)\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"naive: 3.59 µs ± 220 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n",
|
|||
|
"\n",
|
|||
|
"optimised: 225 µs ± 3.51 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "92128c85",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Для $\\chi = 4$ получается (x4000)\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"naive: 1.59 s ± 25.5 ms per loop (mean ± std. dev. of 2 runs, 2 loops each)\n",
|
|||
|
"\n",
|
|||
|
"optimised: 401 µs ± 17.5 µs per loop (mean ± std. dev. of 2 runs, 100 loops each)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "db7a9dcc",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Для $\\chi=50$: \n",
|
|||
|
"\n",
|
|||
|
"optimised: 438 ms ± 17 ms per loop (mean ± std. dev. of 2 runs, 10 loops each)\n",
|
|||
|
"\n",
|
|||
|
"Для $\\chi=5-50$ naive заexplorить, к сожалению, не получается "
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 326,
|
|||
|
"id": "d332b860",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAYAAACutU7EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAuy0lEQVR4nO3dd5gV9dn/8fdNx4CogICPItifiIqKgp1g7NHoZSMaaxRje0wz+SnmsST2xPJENGIK2KJGo1GKiCVCgFWKDUFQsFOVIh2WvX9/fGfleLLl7O6cmVM+r+uaa/bMzM7ce+R8vc+3mrsjIiIiUkyapR2AiIiISEMpgREREZGiowRGREREio4SGBERESk6SmBERESk6CiBERERkaKjBEYSZ2bDzOyjtOMQkeJgZh+Z2bBCv2cOz+xvZm5m/ZN8bqlSAiO1MrNzow/bOjPrXsP5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8pFoJ14z60ZY9vxYwpoZc4GL3f3Vun6vU6dO3qNHj/wHKCINNnXq1C/cvXP9VxYulTEihamu8iWxBCaahXECYZKg4wgLYO1AmNmxTj169GDKlCn1XSYiKTCzj+u/qrCpjBEpTHWVL0nWwPwSmO/uZ2ccK+R1P0RERKRAJdkH5kTgNTN73MwWmdmbZnaZZc3LLSIiIlKfJBOYHYBLCP1ejgLuJvSHubSuXxIRERHJlmQTUjNgirtfFb1+w8x2JiQw92RfbGaDCGsR0b1798SCFBERkcKXZA3MfCB7UcSZQI3ZibsPdfc+7t6nc+eiHuAgIiIiMUsygZlAWI000y5A0Y9gEBERkWQlmcDcCfQzs8FmtpOZnQr8D2FNEBEpEFdeGTYRkdhNnQpHHgkzZzb5Vok
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 576x288 with 2 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAYAAACutU7EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAqBElEQVR4nO3deXgUVb7G8e8vkS0E2WUJFxFw44IgE5HFERwVFBGHwW0QNRcVUPSK48JV1OvKqDACCurgdZAZiAvqMA4RNySiAmqC7CqoCApIQARkEbOc+0d1QhICdEinqrvzfp6nnnTX6VS9SQn8PHXqHHPOISIiIhJLEoIOICIiIlJeKmBEREQk5qiAERERkZijAkZERERijgoYERERiTkqYERERCTmqIARERGRmBNTBYyZnWlmr5vZBjNzZpZ2BMeYaGZZZvaLmX1bRvt9oWOXtR0TiZ9DREREKiamChggGVgB3AzsPcJjJADTgL8fpH0c0KzU9j6Q6ZzLOcJzioiIS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natSvJycnUrVuXLl26sGLFiqL2BQsW0LNnT5KSkkhJSeH6669n586dRe179uwhLS2N5ORkmjRpwpgxYyrvlywiIkfk7bchP18FzAGcc7uAlcDxQWUI0rZt23jzzTcZMWIESUlJJdqSkpK44YYbmDNnDj/99BMAM2bMoKCggIULF/LXv/6VKVOmMGHCBABee+01WrRowb333sumTZvYtGnTQc+7b98+/vKXvzB
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 576x288 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"N, O = [], []\n",
|
|||
|
"C = np.arange(3, 51)\n",
|
|||
|
"\n",
|
|||
|
"for c in C:\n",
|
|||
|
" np.random.seed(42)\n",
|
|||
|
" lambda1 = np.random.normal(size=(c, c))\n",
|
|||
|
" lambda2 = np.random.normal(size=(c, c))\n",
|
|||
|
" lambda3 = np.random.normal(size=(c, c))\n",
|
|||
|
" G1 = np.random.normal(size=(c, c, c))\n",
|
|||
|
" G2 = np.random.normal(size=(c, c, c))\n",
|
|||
|
" U = np.random.normal(size=(c, c, c, c))\n",
|
|||
|
" \n",
|
|||
|
" res = np.einsum_path(\"ab,cbd,de,feg,gh,ijcf->ahij\", lambda1, G1, lambda2, G2, lambda3, U, optimize=True)\n",
|
|||
|
" n, o = res[1].split('\\n')[3:5]\n",
|
|||
|
" n = np.float64(n.split(' ')[-1])\n",
|
|||
|
" o = np.float64(o.split(' ')[-1])\n",
|
|||
|
" N.append(n)\n",
|
|||
|
" O.append(o)\n",
|
|||
|
"\n",
|
|||
|
" \n",
|
|||
|
"plt.rcParams.update({'font.size': 14})\n",
|
|||
|
"plt.figure(figsize=(8,4))\n",
|
|||
|
"\n",
|
|||
|
"plt.subplot(121)\n",
|
|||
|
"plt.plot(C, N, color=\"blue\")\n",
|
|||
|
"plt.title(\"Naive\")\n",
|
|||
|
"plt.xlabel(\"$\\chi$\")\n",
|
|||
|
"plt.ylabel(\"FLOPs\")\n",
|
|||
|
"\n",
|
|||
|
"plt.subplot(122)\n",
|
|||
|
"plt.plot(C, O, color=\"red\")\n",
|
|||
|
"plt.title(\"Optimised\")\n",
|
|||
|
"plt.xlabel(\"$\\chi$\")\n",
|
|||
|
"plt.ylabel(\"FLOPs\")\n",
|
|||
|
"\n",
|
|||
|
"plt.tight_layout()\n",
|
|||
|
"\n",
|
|||
|
"plt.figure(figsize=(8,4))\n",
|
|||
|
"plt.plot(C, N, color=\"blue\", label=\"Naive\")\n",
|
|||
|
"plt.plot(C, O, color=\"red\", label=\"Optimised\")\n",
|
|||
|
"plt.xlabel(\"$\\chi$\")\n",
|
|||
|
"plt.ylabel(\"FLOPs\")\n",
|
|||
|
"\n",
|
|||
|
"plt.legend()\n",
|
|||
|
"\n",
|
|||
|
"plt.tight_layout()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 179,
|
|||
|
"id": "688ca9b4",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"c=2\n",
|
|||
|
"np.random.seed(42)\n",
|
|||
|
"lambda1 = np.random.normal(size=(c, c))\n",
|
|||
|
"lambda2 = np.random.normal(size=(c, c))\n",
|
|||
|
"lambda3 = np.random.normal(size=(c, c))\n",
|
|||
|
"G1 = np.random.normal(size=(c, c, c))\n",
|
|||
|
"G2 = np.random.normal(size=(c, c, c))\n",
|
|||
|
"U = np.random.normal(size=(c, c, c, c))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 180,
|
|||
|
"id": "6eeac349",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"array([[[ 0.24196227, -1.91328024],\n",
|
|||
|
" [-1.72491783, -0.56228753]],\n",
|
|||
|
"\n",
|
|||
|
" [[-1.01283112, 0.31424733],\n",
|
|||
|
" [-0.90802408, -1.4123037 ]]])"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 180,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"lambda1\n",
|
|||
|
"G1"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 319,
|
|||
|
"id": "9e977aa4",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def mY_Z(lambda1, lambda2, lambda3, G1, G2, U):\n",
|
|||
|
" c=2\n",
|
|||
|
" A = np.tensordot(lambda1,G1, axes=([1], [1]))\n",
|
|||
|
" B = np.tensordot(lambda2, G2, axes=([1],[1]))\n",
|
|||
|
" C = np.tensordot(B, lambda3, axes=([2], [0]))\n",
|
|||
|
" D = np.tensordot(A, C, axes=([2], [0]))\n",
|
|||
|
" Z = np.tensordot(D, U, axes=([1, 2], [2,3]))\n",
|
|||
|
" \n",
|
|||
|
" return Z"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 365,
|
|||
|
"id": "324465ef",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"assert np.allclose(mY_Z(lambda1, lambda2, lambda3, G1, G2, U), Z(lambda1, lambda2, lambda3, G1, G2, U))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 366,
|
|||
|
"id": "207b4224",
|
|||
|
"metadata": {
|
|||
|
"scrolled": false
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"O, my, Oerr, myerr = [], [], [], []\n",
|
|||
|
"C = np.arange(3, 50)\n",
|
|||
|
"\n",
|
|||
|
"for c in C:\n",
|
|||
|
" np.random.seed(42)\n",
|
|||
|
" lambda1 = np.random.normal(size=(c, c))\n",
|
|||
|
" lambda2 = np.random.normal(size=(c, c))\n",
|
|||
|
" lambda3 = np.random.normal(size=(c, c))\n",
|
|||
|
" G1 = np.random.normal(size=(c, c, c))\n",
|
|||
|
" G2 = np.random.normal(size=(c, c, c))\n",
|
|||
|
" U = np.random.normal(size=(c, c, c, c))\n",
|
|||
|
" \n",
|
|||
|
"\n",
|
|||
|
" time1 = %timeit -o -q -n 2 -r 2 mY_Z(lambda1, lambda2, lambda3, G1, G2, U)\n",
|
|||
|
" time2 = %timeit -o -q -n 2 -r 2 Z(lambda1, lambda2, lambda3, G1, G2, U)\n",
|
|||
|
" \n",
|
|||
|
" my.append(time1.average)\n",
|
|||
|
" myerr.append(3*time1.stdev)\n",
|
|||
|
" O.append(3*time2.average)\n",
|
|||
|
" Oerr.append(time2.stdev)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 367,
|
|||
|
"id": "f7cb2b04",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAYAAACutU7EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAA2DklEQVR4nO3dd5hU5dnH8e9NB1FAQMAYxK5gwbgWrFhIwViixhYLMUosQYkliYmvLWoSotgJYGJAjIlYghogooKKgmUBRbArYBRpUkRZXBbu94/njAzjttmdmTPl97muc83OOc+cuWcv9uGep5q7IyIiIlJImsQdgIiIiEi6lMCIiIhIwVECIyIiIgVHCYyIiIgUHCUwIiIiUnCUwIiIiEjBUQIjIiIFz8zmm9mofL9nPd6zr5m5mfXN5fsWIiUwkjVmNiD6Q3QzO6SGMu9H15/NcXgiki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2oRYRyZlEAtOuXaNvpQRGpATNnAm77gqbbRZ3JCJSUlauDDMHMjB7QAmMSAmaMUPjX0QkBitWZKT7CJTAiJScRYvCIF6NfxGRnMvQNgKgBEak5MyaFR6VwIhIzimBEZGGSmwhsPfe8cYhIiVICYyINFR5Oey0E2yxRdyRiEjJUQIjIg3hDtOnwwEHxB2JiJQkJTAi0hDz58PixdCnT9yRNJ6ZHWpmj5vZJ2bmZjagjvJ9zewxM/vUzNaY2WwzO6eaMl7NsWtWP4xIKXDPaALTLCN3EZGCMH16eCyGBAZoC8wB7ouOuhwIvAEMAT4FvgeMNLO17v5AStlewPKk50sbH65IiauogHXrlMCISPqmTQuL1+2+e9yRNJ67TwAmAJjZqHqUvynl1F/
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 576x288 with 2 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAYAAACutU7EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAA3wElEQVR4nO3dd5hU5dnH8e+9DXbpUpYmgkAEK5qNXVxFJGLFEo0VxYLY0KiYaDQaX31jwd4VBdEEg8bYG0pQUXgXUQQioosoHaQKS9nd5/3jmWGHYXbZgdk5M7O/z3Wd68ypc88cYG6eas45RERERNJJVtABiIiIiMRLCYyIiIikHSUwIiIiknaUwIiIiEjaUQIjIiIiaUcJjIiIiKSdnKADCFKrVq1c586dgw5DREREYpgyZcoy51zrWMfqdQLTuXNnSkpKgg5DREREYjCzudUdUxWSiIiIpB0lMCIiIpJ2lMCIiIhI2klqAmNmvc3sNTObb2bOzAZu4/zOofOil99GnXe4mU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a69l9uzZNGnShHfeeYeHHnqIm2++efM5F154Ic888wzXX389AO+++y5Llizh7LPPru62IiIiVUpLYdddA3lrlcBkqBYtWjBgwABGjBjByJEjKS4u3qr9y3nnnUdpaSkTJ04EfCnNSSedRMuWLYMIWURE0olzgSYwKoGJw/aUhEBggxRywQUXcN5559G4cWNuu+22rY63bt2aE044gREjRrDbbrvx2muv8frrryc3SBERSU8//wxr1iiBkcTr06cPeXl5LFu2jJNOOinmORdddBGnnnoqu+66K23btuWoo45KbpAiIpKewj2QlMBIopkZ06ZNwzlHgwYNYp7Tt29fWrZsya233soNN9xAVgANsUREJA0FnMDo1yrDNWnShKZNm1Z73Mw4//zz2bRpE+eff34SIxMRkbQWTmC6dAnk7VUCk2Gee+65uI8vXLiQPn360Llz5zqJSUREMlBpKRQWQmjA1GRTAlOPrVq1ipkzZzJq1KgtBr0TERHZpgB7IIESmKRIdu+j2jrxxBOZPHkygwYN4thjjw06HBERSSelpXDooYG9vRKYemx8qmZWIiKS2jZuhJ9+CrQERo14RUREJD4//giVlUpgREREJI0E3IUalMCIiIhIvJTAiIiISNopLYUGDaB9+8BCUAIjIiIi8Skt9QPYBTh6uxKYZCgurprRUUREJN0FPAYMKIGR7TB+/HjMjGXLlu3QfQYOHMhxxx2XoKhiO+644xg4cGCdvoeISL3iHHz/vRIYqRvz58/n4osvpmPHjuTl5dGhQwcuuugi5s2bF9d9OnfuzD333LPFvoMPPpiFCxfSsmXLHYrxgQceYPTo0Tt0DxERSbIVK2D1aiUwknhz5syhqKiI6dOnM3LkSL777jtGjx7NjBkz+M1vfsMPP/ywQ/fPy8ujbdu2mNkO3adZs2Y0b958h+4hIiJJlgI9kEAJTEa67LLLyMrK4oMPPqBPnz506tSJI444gg8++ICsrCwuu+wyAIqLixk8eDBXXXUVLVq0oEWLFlx33XVUVlZuPj537lyuu+46zGxzwhJdhfTcc8/RuHFj3n77bXr06EFBQQEnnHACq1atYuzYsXTv3p1mzZpxzjnnUFZWtjnO6CqkCRMmc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G2/k1VdfpV+/flRWVnLhhRdy9dVXb3Vdhw4d+PbbbxPzQUREpGalpXDwwUFHkV6NeM3sOKAP8GTE7oXApfhSmZOBWcA4MzusmntcbGYlZlaydGnia5mC1rJlS/r168ejjz7KunXrtji2bt06HnnkEY455hh22mknACZNmrS5JAPg888/p3379ptLX/Ly8qioqEha/Pvssw/Dhg1j/PjxFBcXM3LkSAD
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 576x288 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plt.rcParams.update({'font.size': 14})\n",
|
|||
|
"plt.figure(figsize=(8,4))\n",
|
|||
|
"\n",
|
|||
|
"plt.subplot(121)\n",
|
|||
|
"plt.plot(C, my, color=\"blue\")\n",
|
|||
|
"plt.title(\"My\")\n",
|
|||
|
"plt.xlabel(\"$\\chi$\")\n",
|
|||
|
"plt.ylabel(\"time,sec\")\n",
|
|||
|
"\n",
|
|||
|
"plt.subplot(122)\n",
|
|||
|
"plt.plot(C, O, color=\"red\")\n",
|
|||
|
"plt.title(\"Optimised\")\n",
|
|||
|
"plt.xlabel(\"$\\chi$\")\n",
|
|||
|
"plt.ylabel(\"time,sec\")\n",
|
|||
|
"\n",
|
|||
|
"plt.tight_layout()\n",
|
|||
|
"\n",
|
|||
|
"plt.figure(figsize=(8,4))\n",
|
|||
|
"plt.errorbar(C, my, myerr, color=\"blue\", label=\"My\")\n",
|
|||
|
"plt.errorbar(C, O, Oerr, color=\"red\", label=\"Optimised\")\n",
|
|||
|
"plt.xlabel(\"$\\chi$\")\n",
|
|||
|
"plt.ylabel(\"time,sec\")\n",
|
|||
|
"\n",
|
|||
|
"plt.legend()\n",
|
|||
|
"\n",
|
|||
|
"plt.tight_layout()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "1b180a50",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Задача 7"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 64,
|
|||
|
"id": "1a00a71e",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"N = 1000\n",
|
|||
|
"K = 3\n",
|
|||
|
"d = 3\n",
|
|||
|
"L = 10\n",
|
|||
|
"\n",
|
|||
|
"np.random.seed(42)\n",
|
|||
|
"mu_true = np.random.uniform(-L, L, size = (K, d))\n",
|
|||
|
"data = np.random.normal(mu_true, size = (N, K, d))\n",
|
|||
|
"data = np.vstack(data)\n",
|
|||
|
"np.random.shuffle(data)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 65,
|
|||
|
"id": "381a2bc2",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def scatter_plot(data, col=None):\n",
|
|||
|
" from mpl_toolkits.mplot3d import Axes3D \n",
|
|||
|
" %matplotlib inline\n",
|
|||
|
" fig = plt.figure()\n",
|
|||
|
" ax = fig.add_subplot(111, projection='3d')\n",
|
|||
|
" ax.scatter(data[:,0], data[:,1], data[:,2], s = 0.5, color=col)\n",
|
|||
|
" plt.show()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 66,
|
|||
|
"id": "1cc70b18",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPYAAADwCAYAAAAzS5nVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAABmq0lEQVR4nO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AZ7vHOeydYUMnXICsHNVDi92O3AHwoREGV9IYm3xtCvvD4n83+FRbt5Tw4bSbB45OoIvEKQqw8jw8DAul4uMjAzVbTebzXN+bstJ7ERd8eXGWUPs6HE68TzpF0psr9eL2+2mqKiITZs2JXQzKZY+FTegViPwgb1VaDXzn2uh15RlmXF3kKIsA2POAIdOTeELibyldrrZxO4Ocrh/Ck8gTGG2AYcnxKs9E+zMnr7Ji7MNFJn13PNcD55giNv2VmH3BGkbdvGm+gJqCzLJNU0/FLdVWgiJEqWl2RSXlNA/4aPAKOOcmqSvr091XxWim0ymGWuNt058MZCIK74StlhnBbFjjdOJBwsh9ujoKCdPnlTLMRP9ghLVPeuxeXAFRBoqYkfY5yO11RUguACnRJRkXh+YIhCW+EO7lVt2VdBYlUOPzc19f7Nz/dZSBEGgoSKL/335FKvyTDi9IYqyjYxM+fndeIjCjnEeOjJMMCwx6vRhc4exOQNsKM/mtb4pcjP1XL2pWL1mYZaBbz3fw9ZKC1vKs/nFq4O8f28l6yoqqKioUMUmHA4HJ06cIBAIYLFYVKLLsrysxE7EYi83uVc8sePV9I4FrVYbd1RckiQ6Ozvx+Xzs3r2blpaWlKuoRGLM6WfC7adr3EfzsAtBlui
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 432x288 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"if d == 3:\n",
|
|||
|
" scatter_plot(data, None)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 67,
|
|||
|
"id": "e32355c0",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def naive_dist_i(x, mu):\n",
|
|||
|
" # x: N datapoints, mu: N cluster centers\n",
|
|||
|
" # returns: D_{i}, squared distances from x[i] to mu[i]\n",
|
|||
|
" dist = np.zeros(x.shape[0])\n",
|
|||
|
" for i in range(x.shape[0]):\n",
|
|||
|
" dist[i] = np.sum((x[i] - mu[i])**2)\n",
|
|||
|
" return dist\n",
|
|||
|
"\n",
|
|||
|
"def naive_dist_ij(x, mu):\n",
|
|||
|
" # x: N datapoints, mu: K cluster centers\n",
|
|||
|
" # returns: D_{ij}, squared distances from x[i] to mu[j]\n",
|
|||
|
" dist = np.zeros((x.shape[0], mu.shape[0]))\n",
|
|||
|
" for i in range(x.shape[0]):\n",
|
|||
|
" for j in range(mu.shape[0]):\n",
|
|||
|
" dist[i, j] += np.sum((x[i] - mu[j])**2)\n",
|
|||
|
" return dist\n",
|
|||
|
"\n",
|
|||
|
"def naive_kmeans():\n",
|
|||
|
" ss_list=[]\n",
|
|||
|
" mu = data[np.random.choice(range(data.shape[0]), K, replace=False)]\n",
|
|||
|
" c = np.random.randint(low=0, high=K-1, size=data.shape[0])\n",
|
|||
|
" for n in range(10):\n",
|
|||
|
" c = np.argmin(naive_dist_ij(data, mu), axis = 1) \n",
|
|||
|
" ss = np.mean(naive_dist_i(data, mu[c]))\n",
|
|||
|
" ss_list.append(ss) \n",
|
|||
|
" for i in range(K):\n",
|
|||
|
" cluster_members = data[c == i]\n",
|
|||
|
" cluster_members = cluster_members.mean(axis = 0)\n",
|
|||
|
" mu[i] = cluster_members\n",
|
|||
|
" return ss_list, c"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 79,
|
|||
|
"id": "6b0e811d",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def dist_i(x, mu):\n",
|
|||
|
" # x: N datapoints, mu: N cluster centers\n",
|
|||
|
" # returns: D_{i}, squared distances from x[i] to mu[i]\n",
|
|||
|
" return np.sum((x - mu)**2)\n",
|
|||
|
"\n",
|
|||
|
"def dist_ij(x, mu):\n",
|
|||
|
" # x: N datapoints, mu: K cluster centers\n",
|
|||
|
" # returns: D_{ij}, squared distances from x[i] to mu[j]\n",
|
|||
|
" n, k = x.shape\n",
|
|||
|
" x_mod = np.transpose(x[None,...].repeat(k, axis=0), axes=(1, 0, 2))\n",
|
|||
|
" mu_mod = mu[None, ...]\n",
|
|||
|
" return np.sum((x_mod-mu_mod)*(x_mod-mu_mod), axis=2)\n",
|
|||
|
" \n",
|
|||
|
"def kmeans():\n",
|
|||
|
" ss_list=[]\n",
|
|||
|
" mu = data[np.random.choice(range(data.shape[0]), K, replace=False)]\n",
|
|||
|
" c = np.random.randint(low=0, high=K-1, size=data.shape[0])\n",
|
|||
|
" \n",
|
|||
|
" for _ in range(10):\n",
|
|||
|
" c = np.argmin(dist_ij(data, mu), axis = 1) \n",
|
|||
|
" ss = np.mean(dist_i(data, mu[c]))\n",
|
|||
|
" ss_list.append(ss) \n",
|
|||
|
" for i in range(K):\n",
|
|||
|
" mu[i] = data[c==i].mean(axis=0)\n",
|
|||
|
" return ss_list, c"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 82,
|
|||
|
"id": "84416419",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"885 ms ± 51.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/var/folders/h9/ll8t8frd1r575llt96l1glhm0000gn/T/ipykernel_21445/3230926319.py:24: RuntimeWarning: Mean of empty slice.\n",
|
|||
|
" mu[i] = data[c==i].mean(axis=0)\n",
|
|||
|
"/Users/goloshch/.conda/envs/NPM/lib/python3.10/site-packages/numpy/core/_methods.py:181: RuntimeWarning: invalid value encountered in true_divide\n",
|
|||
|
" ret = um.true_divide(\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"7.57 ms ± 419 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"%timeit naive_kmeans()\n",
|
|||
|
"%timeit kmeans()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 83,
|
|||
|
"id": "84fb1f46",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPYAAADwCAYAAAAzS5nVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAABjO0lEQVR4nO29eXgcZ5nu/Xurqru1tvZ9seV93+0sBBJCFpKY2GENOSTDECAznMnkY2ACDMMAhwFCgBMYBhiGgYE5wIQkdggkmUAChC2QxMa2ZMu2LFuyFmvf1VIvVfV+f7Sq3Gq3pF4l2e77unxZW3W9XV13Pc/7LPcjpJSkkUYalxaUhV5AGmmkkXykiZ1GGpcg0sROI41LEGlip5HGJYg0sdNI4xKENsfv0yHzNNJIPUSyXzBtsdNI4xJEmthppHEJIk3sNNK4BJEmdhppXIJIEzuNNC5BpImdRhqXINLETiONSxBpYqeRxiWINLHTSOMSRJrYaaRxCSJN7DTSuASRJnYaaVyCSBM7jTQuQaSJnUYalyDSxE4jjUsQaWIvAKSUBAIBDMMgrRKbRiowl9BCGkmGaZoEAgEmJycBEEKgaZr9T1EUhEh6330alxnEHBYjbU6SBCklhmEQCAQA0HXd/rlpmvbfWUR3OByoqpom+uWBpH/AaWLPA0Jdb4ukFsEj/a1pmkgpEUIghMDhcNgW3fpZGpcU0sS+2GCaJn6/fxpRp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qGhIekqKqE4B/QrCsdUlQOKArpOk6Kw2jTZMXVeL3BKUVhjmoRuBAaAH2kae3Sdf3M6KS8o4OoY1zk2JvjD7114vYIlS3QO/dl
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 432x288 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"ss_list, c = kmeans()\n",
|
|||
|
"plt.plot(ss_list)\n",
|
|||
|
"\n",
|
|||
|
"colors = np.array([plt.cm.cool(i/(K-1)) for i in range(K)])\n",
|
|||
|
"if d == 3:\n",
|
|||
|
" scatter_plot(data, colors[c])"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "717e0a58",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__Вывод:__\n",
|
|||
|
"\n",
|
|||
|
"- При N=1000 naive_kmeans работает за 874 ms ± 77.6 ms ms, kmeans за 7.7 ms ± 265 µs, т.е. быстрее примерно в 100 раз\n",
|
|||
|
"\n",
|
|||
|
"- naive_kmeans для N=10000 мой компухтер уже не вывозит"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "3f82aca7",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Задача 8"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 69,
|
|||
|
"id": "46c3a97e",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def HWseq1(n):\n",
|
|||
|
" if n > 2:\n",
|
|||
|
" a = np.zeros(n+1, dtype=int)\n",
|
|||
|
" a[1] = a[2] = 1\n",
|
|||
|
" for i in range(3, n+1):\n",
|
|||
|
" a[i] = a[a[i-1]] + a[i-a[i-1]] \n",
|
|||
|
" else:\n",
|
|||
|
" a = np.array([0, 1, 1])[1:n+1]\n",
|
|||
|
" \n",
|
|||
|
" return a[1:]\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"def HWseq2(n):\n",
|
|||
|
" if n > 2:\n",
|
|||
|
" a = [0, 1, 1]\n",
|
|||
|
" a[1] = a[2] = 1\n",
|
|||
|
" for i in range(3, n+1):\n",
|
|||
|
" a.append(a[a[i-1]] + a[i-a[i-1]])\n",
|
|||
|
" else:\n",
|
|||
|
" a = [0, 1, 1][1:n+1]\n",
|
|||
|
" \n",
|
|||
|
" return np.array(a[1:])\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"@jit(nopython=True)\n",
|
|||
|
"def HWseq3(n):\n",
|
|||
|
" if n > 2:\n",
|
|||
|
" a = np.zeros(n+1, dtype=np.int32)\n",
|
|||
|
" a[1] = a[2] = 1\n",
|
|||
|
" for i in range(3, n+1):\n",
|
|||
|
" a[i] = a[a[i-1]] + a[i-a[i-1]] \n",
|
|||
|
" else:\n",
|
|||
|
" a = np.array([0, 1, 1], dtype=np.int32)[1:n+1]\n",
|
|||
|
" \n",
|
|||
|
" return a[1:]"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 70,
|
|||
|
"id": "f89b5663",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"79 ms ± 2.52 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
|
|||
|
"32.8 ms ± 258 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
|
|||
|
"520 µs ± 10.7 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"n = 10**5\n",
|
|||
|
"\n",
|
|||
|
"%timeit HWseq1(n)\n",
|
|||
|
"%timeit HWseq2(n)\n",
|
|||
|
"%timeit HWseq3(n)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 73,
|
|||
|
"id": "8465b982",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"686 ms ± 21.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
|
|||
|
"52736107\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmUAAAFSCAYAAACzGKivAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAuKklEQVR4nO3debhdZX3//fc3CSFMMiUoQkIYggQqMkRwQMA6oVRwRFCrIAV9+qC1qIitRR6qVfn5q0MVFRVRKyBwtZgqlYJCQRnDIIUgIQSEBIEkJIEMZDrf54977Z6dnTPsk5yzz0rO+3Vd+zp7DXut+6y1196ffd/3WisyE0mSJA2vUcNdAEmSJBnKJEmSasFQJkmSVAOGMkmSpBowlEmSJNWAoUySJKkGDGXSZiQiRkXEdyNiYURkRBzdwXVPq9Y5uVPrlKTNiaFMGmYRcXFE/KKH8RsSct4CnAK8FdgVuLmfdZ8cEUsHUt52DeWym9YxNiI+FRF3R8TyiHgmIm6NiA9HxJZDuW5JGmxjhrsAkgbVPsCfMrPPMLapiYgtMnN1y7ixwDXAwcA5wE3AYuDlwJnAg8ANHS2oJG0Ea8qkTUhEHBkRt0XE8xHxVER8tQonRMTFwFeBSVUN26NNr7k1IpZGxJKIuD0i/qxq2vwhsE01f0bEudVr3h8Rd0TEcxHxdERcERG7tZTlmIj4Q1WWm4B9m6b1teyxEfHliJhb1W7d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BdKkiSpE8Z0akURcSlwNDA+IuYCnwO2AMjM7wBXA28BZgPLgVM6VTZJkqTh1rFQlpkn9TM9gf+3Q8WRJEmqlTo1X0qSJI1YhjJJkqQaMJRJkiTVgKFMkiSpBgxl7coc7hJIkqTNmKGsPxH
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 720x360 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"n=10**8\n",
|
|||
|
"\n",
|
|||
|
"x = np.arange(1, n+1)\n",
|
|||
|
"%timeit y = HWseq3(n)\n",
|
|||
|
"\n",
|
|||
|
"plt.figure(figsize=(10,5))\n",
|
|||
|
"plt.plot(x, y/x, color=\"red\")\n",
|
|||
|
"plt.ylabel(\"a(n)/n\", fontsize=fontsize)\n",
|
|||
|
"plt.xlabel(\"n\", fontsize=fontsize)\n",
|
|||
|
"plt.title(\"Hofstadter-Conway sequence\", fontsize=fontsize)\n",
|
|||
|
"\n",
|
|||
|
"print(y[-1])"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "af6b8830",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"__Вывод:__\n",
|
|||
|
"\n",
|
|||
|
"- $a(10^8) = 52736107$\n",
|
|||
|
"- $jit$-версия работает лучше всего"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"kernelspec": {
|
|||
|
"display_name": "Python 3 (ipykernel)",
|
|||
|
"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.0"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 5
|
|||
|
}
|