Skip to content

Instantly share code, notes, and snippets.

@mdouze
Created September 2, 2019 11:43
Show Gist options
  • Select an option

  • Save mdouze/72221ab1937baa219545aff9ba7221c0 to your computer and use it in GitHub Desktop.

Select an option

Save mdouze/72221ab1937baa219545aff9ba7221c0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import faiss\n",
"import numpy as np\n",
"\n",
"feat = np.random.normal(loc=0.0, scale=1.0, size=(10000,512))\n",
"feat = feat.astype(np.float32)\n",
"M = 32\n",
"index = faiss.IndexPQ(d,M,4)\n",
"index.train(feat)\n",
"# get the PQ \n",
"pq = index.pq"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"# read the PQ centroids\n",
"cen = faiss.vector_to_array(pq.centroids)\n",
"cen = cen.reshape(pq.M, pq.ksub, pq.dsub)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(32, 16, 16)"
]
},
"execution_count": 34,
"metadata": {
"bento_obj_id": "140454597395440"
},
"output_type": "execute_result"
}
],
"source": [
"cen.shape"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"# change the centroids of one of the sub-quantizers\n",
"cen[5, :, :] = 0"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"# write back all centroids \n",
"faiss.copy_array_to_vector(cen.ravel(), index.pq.centroids)"
]
}
],
"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.6.3rc1+"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment