Created
August 27, 2019 08:30
-
-
Save mdouze/dd11f1ebd2f1c2f3bcd74beee303e513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import faiss" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"8" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": { | |
"bento_obj_id": "139916685704704" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"faiss.get_num_gpus()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"d = 256\n", | |
"quantizer = faiss.IndexBinaryFlat(d)\n", | |
"index = faiss.IndexBinaryIVF(quantizer, d, 4096)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"xt = faiss.randint((100000, 256 // 8)).astype('uint8')\n", | |
"xt.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1min 35s, sys: 15.8 s, total: 1min 51s\n", | |
"Wall time: 11.2 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"index.train(xt)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"quantizer2 = faiss.IndexBinaryFlat(d)\n", | |
"index2 = faiss.IndexBinaryIVF(quantizer2, d, 4096)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"clustering_index = faiss.index_cpu_to_all_gpus(faiss.IndexFlatL2(d))\n", | |
"index2.clustering_index = clustering_index" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.33 s, sys: 697 ms, total: 2.03 s\n", | |
"Wall time: 690 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"index2.train(xt)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"bento_stylesheets": { | |
"bento/extensions/flow/main.css": true, | |
"bento/extensions/kernel_selector/main.css": true, | |
"bento/extensions/kernel_ui/main.css": true, | |
"bento/extensions/new_kernel/main.css": true, | |
"bento/extensions/system_usage/main.css": true, | |
"bento/extensions/theme/main.css": true | |
}, | |
"kernelspec": { | |
"display_name": "pytorch", | |
"language": "python", | |
"name": "bento_kernel_pytorch" | |
}, | |
"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.6.3rc1+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
BTW: provided trained index, would you recommend to cpu or gpu for search?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice of you !