Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
F
faiss
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
faiss
Commits
c4ebee7f
Commit
c4ebee7f
authored
Dec 07, 2017
by
matthijs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added tuto for GPU
parent
e652a664
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
17 deletions
+57
-17
1-Flat.py
tutorial/python/1-Flat.py
+8
-8
2-IVFFlat.py
tutorial/python/2-IVFFlat.py
+6
-4
3-IVFPQ.py
tutorial/python/3-IVFPQ.py
+5
-5
4-GPU.py
tutorial/python/4-GPU.py
+38
-0
No files found.
tutorial/python/1-Flat.py
View file @
c4ebee7f
...
@@ -4,7 +4,9 @@
...
@@ -4,7 +4,9 @@
# This source code is licensed under the BSD+Patents license found in the
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
# LICENSE file in the root directory of this source tree.
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
d
=
64
# dimension
d
=
64
# dimension
nb
=
100000
# database size
nb
=
100000
# database size
nq
=
10000
# nb of queries
nq
=
10000
# nb of queries
...
@@ -16,16 +18,14 @@ xq[:, 0] += np.arange(nq) / 1000.
...
@@ -16,16 +18,14 @@ xq[:, 0] += np.arange(nq) / 1000.
import
faiss
# make faiss available
import
faiss
# make faiss available
index
=
faiss
.
IndexFlatL2
(
d
)
# build the index
index
=
faiss
.
IndexFlatL2
(
d
)
# build the index
print
index
.
is_trained
print
(
index
.
is_trained
)
index
.
add
(
xb
)
# add vectors to the index
index
.
add
(
xb
)
# add vectors to the index
print
index
.
ntotal
print
(
index
.
ntotal
)
k
=
4
# we want to see 4 nearest neighbors
k
=
4
# we want to see 4 nearest neighbors
D
,
I
=
index
.
search
(
xb
[:
5
],
k
)
# sanity check
D
,
I
=
index
.
search
(
xb
[:
5
],
k
)
# sanity check
print
I
print
(
I
)
print
D
print
(
D
)
D
,
I
=
index
.
search
(
xq
,
k
)
# actual search
D
,
I
=
index
.
search
(
xq
,
k
)
# actual search
print
I
[:
5
]
# neighbors of the 5 first queries
print
(
I
[:
5
])
# neighbors of the 5 first queries
print
I
[
-
5
:]
# neighbors of the 5 last queries
print
(
I
[
-
5
:])
# neighbors of the 5 last queries
tutorial/python/2-IVFFlat.py
View file @
c4ebee7f
...
@@ -4,7 +4,9 @@
...
@@ -4,7 +4,9 @@
# This source code is licensed under the BSD+Patents license found in the
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
# LICENSE file in the root directory of this source tree.
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
d
=
64
# dimension
d
=
64
# dimension
nb
=
100000
# database size
nb
=
100000
# database size
nq
=
10000
# nb of queries
nq
=
10000
# nb of queries
...
@@ -20,15 +22,15 @@ nlist = 100
...
@@ -20,15 +22,15 @@ nlist = 100
k
=
4
k
=
4
quantizer
=
faiss
.
IndexFlatL2
(
d
)
# the other index
quantizer
=
faiss
.
IndexFlatL2
(
d
)
# the other index
index
=
faiss
.
IndexIVFFlat
(
quantizer
,
d
,
nlist
,
faiss
.
METRIC_L2
)
index
=
faiss
.
IndexIVFFlat
(
quantizer
,
d
,
nlist
,
faiss
.
METRIC_L2
)
# here we specify METRIC_L2, by default it performs inner-product search
# here we specify METRIC_L2, by default it performs inner-product search
assert
not
index
.
is_trained
assert
not
index
.
is_trained
index
.
train
(
xb
)
index
.
train
(
xb
)
assert
index
.
is_trained
assert
index
.
is_trained
index
.
add
(
xb
)
# add may be a bit slower as well
index
.
add
(
xb
)
# add may be a bit slower as well
D
,
I
=
index
.
search
(
xq
,
k
)
# actual search
D
,
I
=
index
.
search
(
xq
,
k
)
# actual search
print
I
[
-
5
:]
# neighbors of the 5 last queries
print
(
I
[
-
5
:])
# neighbors of the 5 last queries
index
.
nprobe
=
10
# default nprobe is 1, try a few more
index
.
nprobe
=
10
# default nprobe is 1, try a few more
D
,
I
=
index
.
search
(
xq
,
k
)
D
,
I
=
index
.
search
(
xq
,
k
)
print
I
[
-
5
:]
# neighbors of the 5 last queries
print
(
I
[
-
5
:])
# neighbors of the 5 last queries
tutorial/python/3-IVFPQ.py
View file @
c4ebee7f
...
@@ -4,7 +4,9 @@
...
@@ -4,7 +4,9 @@
# This source code is licensed under the BSD+Patents license found in the
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
# LICENSE file in the root directory of this source tree.
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
d
=
64
# dimension
d
=
64
# dimension
nb
=
100000
# database size
nb
=
100000
# database size
nq
=
10000
# nb of queries
nq
=
10000
# nb of queries
...
@@ -25,10 +27,8 @@ index = faiss.IndexIVFPQ(quantizer, d, nlist, m, 8)
...
@@ -25,10 +27,8 @@ index = faiss.IndexIVFPQ(quantizer, d, nlist, m, 8)
index
.
train
(
xb
)
index
.
train
(
xb
)
index
.
add
(
xb
)
index
.
add
(
xb
)
D
,
I
=
index
.
search
(
xb
[:
5
],
k
)
# sanity check
D
,
I
=
index
.
search
(
xb
[:
5
],
k
)
# sanity check
print
I
print
(
I
)
print
D
print
(
D
)
index
.
nprobe
=
10
# make comparable with experiment above
index
.
nprobe
=
10
# make comparable with experiment above
D
,
I
=
index
.
search
(
xq
,
k
)
# search
D
,
I
=
index
.
search
(
xq
,
k
)
# search
print
I
[
-
5
:]
print
(
I
[
-
5
:])
tutorial/python/4-GPU.py
0 → 100644
View file @
c4ebee7f
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.
from
__future__
import
print_function
import
numpy
as
np
d
=
64
# dimension
nb
=
100000
# database size
nq
=
10000
# nb of queries
np
.
random
.
seed
(
1234
)
# make reproducible
xb
=
np
.
random
.
random
((
nb
,
d
))
.
astype
(
'float32'
)
xb
[:,
0
]
+=
np
.
arange
(
nb
)
/
1000.
xq
=
np
.
random
.
random
((
nq
,
d
))
.
astype
(
'float32'
)
xq
[:,
0
]
+=
np
.
arange
(
nq
)
/
1000.
import
faiss
# make faiss available
print
(
"number of GPUs:"
,
faiss
.
get_num_gpus
())
index
=
faiss
.
IndexFlatL2
(
d
)
# build the index
res
=
faiss
.
StandardGpuResources
()
index
=
faiss
.
index_cpu_to_gpu
(
res
,
0
,
index
)
index
.
add
(
xb
)
# add vectors to the index
print
(
index
.
ntotal
)
k
=
4
# we want to see 4 nearest neighbors
D
,
I
=
index
.
search
(
xb
[:
5
],
k
)
# sanity check
print
(
I
)
print
(
D
)
D
,
I
=
index
.
search
(
xq
,
k
)
# actual search
print
(
I
[:
5
])
# neighbors of the 5 first queries
print
(
I
[
-
5
:])
# neighbors of the 5 last queries
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment