在推荐或者搜索场景中,高质量的召回都是很有必要的,有时候ann搜索算法(Approximate Nearest Neighbor)可以帮助我们实现这一个功能
ngt是yahoo日本团队基于graph和tree做的的ann搜索工具,地址:https://github.com/yahoojapan/NGT
它实现了onng/pnng的算法,对应的论文
onng: https://arxiv.org/abs/1810.07355
pnng: https://www.aclweb.org/anthology/P16-1214.pdf
我最初接触近邻搜索算法是从kdtree开始的,kdtree的思路是二叉搜索树的思路,通过不断分割子空间,然后锁定部分子空间进行搜索。但是,kdtree是很容易碰到维度灾难的,实际项目中很少使用。github上比较有名的集中ann算法,都可以再这个benchmark找到

讯享网
开源的ann算法有很多,为什么选择ngt?
- 看上面的benchmark,它是可以保持优势的
- github上维护比较勤,代码的质量比较高
- ngt还有一个兄弟开源代码仓, ngtd,也就是ngt的server版本,主要开发语言是go,调用ngt提供的cgo binding,这个对于算法的工程化非常有用,因为c++的后端开发成本还是有点高的
快速入门
ngtd这里不展开讲,因为它跟算法相关的不多,这里主要讲ngt以及go调用ngt的binding
step1:
安装ngt
https://github.com/yahoojapan/NGT/releases
我用的是1.9.1
下载后解压,
cd NGT-x.x.x mkdir build cd build cmake .. make make install
讯享网
ngt的依赖很少,很容易就安装成功了,注意可能需要sudo权限

step2:
安装gongt https://github.com/yahoojapan/gongt
讯享网go get -u github.com/yahoojapan/gongt
step3
验证安装
随机生成128维的数据,生成100k条数据,生成索引,然后做10次ann搜索,看看耗时
由于用的是随机数,这里不验证准确性
测试代码
package main import ( "fmt" "os" "runtime" "github.com/yahoojapan/gongt" "math/rand" "time" ) func getVectors(dimension, rowCount int) [][]float64 {
vec := make([][]float64, rowCount) for i := 0; i < rowCount; i++ {
vec[i] = make([]float64, dimension) for j := 0; j < dimension; j++ {
vec[i][j] = rand.Float64() } } return vec } func create(indexName string) error{
vectors := getVectors(128, 100*1000) fmt.Printf("generate %d random vectors\n", len(vectors)) n := gongt.New(indexName).SetObjectType(gongt.Float).SetDimension(len(vectors[0])).Open() defer n.Close() for _, v := range vectors {
_, err := n.Insert(v) if err!=nil{
return err } } if err := n.CreateAndSaveIndex(runtime.NumCPU()); err != nil {
return err } return nil } func search(n *gongt.NGT) {
//n.GetDim() vectors := getVectors(128, 10) fmt.Printf("search %d items\n", len(vectors)) for k, v := range vectors {
result, err := n.Search(v, 10, gongt.DefaultEpsilon) if err != nil {
panic(err) } fmt.Printf("result[%v]:%+v \n", k, result) } } func main() {
rand.Seed(10) indexName := "testIndex" start := time.Now() _, err := os.Stat(indexName) if err != nil {
// not exist, then create database err := create(indexName) if err!=nil{
panic(err) } fmt.Printf("create duration:%v\n", time.Now().Sub(start)) start = time.Now() } ngt := gongt.New(indexName).Open() defer ngt.Close() search(ngt) fmt.Printf("search duration:%v \n", time.Now().Sub(start)) }
输出
讯享网generate random vectors create duration:2m55.s search 10 items result[0]:[{
ID:4453 Distance:3.23} {
ID:7138 Distance:3.29688} {
ID:20912 Distance:3.3384} {
ID:55578 Distance:4.0215} {
ID:50904 Distance:4.0113} {
ID:25982 Distance:4.065} {
ID:37933 Distance:4.01855} {
ID:12410 Distance:4.0746} {
ID:5115 Distance:4.06875} {
ID:9653 Distance:4.075}] result[1]:[{
ID:3241 Distance:3.35986} {
ID:47653 Distance:3.7324} {
ID:33205 Distance:3.209} {
ID:40472 Distance:3.6797} {
ID:38679 Distance:3.4258} {
ID:12487 Distance:3.45996} {
ID:17778 Distance:3.7207} {
ID:30227 Distance:3.34277} {
ID:91308 Distance:3.6284} {
ID:9489 Distance:3.69434}] result[2]:[{
ID:10999 Distance:3.11572} {
ID:93639 Distance:3.73096} {
ID:39766 Distance:3.2954} {
ID:4030 Distance:3.0249} {
ID:34369 Distance:3.89307} {
ID:29719 Distance:3.67773} {
ID:39147 Distance:3.38037} {
ID:68061 Distance:3.48145} {
ID:43473 Distance:3.61133} {
ID:16032 Distance:3.5879}] result[3]:[{
ID:69316 Distance:3.86865} {
ID:22104 Distance:3.58936} {
ID:10002 Distance:3.4463} {
ID:17405 Distance:3.67773} {
ID:40514 Distance:3.98096} {
ID:6449 Distance:3.5259} {
ID:23647 Distance:3.895} {
ID:79469 Distance:3.75195} {
ID:21532 Distance:3.29443} {
ID:52289 Distance:3.7417}] result[4]:[{
ID:29526 Distance:3.47803} {
ID:91662 Distance:3.53613} {
ID:10173 Distance:3.8794} {
ID:58227 Distance:3.55127} {
ID:23559 Distance:3.22314} {
ID:95411 Distance:3.50537} {
ID:72048 Distance:3.59473} {
ID:88035 Distance:3.8574} {
ID:41345 Distance:3.2383} {
ID:75584 Distance:3.42334}] result[5]:[{
ID:18617 Distance:3.30273} {
ID:3132 Distance:3.9204} {
ID:22819 Distance:3.1909} {
ID:67798 Distance:3.229} {
ID:87321 Distance:3.98096} {
ID:23139 Distance:3.33057} {
ID:6745 Distance:3.68555} {
ID:7138 Distance:3.08545} {
ID:97341 Distance:3.5991} {
ID:71935 Distance:3.43213}] result[6]:[{
ID:83274 Distance:3.28027} {
ID:99977 Distance:3.41846} {
ID:43642 Distance:3.74854} {
ID:80104 Distance:3.97314} {
ID:20479 Distance:3.9795} {
ID:94469 Distance:3.8003} {
ID:27608 Distance:3.64697} {
ID:407 Distance:3.8877} {
ID:24981 Distance:3.66895} {
ID:12989 Distance:4.0017}] result[7]:[{
ID:56672 Distance:3.91553} {
ID:7893 Distance:3.16357} {
ID:4895 Distance:3.95557} {
ID:30108 Distance:3.00928} {
ID:43823 Distance:3.8716} {
ID:75563 Distance:3.41943} {
ID:51034 Distance:4.0032} {
ID:61472 Distance:4.008} {
ID:11582 Distance:4.0529} {
ID:74277 Distance:4.0898}] result[8]:[{
ID:13815 Distance:3.56885} {
ID:76880 Distance:3.8325} {
ID:30706 Distance:3.92627} {
ID:50794 Distance:4.0046} {
ID:93799 Distance:4.0922} {
ID:35995 Distance:4.0654} {
ID:33510 Distance:4.0461} {
ID:90881 Distance:4.025} {
ID:31972 Distance:4.03965} {
ID:33738 Distance:4.06875}] result[9]:[{
ID:68548 Distance:3.52295} {
ID:1528 Distance:3.77637} {
ID:14922 Distance:3.55273} {
ID:62409 Distance:3.9297} {
ID:71139 Distance:3.7915} {
ID:1429 Distance:3.1416} {
ID:98372 Distance:3.96094} {
ID:59946 Distance:3.42334} {
ID:18247 Distance:3.36133} {
ID:80053 Distance:3.8755}] search duration:98.ms
测试环境是一个2核的虚拟机,时间的绝对值没有太大参考意义。至此验证gongt安装成功
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/83539.html