-
-
Save wilfoderek/d5a81f3341a60c11576afd08b135b3dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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": {}, | |
"outputs": [], | |
"source": [ | |
"import faiss\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"index = faiss.IndexIDMap2(faiss.IndexFlatL2(32))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# populate with random elements\n", | |
"ids = np.random.randint(0, 5000, size=10)\n", | |
"x = np.random.rand(10, 32).astype('float32')\n", | |
"index.add_with_ids(x, ids)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(array([[2.88299 , 3.3764014, 3.4181166, 3.5144272],\n", | |
" [3.3090184, 3.456828 , 3.9004102, 3.9349656],\n", | |
" [3.5935507, 3.9127915, 4.041091 , 4.4257126],\n", | |
" [2.756099 , 3.5408492, 3.6268148, 3.817906 ]], dtype=float32),\n", | |
" array([[2992, 2614, 74, 4195],\n", | |
" [3118, 4201, 2309, 2841],\n", | |
" [2992, 4365, 4201, 1886],\n", | |
" [ 608, 4201, 1137, 2841]]))" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": { | |
"bento_obj_id": "140041896012608" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# reference result\n", | |
"xq = np.random.rand(4, 32).astype('float32')\n", | |
"index.search(xq, 4)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Convert IndexIDMap2 to IndexIDMap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"index2 = faiss.IndexIDMap(faiss.IndexFlatL2(32))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# get the vectors and the ids\n", | |
"vecs = index.index.reconstruct_n(0, index.ntotal)\n", | |
"ids = faiss.vector_to_array(index.id_map)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# re-add them\n", | |
"index2.add_with_ids(vecs, ids)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(array([[2.88299 , 3.3764014, 3.4181166, 3.5144272],\n", | |
" [3.3090184, 3.456828 , 3.9004102, 3.9349656],\n", | |
" [3.5935507, 3.9127915, 4.041091 , 4.4257126],\n", | |
" [2.756099 , 3.5408492, 3.6268148, 3.817906 ]], dtype=float32),\n", | |
" array([[2992, 2614, 74, 4195],\n", | |
" [3118, 4201, 2309, 2841],\n", | |
" [2992, 4365, 4201, 1886],\n", | |
" [ 608, 4201, 1137, 2841]]))" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": { | |
"bento_obj_id": "140041896039760" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# check if same as reference result\n", | |
"index2.search(xq, 4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"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": "faiss", | |
"language": "python", | |
"name": "bento_kernel_faiss" | |
}, | |
"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.7.5+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment