Searching for Code? Let a Neural Network Do That for You!

Synced
Synced
Jul 1 · 4 min read

To avoid “reinventing the wheel,” programmers typically search popular discussion forums such as Stack Overflow to find examples that can guide them on particular coding tasks. This approach works most of the time, and especially when the coding task is a popular one. However, when it comes to a niche task or uncommon programming languages or APIs, a simple keyword search on Google or Stack Overflow may not provide the answers programmers are looking for.

Last week, Facebook published a blog article demonstrating their newly developed code search tool, Neural Code Search (NCS). Powered by neural networks, NCS takes natural language queries and returns relevant code snippets retrieved directly from a large codebase. Facebook introduced the NCS model along with a supervised extension, UNIF.

As demonstrated in the graph below, the NCS model uses embeddings to map code snippets and natural language queries as vectors in the same vector space, and calculates the cosine similarities between embedded code snippets and the given query to locate and deliver the most relevant code as the output.

A newly created dataset based on public queries on Stack Overflow was used to evaluate NCS performance, which was comparable to the traditional IR (Information Retrieval) benchmark method BM25.

As an unsupervised model, NCS can be trained quickly and easily to learn embeddings directly from the search corpus. NCS however struggles in situations where there is no overlap of words between the queries and the source code. To solve that problem researchers added the Embedding Unification model (UNIF).

There are two main differences between NCS and UNIF processes:

  1. Instead of using a single token embedding matrix (T) for both code and query input, UNIF produces two embedding matrices for code (Tc) and query tokens (Tq) respectively.
  2. Instead of using TF-IDF weighting for the code token embeddings, UNIF adopts a learned attention-based weighting scheme.

UNIF’s advantage is that improvements can be observed when there is good labelled data available for training. As the comparison demonstrates, UNIF significantly outperforms NCS in “number of questions answered.”

As the number of publicly available code repositories continues to grow we can expect machine learning to play an increasing role in improving the coding experience. Facebook previously released its code recommendation tool Aroma and Auto-Debug tool Getafix, and we can expect these and NCS and UNIF to be integrated into popular IDEs in the near future.

More details on the neural code search models can be found in the papers Retrieval on Source Code: A Neural Code Search and When Deep Learning Met Code Search, and on the Facebook Artificial Intelligence blog.


Author: Mos Zhang | Editor: Michael Sarazen


2018 Fortune Global 500 Public Company AI Adaptivity Report is out!
Purchase a Kindle-formatted report on Amazon.
Apply for Insight Partner Program to get a complimentary full PDF report.


Follow us on Twitter @Synced_Global for daily AI news!


We know you don’t want to miss any stories. Subscribe to our popular Synced Global AI Weekly to get weekly AI updates.

SyncedReview

We produce professional, authoritative, and thought-provoking content relating to artificial intelligence, machine intelligence, emerging technologies and industrial insights.

Synced

Written by

Synced

AI Technology & Industry Review — syncedreview.com | Newsletter: goo.gl/Q4cP3B | Become Synced Insight Partner: goo.gl/ucXZDw | Twitter: @Synced_Global

SyncedReview

We produce professional, authoritative, and thought-provoking content relating to artificial intelligence, machine intelligence, emerging technologies and industrial insights.