gann(go-approximate-nearest-neighbor) is a library for Approximate Nearest Neighbor Search written in Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
index
item
node
Gopkg.lock
Gopkg.toml
LICENSE.txt
README.md
gann.go
gann_test.go

README.md

gann

CircleCI MIT License

image

gann (go-approximate-nearest-neighbor) is a library for approximate nearest neighbor search purely written in golang.

The implemented algorithm is truly inspired by Annoy (https://github.com/spotify/annoy).

feature

  1. ONLY written in golang, no dependencies out of go world.
  2. easy to tune with a bit of parameters
  3. ONLY support for cosine similarity search. (issue: https://github.com/mathetake/gann/issues/12)

usage

import (
	"fmt"
	"github.com/mathetake/gann/index"
	"math/rand"
	"time"
)

func main() {
	var dim = 3
	var nTrees = 2
	var k = 10
	var nItem = 1000

	rawItems := make([][]float32, 0, nItem)
	rand.Seed(time.Now().UnixNano())

	for i := 0; i < nItem; i++ {
		item := make([]float32, 0, dim)
		for j := 0; j < dim; j++ {
			item = append(item, rand.Float32())
		}
		rawItems = append(rawItems, item)
	}

	// create index
	gIDx := index.GetIndex(rawItems, dim, nTrees, k, true)
	gIDx.Build()

	// do search
	q := []float32{0.1, 0.02, 0.001}
	ann, _ := gIDx.GetANNbyVector(q, 5, 10)
	fmt.Println("result:", ann)
}

You can also save and load your index to/from disk:

gIDx := index.GetIndex(rawItems, dim, nTrees, k, true)
gIDx.Build()

var path = "foo.gann"

err := gIDx.Save(path)
if err != nil {
    panic(err)
}

var idx = &index.Index{}
err := idx.Load(path)
if err != nil {
    panic(err)
}

parameters

See the blog post describing the parameters and algorithms in gann :

https://mathetake.github.io/blogs/gann.html

references

License

MIT