ScaNN (Scalable Nearest Neighbors) is an efficient approximate nearest neighbor search library that significantly outperforms brute-force exhaustive search for retrieval tasks in recommendation systems; by replacing the brute-force factorized top-k layer with ScaNN's optimized algorithms (including tree-based space partitioning, asymmetric hashing, and quantization), practitioners can achieve approximately 8x faster query processing times while maintaining comparable accuracy, with tunable parameters like num_leaves and num_leaves_to_search allowing control over the speed-accuracy trade-off.
Efficient Serving with ScaNN for Approximate Nearest Neighbor Search
Added:hi there welcome back to the video series of building recommendation systems with tensorflow my name is wei and i'm a developer advocate at google last time we learned how to use dcn deepen across network to improve your model performance so far we have been focusing on building recommendation models and training them but what about deployment which is another important step in machine learning engineering practice in this video we are going to cover efficient serving with scan for retrieval you may recall that in our first video when we introduced several google open source products related to recommendation systems we briefly touched upon scan scanning short for scalable nearest neighbors for modern recommendation systems at the retrieval stage we need to quickly find the nearest data sets embeddings for a given query embedding usually the set of embedding is often too large for exhaustive search so you would need a tool like scan to do approximate neighborhood search scan was open sourced in june 2020 and it provides highly efficient vector similarity search namely faster matching and retrieval of similar items from massive databases it includes state-of-the-art implementations of tree based space partitioning asymmetrical hashing quantization inverted indexes and etc due to these highly optimized algorithms scan offers significant speed ups in nearest neighbor search for massive and moderate size databases which makes it perfect for the retrieval stage of recommenders scanning achieves state-of-the-art performance on amn benchmarks.com has shown on the glove 100 angular dataset as you can see here this is a chart that shows the trade-off between speed and accuracy on the x-axis we have the recall at 10 metric as accuracy on the y-axis we have query per second at speed for nearest neighbor search high accuracy comes with more expensive search which means your speed will be slower in this chart the purple line with the little triangles on it at the top is scanned so you can see it is outperforming other alternatives quite a bit before we show you how to use scan let's first see how to do exhaustive neighborhood search using tensorflow recommenders remember we are doing a retrieval task here so you will use a brute force factorized top k layer from tensorflow recommenders to index the movie embeddings for context factorized top k layer is responsible for retrieving top k recommendations from retrieval models then you can use the brute force layer to do exhaustive search and retrieve the neighbors of a given query here we are recommending homeworld bound kit in king arthur's chords and rudy to user 42 if we time it we can see on average it takes about 25 milliseconds which is not bad but let's see if we can make it even faster let's use scan this time actually it's very easy to replace the exhaustive search with scan you only need to replace the brute force layer with the scan layer the rest is pretty much the same as before if we time it now it takes only three milliseconds which is almost eight times faster than exhaustive search and this is on the movie lens data sets repeated a certain times which is relatively small by the standards of today's large recommendation systems on large data sets the speed up will be even bigger now that you have taken a peek at the power of scan let's see how you can deploy it we first built the index as we did just now then we serialize the model with a simple save method later we can load the model back as a normal keras model and get retrieval results as before you can also use a customized version of tensorflow serving to deploy the model the custom version of tf serving is available as a docker container image on docker hub scan also comes with a few knobs that you can tune to suit your own needs the most important ones are num leaves and num leaves to search by default they are set to 110 this means our database is partitioned into 100 disjoint subsets and the 10 most promising of these partitions is scored 10 divided by 100 which is 10 of the data set is being searched so as our baseline we get 33.1 recall and 3.35 milliseconds latency if we set num leaves to a thousand and now leaves to search to 100 we would also be searching 10 of the database however in comparison to the previous setting the ten percent we will search will contain higher quality candidates because a high number leaves allows us to make finer grand decisions about what parts of the database sets are worth searching in this case we get a higher recoil 96.6 percent with a slightly higher latency 3.39 milliseconds if we reduce num leaves to search to 70 we sacrifice our record little bits to 95.7 percent but our latency is reduced to 3.22 milliseconds in general tuning scan is about picking the right trade-offs each individual parameter change generally won't make search both faster and more accurate so our goal is to tune the parameters to optimally trade off between these two conflicting goals so in this episode we learned about how to leverage scan to do efficient serving for our retrieval models scan holds clear advantages over exhaustive search in terms of latency and you can further tune scan parameters to strike the right balance of accuracy and speed we're continuously adding more features to tensorflow recommenders and we'll be creating more video tutorials as well thank you for watching this video series and please stay tuned for future updates thank you [Music] you
Up Next

Visual Localization Methods: From Image Retrieval to Pose Regression
@naamiinepal
229 views•2026-01-10

Building Real-Time ML Pipelines with Feature Stores and MLOps Frameworks
@ODSCAI
5.1K views•2022-02-20

Recommendation Systems Overview with TensorFlow: Key Concepts
@TensorFlow
142.1K views•2021-06-29

Neural Networks Explained: Math, Layers, and Learning Fundamentals
@3blue1brown
21.9M views•2017-10-05
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Artificial Intelligence




































