Skip to content

Instantly share code, notes, and snippets.

@mdouze
Created August 30, 2019 08:20
Show Gist options
  • Save mdouze/ca65bce66f77cd2ef4df8769e19443a9 to your computer and use it in GitHub Desktop.
Save mdouze/ca65bce66f77cd2ef4df8769e19443a9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import faiss\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# training data\n",
"xt = np.random.rand(1000, 20).astype('float32')\n",
"# test data\n",
"x = np.random.rand(10, 20).astype('float32')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# make the PCA matrix\n",
"pca = faiss.PCAMatrix(20, 10)\n",
"pca.train(xt)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# apply it to test data\n",
"yref = pca.apply_py(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# extract matrix + bias from the PCA object\n",
"# works for any linear transform (OPQ, random rotation, etc.)\n",
"b = faiss.vector_to_array(pca.b)\n",
"A = faiss.vector_to_array(pca.A).reshape(pca.d_out, pca.d_in)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# apply transformation\n",
"ynew = x @ A.T + b"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {
"bento_obj_id": "140586613660256"
},
"output_type": "execute_result"
}
],
"source": [
"# are the vectors the same?\n",
"np.allclose(yref, ynew)"
]
},
{
"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
},
"disseminate_notebook_id": {
"notebook_id": "368435637443095"
},
"disseminate_notebook_info": {
"bento_version": "20190826-030256",
"description": "",
"hide_code": false,
"hipster_group": "",
"kernel_build_info": {
"error": "The file located at '/data/users/matthijs/fbsource/fbcode/bento/kernels/TARGETS' could not be found."
},
"no_uii": true,
"notebook_number": "138918",
"others_can_edit": false,
"reviewers": "",
"revision_id": "544712462932444",
"tags": "",
"tasks": "",
"title": "get_matrix_from_PCA"
},
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment