Broadcast Annoy object in Spark (for nearest neighbors)?

前端 未结 2 656
庸人自扰
庸人自扰 2021-01-03 05:05

As Spark\'s mllib doesn\'t have nearest-neighbors functionality, I\'m trying to use Annoy for approximate Nearest Neighbors. I try to broadcast the Annoy object and pass it

2条回答
  •  隐瞒了意图╮
    2021-01-03 06:11

    I've never used Annoy but I am pretty sure that the package description explains what is going on here:

    It also creates large read-only file-based data structures that are mmapped into memory so that many processes may share the same data.

    Since it is using memory mapped indexes when you serialize it and pass it to the workers all data is lost on the way.

    Try something like this instead:

    from pyspark import SparkFiles
    
    t.save("index.ann")
    sc.addPyFile("index.ann")
    
    def find_neighbors(iter):
        t = AnnoyIndex(f)
        t.load(SparkFiles.get("index.ann"))
        return (t.get_nns_by_vector(vector=x[1], n=5) for x in iter)
    
    sparkvectors.mapPartitions(find_neighbors).first()
    ## [0, 13, 12, 6, 4]
    

提交回复
热议问题