Giter VIP home page Giter VIP logo

kakao / n2 Goto Github PK

View Code? Open in Web Editor NEW
564.0 39.0 75.0 1.31 MB

TOROS N2 - lightweight approximate Nearest Neighbor library which runs fast even with large datasets

License: Apache License 2.0

Makefile 1.86% Python 9.47% Go 4.76% C++ 35.54% SWIG 0.68% Jupyter Notebook 43.43% Cython 4.25%
python go ml knn machine-learning approximate k-nearest-neighbors nearest-neighbor-search approximate-nearest-neighbor-search

n2's Introduction

N2

Latest Version Documentation Status Build Status Apache-License 2.0

Lightweight approximate Nearest Neighbor algorithm library written in C++ (with Python/Go bindings).

N2 stands for two N's, which comes from 'Approximate Nearest Neighbor Algorithm'.

Why N2 Was Made

Before N2, there has been other great approximate nearest neighbor libraries such as Annoy and NMSLIB. However, each of them had different strengths and weaknesses regarding usability, performance, and etc. So, N2 has been developed aiming to bring the strengths of existing aKNN libraries and supplement their weaknesses.

Features

  • Lightweight library which runs fast with large datasets.
  • Good performance in terms of index build time, search speed, and memory usage.
  • Supports multi-core CPUs for index building.
  • Supports a mmap feature by default to efficiently process large index files.
  • Supports Python/Go bindings.

Supported Distance Metrics

Metric Definition d(p, q)
"angular" 1 - cosθ 1 - {sum(p i · q i) / sqrt(sum(p i · p i) · sum(q i · q i))}
"L2" squared L2 sum{(p i - q i) 2}
"dot" dot product sum(p i · q i)

N2 supports three distance metrics. For "angular" and "L2", d (distance) is defined such that the closer the vectors are, the smaller d is. However for "dot", d is defined such that the closer the vectors are, the larger d is. You may be wondering why we defined and implemented "dot" metric as plain dot product and not as (1 - dot product). The rationale for this decision was to allow users to directly interpret the d value returned from Hnsw search function as a dot product value.

Quickstart

  1. Install N2 with pip.
$ pip install n2
  1. Here is a python code snippet demonstrating how to use N2.
import numpy as np

from n2 import HnswIndex

N, dim = 10240, 20
samples = np.arange(N * dim).reshape(N, dim)

index = HnswIndex(dim)
for sample in samples:
    index.add_data(sample)
index.build(m=5, n_threads=4)
print(index.search_by_id(0, 10))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Full Documentation

Visit n2.readthedocs.io for full documentation. The documentation site explains the following contents in detail.

Performance

  • Here are the results of our benchmark experiments.
  • You can also see benchmarks of various ANN libraries in Python at ann-benchmarks.com. Note that N2 version 0.1.6 is used in ann-benchmarks.com (last checked on October 8th, 2020) and we are continuing our efforts to improve N2 performance.

Index Build Time

image0

Search Speed

image1

Memory Usage

image2

References

License

This software is licensed under the Apache 2 license, quoted below.

Copyright 2017 Kakao Corp. http://www.kakaocorp.com

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.