hnswlib RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is to sma
生活随笔
收集整理的這篇文章主要介紹了
hnswlib RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is to sma
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 問題現象
index = hnswlib.Index(space = '100', dim = 512)
index.init_index(max_elements = 100, ef_construction = 200, M = 16)# Insert this single entry
index.add_items(dd, np.asarray([10000]))index.set_ef(200)
labels, distances = index.knn_query(dd, k = 3)
在使用 hnswlib 庫進行搜索匹配時有以下錯誤
RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is to small
2. 問題原因
搜索算法無法找到 k=3 個鄰居,因為庫中只有一個元素,所以這會觸發異常。我們在下面的代碼中查詢配置時使用了 k = 3 的參數。
labels, distances = index.knn_query(dd, k = 3)
3. 解決方法
- 捕獲該異常,并將庫中的
cur_idx返回,即如果要查詢前 3 個元素,那么庫中只有一個元素時,捕獲該異常,并返回這 1 個元素。 - 往庫中添加多余 3 個的特征,這樣就可以正常返回了。
總結
以上是生活随笔為你收集整理的hnswlib RuntimeError: Cannot return the results in a contigious 2D array. Probably ef or M is to sma的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 寻找尼古拉凯奇主演电影名
- 下一篇: 《题山石榴花》是谁的作品?