|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from sklearn.datasets import make_classification\n", |
| 10 | + "import numpy as np\n", |
| 11 | + "import time\n", |
| 12 | + "import faiss\n", |
| 13 | + "\n", |
| 14 | + "import torch" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 2, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "data": { |
| 24 | + "text/plain": [ |
| 25 | + "((100, 256), (100,))" |
| 26 | + ] |
| 27 | + }, |
| 28 | + "execution_count": 2, |
| 29 | + "metadata": {}, |
| 30 | + "output_type": "execute_result" |
| 31 | + } |
| 32 | + ], |
| 33 | + "source": [ |
| 34 | + "# X, y = make_classification(\n", |
| 35 | + "# n_samples=100000, n_features=4, n_classes=4, n_clusters_per_class=2, random_state=1024, n_informative=4, n_redundant = 0\n", |
| 36 | + "# )\n", |
| 37 | + "features = 256\n", |
| 38 | + "clusters = 256\n", |
| 39 | + "X, y = make_classification(\n", |
| 40 | + " n_samples=100, n_features=features, n_classes=2, random_state=1024, n_informative=features, n_redundant=0, n_clusters_per_class = 1\n", |
| 41 | + ")\n", |
| 42 | + "X.shape, y.shape" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 3, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "xy = np.concatenate((X, y.reshape(-1, 1)), axis=1)\n", |
| 52 | + "# np.savetxt(\"test_clusters_\" + str(clusters) + \".csv\", xy, delimiter=\",\")\n", |
| 53 | + "np.savetxt(\"test_samples.csv\", xy, delimiter=\",\")" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 137, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + "used 50 iterations (17.7545s) to cluster 100000 items into 256 clusters\n", |
| 66 | + "KMeans fit time: 17754.658460617065 ms\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "from fast_pytorch_kmeans import KMeans\n", |
| 72 | + "\n", |
| 73 | + "kmeans = KMeans(n_clusters=clusters, mode='euclidean', verbose=1, tol = -1, max_iter=50)\n", |
| 74 | + "x = torch.from_numpy(X).to(\"cuda\")\n", |
| 75 | + "\n", |
| 76 | + "start_time = time.time()\n", |
| 77 | + "labels = kmeans.fit_predict(x)\n", |
| 78 | + "# 记录结束时间\n", |
| 79 | + "end_time = time.time()\n", |
| 80 | + "\n", |
| 81 | + "# 计算并输出fit方法所需的时间\n", |
| 82 | + "fit_time = end_time - start_time\n", |
| 83 | + "print(f\"KMeans fit time: {fit_time * 1000} ms\")" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 138, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "name": "stdout", |
| 93 | + "output_type": "stream", |
| 94 | + "text": [ |
| 95 | + "Clustering 100000 points in 1024D to 256 clusters, redo 1 times, 50 iterations\n", |
| 96 | + " Preprocessing in 0.04 s\n", |
| 97 | + "KMeans fit time: 3554.8036098480225 ms: objective=3.40156e+10 imbalance=1.259 nsplit=0 \n", |
| 98 | + "\n" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "d = X.shape[1]\n", |
| 104 | + "clus = faiss.Clustering(d, clusters)\n", |
| 105 | + "clus.verbose = True\n", |
| 106 | + "clus.niter = 50\n", |
| 107 | + "# otherwise the kmeans implementation sub-samples the training set\n", |
| 108 | + "clus.max_points_per_centroid = 10**9\n", |
| 109 | + "cfg = faiss.GpuIndexFlatConfig()\n", |
| 110 | + "cfg.useFloat16 = False\n", |
| 111 | + "cfg.device = 0\n", |
| 112 | + "index = faiss.GpuIndexFlatL2(faiss.StandardGpuResources(), d, cfg)\n", |
| 113 | + "start_time = time.time()\n", |
| 114 | + "clus.train(X, index)\n", |
| 115 | + "# 记录结束时间\n", |
| 116 | + "end_time = time.time()\n", |
| 117 | + "#计算并输出fit方法所需的时间\n", |
| 118 | + "fit_time = end_time - start_time\n", |
| 119 | + "print(f\"KMeans fit time: {fit_time * 1000} ms\")" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 134, |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [], |
| 127 | + "source": [ |
| 128 | + "# ncentroids = clusters\n", |
| 129 | + "# niter = 50\n", |
| 130 | + "# verbose = True\n", |
| 131 | + "# d = X.shape[1]\n", |
| 132 | + "# kmeans = faiss.Kmeans(d, ncentroids, niter=niter, verbose=verbose, nredo = 1, gpu=True)\n", |
| 133 | + "# kmeans.max_points_per_centroid = 10**9\n", |
| 134 | + "# start_time = time.time()\n", |
| 135 | + "# kmeans.train(X)\n", |
| 136 | + "# # 记录结束时间\n", |
| 137 | + "# end_time = time.time()\n", |
| 138 | + "# # 计算并输出fit方法所需的时间\n", |
| 139 | + "# fit_time = end_time - start_time\n", |
| 140 | + "# print(f\"KMeans fit time: {fit_time * 1000} ms\")" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [] |
| 149 | + } |
| 150 | + ], |
| 151 | + "metadata": { |
| 152 | + "kernelspec": { |
| 153 | + "display_name": "dl", |
| 154 | + "language": "python", |
| 155 | + "name": "python3" |
| 156 | + }, |
| 157 | + "language_info": { |
| 158 | + "codemirror_mode": { |
| 159 | + "name": "ipython", |
| 160 | + "version": 3 |
| 161 | + }, |
| 162 | + "file_extension": ".py", |
| 163 | + "mimetype": "text/x-python", |
| 164 | + "name": "python", |
| 165 | + "nbconvert_exporter": "python", |
| 166 | + "pygments_lexer": "ipython3", |
| 167 | + "version": "3.10.13" |
| 168 | + } |
| 169 | + }, |
| 170 | + "nbformat": 4, |
| 171 | + "nbformat_minor": 2 |
| 172 | +} |
0 commit comments