-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
readme, added PopAndPush in distqueue
- Loading branch information
Oscar Franzen
committed
Mar 16, 2017
1 parent
75ebd02
commit adb7d6c
Showing
4 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,58 @@ | ||
# go-hnsw | ||
|
||
go-hnsw is a GO implementation of the HNSW approximate nearest-neighbour search algorithm implemented in C++ in https://github.com/searchivarius/nmslib and described in https://arxiv.org/abs/1603.09320 | ||
go-hnsw is a GO implementation of the HNSW approximate nearest-neighbour search algorithm implemented in C++ in https://github.com/searchivarius/nmslib and described in https://arxiv.org/abs/1603.09320 | ||
|
||
## Usage | ||
|
||
Simple usage example. Note that both index building and searching can be safely done in parallel with multiple goroutines. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/Bithack/go-hnsw" | ||
) | ||
|
||
func main() { | ||
|
||
const ( | ||
M = 32 | ||
efConstruction = 400 | ||
efSearch = 100 | ||
K = 10 | ||
) | ||
|
||
var zero hnsw.Point = make([]float32, 128) | ||
|
||
h := hnsw.New(M, efConstruction, &zero) | ||
h.Grow(10000) | ||
|
||
for i := 0; i < 10000; i++ { | ||
h.Add(randomPoint(), uint32(i)) | ||
if (i+1)%1000 == 0 { | ||
fmt.Printf("%v points added\n", i+1) | ||
} | ||
} | ||
|
||
start := time.Now() | ||
for i := 0; i < 1000; i++ { | ||
Search(randomPoint, efSearch, K) | ||
} | ||
stop := time.Since(start) | ||
|
||
fmt.Printf("%v queries / second (single thread)\n", 1000.0/stop.Seconds()) | ||
} | ||
|
||
func randomPoint() *hnsw.Point { | ||
var v hnsw.Point = make([]float32, 128) | ||
for i := range v { | ||
v[i] = rand.Float32() | ||
} | ||
return &v | ||
} | ||
|
||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
".." | ||
) | ||
|
||
func main() { | ||
|
||
const ( | ||
M = 32 | ||
efConstruction = 400 | ||
efSearch = 100 | ||
K = 10 | ||
) | ||
|
||
var zero hnsw.Point = make([]float32, 128) | ||
|
||
h := hnsw.New(M, efConstruction, &zero) | ||
h.Grow(10000) | ||
|
||
for i := 0; i < 10000; i++ { | ||
h.Add(randomPoint(), uint32(i)) | ||
if (i+1)%1000 == 0 { | ||
fmt.Printf("%v points added\n", i+1) | ||
} | ||
} | ||
|
||
fmt.Printf("Generating queries and calculating true answers using bruteforce search...\n") | ||
queries := make([]*hnsw.Point, 1000) | ||
truth := make([][]uint32, 1000) | ||
for i := range queries { | ||
queries[i] = randomPoint() | ||
result := h.SearchBrute(queries[i], K) | ||
truth[i] = make([]uint32, K) | ||
for j := K - 1; j >= 0; j-- { | ||
item := result.Pop() | ||
truth[i][j] = item.ID | ||
} | ||
} | ||
|
||
fmt.Printf("Now searching with HNSW...\n") | ||
hits := 0 | ||
start := time.Now() | ||
for i := 0; i < 1000; i++ { | ||
result := h.Search(queries[i], efSearch, K) | ||
for j := 0; j < K; j++ { | ||
item := result.Pop() | ||
for k := 0; k < K; k++ { | ||
if item.ID == truth[i][k] { | ||
hits++ | ||
} | ||
} | ||
} | ||
} | ||
stop := time.Since(start) | ||
|
||
fmt.Printf("%v queries / second (single thread)\n", 1000.0/stop.Seconds()) | ||
fmt.Printf("Average 10-NN precision: %v\n", float64(hits)/(1000.0*float64(K))) | ||
|
||
} | ||
|
||
func randomPoint() *hnsw.Point { | ||
var v hnsw.Point = make([]float32, 128) | ||
for i := range v { | ||
v[i] = rand.Float32() | ||
} | ||
return &v | ||
} |
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