速度數(shù)百倍之差,有人斷言KNN面臨淘汰,更快更強(qiáng)的ANN將取而代之
在模式識別領(lǐng)域中,K - 近鄰算法(K-Nearest Neighbor, KNN)是一種用于分類和回歸的非參數(shù)統(tǒng)計方法。K - 近鄰算法非常簡單而有效,它的模型表示就是整個訓(xùn)練數(shù)據(jù)集。就原理而言,對新數(shù)據(jù)點(diǎn)的預(yù)測結(jié)果是通過在整個訓(xùn)練集上搜索與該數(shù)據(jù)點(diǎn)最相似的 K 個實(shí)例(近鄰)并且總結(jié)這 K 個實(shí)例的輸出變量而得出的。KNN 可能需要大量的內(nèi)存或空間來存儲所有數(shù)據(jù),并且使用距離或接近程度的度量方法可能會在維度非常高的情況下(有許多輸入變量)崩潰,這可能會對算法在你的問題上的性能產(chǎn)生負(fù)面影響。這就是所謂的維數(shù)災(zāi)難。
近似最近鄰算法(Approximate Nearest Neighbor, ANN)則是一種通過犧牲精度來換取時間和空間的方式從大量樣本中獲取最近鄰的方法,并以其存儲空間少、查找效率高等優(yōu)點(diǎn)引起了人們的廣泛關(guān)注。
近日,一家技術(shù)公司的數(shù)據(jù)科學(xué)主管 Marie Stephen Leo 撰文對 KNN 與 ANN 進(jìn)行了比較,結(jié)果表明, 在搜索到最近鄰的相似度為 99.3% 的情況下,ANN 比 sklearn 上的 KNN 快了 380 倍 。
作者表示,幾乎每門數(shù)據(jù)科學(xué)課程中都會講授 KNN 算法,但它正在走向「淘汰」!
KNN 簡述
在機(jī)器學(xué)習(xí)社區(qū)中,找到給定項(xiàng)的「K」個相似項(xiàng)被稱為相似性搜索或最近鄰(NN)搜索。最廣為人知的 NN 搜索算法是 KNN 算法。在 KNN 中,給定諸如手機(jī)電商目錄之類的對象集合,則對于任何新的搜索查詢,我們都可以從整個目錄中找到少量(K 個)最近鄰。例如,在下面示例中,如果設(shè)置 K = 3,則每個「iPhone」的 3 個最近鄰是另一個「iPhone」。同樣,每個「Samsung」的 3 個最近鄰也都是「Samsung」。
KNN 存在的問題
盡管 KNN 擅長查找相似項(xiàng),但它使用詳細(xì)的成對距離計算來查找鄰居。如果你的數(shù)據(jù)包含 1000 個項(xiàng),如若找出新產(chǎn)品的 K=3 最近鄰,則算法需要對數(shù)據(jù)庫中所有其他產(chǎn)品執(zhí)行 1000 次新產(chǎn)品距離計算。這還不算太糟糕,但是想象一下,現(xiàn)實(shí)世界中的客戶對客戶(Customer-to-Customer,C2C)市場,其中的數(shù)據(jù)庫包含數(shù)百萬種產(chǎn)品,每天可能會上傳數(shù)千種新產(chǎn)品。將每個新產(chǎn)品與全部數(shù)百萬種產(chǎn)品進(jìn)行比較是不劃算的,而且耗時良久,也就是說這種方法根本無法擴(kuò)展。
解決方案
將最近鄰算法擴(kuò)展至大規(guī)模數(shù)據(jù)的方法是 徹底避開暴力距離計算,使用 ANN 算法。
近似最近距離算法(ANN)
嚴(yán)格地講,ANN 是一種在 NN 搜索過程中允許少量誤差的算法。但在實(shí)際的 C2C 市場中,真實(shí)的鄰居數(shù)量比被搜索的 K 近鄰數(shù)量要多。與暴力 KNN 相比,人工神經(jīng)網(wǎng)絡(luò)可以在短時間內(nèi)獲得卓越的準(zhǔn)確性。ANN 算法有以下幾種:
-
Spotify 的 ANNOY
-
Google 的 ScaNN
-
Facebook 的 Faiss
-
HNSW
分層的可導(dǎo)航小世界(Hierarchical Navigable Small World, HNSW)
在 HNSW 中,作者描述了一種使用多層圖的 ANN 算法。在插入元素階段,通過指數(shù)衰減概率分布隨機(jī)選擇每個元素的最大層,逐步構(gòu)建 HNSW 圖。這確保 layer=0 時有很多元素能夠?qū)崿F(xiàn)精細(xì)搜索,而 layer=2 時支持粗放搜索的元素數(shù)量少了 e^-2。最近鄰搜索從最上層開始進(jìn)行粗略搜索,然后逐步向下處理,直至最底層。使用貪心圖路徑算法遍歷圖,并找到所需鄰居數(shù)量。
HNSW 圖結(jié)構(gòu)。最近鄰搜索從最頂層開始(粗放搜索),在最底層結(jié)束(精細(xì)搜索)。
HNSW Python 包
整個 HNSW 算法代碼已經(jīng)用帶有 Python 綁定的 C++ 實(shí)現(xiàn)了,用戶可以通過鍵入以下命令將其安裝在機(jī)器上:pip install hnswlib。安裝并導(dǎo)入軟件包之后,創(chuàng)建 HNSW 圖需要執(zhí)行一些步驟,這些步驟已經(jīng)被封裝到了以下函數(shù)中:
- <code>import hnswlib</code><code>import numpy as npdef fit_hnsw_index(features, ef=100, M=16, save_index_file=False):</code><code> # Convenience function to create HNSW graph</code><code> # features : list of lists containing the embeddings</code><code> # ef, M: parameters to tune the HNSW algorithm</code><code> </code><code> num_elements = len(features)</code><code> labels_index = np.arange(num_elements) EMBEDDING_SIZE = len(features[0]) # Declaring index</code><code> # possible space options are l2, cosine or ip</code><code> p = hnswlib.Index(space='l2', dim=EMBEDDING_SIZE) # Initing index - the maximum number of elements should be known</code><code> p.init_index(max_elements=num_elements, ef_construction=ef, M=M) # Element insertion</code><code> int_labels = p.add_items(features, labels_index) # Controlling the recall by setting ef</code><code> # ef should always be > k</code><code> p.set_ef(ef) </code><code> </code><code> # If you want to save the graph to a file</code><code> if save_index_file:</code><code> p.save_index(save_index_file)</code><code> </code><code> return p</code>
創(chuàng)建 HNSW 索引后,查詢「K」個最近鄰就僅需以下這一行代碼:
- ann_neighbor_indices, ann_distances = p.knn_query(features, k)
KNN 和 ANN 基準(zhǔn)實(shí)驗(yàn)
計劃
首先下載一個 500K + 行的大型數(shù)據(jù)集。然后將使用預(yù)訓(xùn)練 fasttext 句子向量將文本列轉(zhuǎn)換為 300d 嵌入向量。然后將在不同長度的輸入數(shù)據(jù) [1000. 10000, 100000, len(data)] 上訓(xùn)練 KNN 和 HNSW ANN 模型,以度量數(shù)據(jù)大小對速度的影響。最后將查詢兩個模型中的 K=10 和 K=100 時的最近鄰,以度量「K」對速度的影響。首先導(dǎo)入必要的包和模型。這需要一些時間,因?yàn)樾枰獜木W(wǎng)絡(luò)上下載 fasttext 模型。
- <code># Imports</code><code># For input data pre-processing</code><code>import json</code><code>import gzip</code><code>import pandas as pd</code><code>import numpy as np</code><code>import matplotlib.pyplot as plt</code><code>import fasttext.util</code><code>fasttext.util.download_model('en', if_exists='ignore') # English pre-trained model</code><code>ft = fasttext.load_model('cc.en.300.bin')# For KNN vs ANN benchmarking</code><code>from datetime import datetime</code><code>from tqdm import tqdm</code><code>from sklearn.neighbors import NearestNeighbors</code><code>import hnswlib</code>
數(shù)據(jù)
使用亞[馬遜產(chǎn)品數(shù)據(jù)集],其中包含「手機(jī)及配件」類別中的 527000 種產(chǎn)品。然后運(yùn)行以下代碼將其轉(zhuǎn)換為數(shù)據(jù)框架。記住僅需要產(chǎn)品 title 列,因?yàn)閷⑹褂盟鼇硭阉飨嗨频漠a(chǎn)品。
- <code># Data: http://deepyeti.ucsd.edu/jianmo/amazon/</code><code>data = []</code><code>with gzip.open('meta_Cell_Phones_and_Accessories.json.gz') as f:</code><code> for l in f:</code><code> data.append(json.loads(l.strip()))# Pre-Processing: https://colab.research.google.com/drive/1Zv6MARGQcrBbLHyjPVVMZVnRWsRnVMpV#scrollTo=LgWrDtZ94w89</code><code># Convert list into pandas dataframe</code><code>df = pd.DataFrame.from_dict(data)</code><code>df.fillna('', inplace=True)# Filter unformatted rows</code><code>df = df[~df.title.str.contains('getTime')]# Restrict to just 'Cell Phones and Accessories'</code><code>df = df[df['main_cat']=='Cell Phones & Accessories']# Reset index</code><code>df.reset_index(inplace=True, drop=True)# Only keep the title columns</code><code>df = df[['title']]# Check the df</code><code>print(df.shape)</code><code>df.head()</code>
如果全部都可以運(yùn)行精細(xì)搜索,你將看到如下輸出:
亞馬遜產(chǎn)品數(shù)據(jù)集。
嵌入
要對文本數(shù)據(jù)進(jìn)行相似性搜索,則必須首先將其轉(zhuǎn)換為數(shù)字向量。一種快速便捷的方法是使用經(jīng)過預(yù)訓(xùn)練的網(wǎng)絡(luò)嵌入層,例如 Facebook [FastText] 提供的嵌入層。由于希望所有行都具有相同的長度向量,而與 title 中的單詞數(shù)目無關(guān),所以將在 df 中的 title 列調(diào)用 get_sentence_vector 方法。
嵌入完成后,將 emb 列作為一個 list 輸入到 NN 算法中。理想情況下可以在此步驟之前進(jìn)行一些文本清理預(yù)處理。同樣,使用微調(diào)的嵌入模型也是一個好主意。
- <code># Title Embedding using FastText Sentence Embedding</code><code>df['emb'] = df['title'].apply(ft.get_sentence_vector)# Extract out the embeddings column as a list of lists for input to our NN algos</code><code>X = [item.tolist() for item in df['emb'].values]</code>
基準(zhǔn)
有了算法的輸入,下一步進(jìn)行基準(zhǔn)測試。具體而言,在搜索空間中的產(chǎn)品數(shù)量和正在搜索的 K 個最近鄰之間進(jìn)行循環(huán)測試。在每次迭代中,除了記錄每種算法的耗時以外,還要檢查 pct_overlap,因?yàn)橐欢ū壤?KNN 最近鄰也被挑選為 ANN 最近鄰。
注意整個測試在一臺全天候運(yùn)行的 8 核、30GB RAM 機(jī)器上運(yùn)行大約 6 天,這有些耗時。理想情況下,你可以通過多進(jìn)程來加快運(yùn)行速度,因?yàn)槊看芜\(yùn)行都相互獨(dú)立。
- <code># Number of products for benchmark loop</code><code>n_products = [1000, 10000, 100000, len(X)]# Number of neighbors for benchmark loop</code><code>n_neighbors = [10, 100]# Dictionary to save metric results for each iteration</code><code>metrics = {'products':[], 'k':[], 'knn_time':[], 'ann_time':[], 'pct_overlap':[]}for products in tqdm(n_products):</code><code> # "products" number of products included in the search space</code><code> features = X[:products]</code><code> </code><code> for k in tqdm(n_neighbors): </code><code> # "K" Nearest Neighbor search</code><code> # KNN </code><code> knn_start = datetime.now()</code><code> nbrs = NearestNeighbors(n_neighbors=k, metric='euclidean').fit(features)</code><code> knn_distances, knn_neighbor_indices = nbrs.kneighbors(X)</code><code> knn_end = datetime.now()</code><code> metrics['knn_time'].append((knn_end - knn_start).total_seconds())</code><code> </code><code> # HNSW ANN</code><code> ann_start = datetime.now()</code><code> p = fit_hnsw_index(features, ef=k*10)</code><code> ann_neighbor_indices, ann_distances = p.knn_query(features, k)</code><code> ann_end = datetime.now()</code><code> metrics['ann_time'].append((ann_end - ann_start).total_seconds())</code><code> </code><code> # Average Percent Overlap in Nearest Neighbors across all "products"</code><code> metrics['pct_overlap'].append(np.mean([len(np.intersect1d(knn_neighbor_indices[i], ann_neighbor_indices[i]))/k for i in range(len(features))]))</code><code> </code><code> metrics['products'].append(products)</code><code> metrics['k'].append(k)</code><code> </code><code>metrics_df = pd.DataFrame(metrics)</code><code>metrics_df.to_csv('metrics_df.csv', index=False)</code><code>metrics_df</code>
運(yùn)行結(jié)束時輸出如下所示。從表中已經(jīng)能夠看出,HNSW ANN 完全超越了 KNN。
以表格形式呈現(xiàn)的結(jié)果。
結(jié)果
以圖表的形式查看基準(zhǔn)測試的結(jié)果,以真正了解二者之間的差異,其中使用標(biāo)準(zhǔn)的 matplotlib 代碼來繪制這些圖表。這種差距是驚人的。根據(jù)查詢K=10 和 K=100 最近鄰所需的時間,HNSW ANN 將 KNN 徹底淘汰。當(dāng)搜索空間包含約 50 萬個產(chǎn)品時,在 ANN 上搜索 100 個最近鄰的速度是 KNN 的 380 倍,同時兩者搜索到最近鄰的相似度為 99.3%。
在搜索空間包含 500K 個元素,搜索空間中每個元素找到 K=100 最近鄰時,HNSW ANN 的速度比 Sklearn 的 KNN 快 380 倍。
在搜索空間包含 500K 個元素,搜索空間中每個元素找到 K=100 最近鄰時,HNSW ANN 和 KNN 搜索到最近鄰的相似度為 99.3%。
基于以上結(jié)果,作者認(rèn)為可以大膽地說:「KNN 已死」。
本篇文章的代碼作者已在 GitHub 上給出:https://github.com/stephenleo/adventures-with-ann/blob/main/knn_is_dead.ipynb