Deep Code Search Xiaodong Gu Hong Kong University








![Cosine Similarity Fusion MLP RNN RNN read a text file method name [M] Cosine Similarity Fusion MLP RNN RNN read a text file method name [M]](https://slidetodoc.com/presentation_image_h/a8453bf9d3adc9a4b9cbdb7fb1ef0dbb/image-9.jpg)












- Slides: 21
Deep Code Search Xiaodong Gu Hong Kong University of Science and Technology Hongyu Zhang The University of Newcastle Sunghun Kim Hong Kong University of Science and Technology 1
Programming is hard • Lack of experience • Unfamiliar libraries 2
Why Not Search for It? Not designed for source code 3
Code Search Engines Keyword Matching! Hard to represent complicated tasks 4
Information Retrieval – Related Work • Consider source code as plain text and apply IR techniques (e. g. , Lucene) • Augment IR approaches by considering properties of source code and NL queries • Typical Techniques [Linstead DMKD’ 09]: augments Lucene by considering method names and code popularity p Portforlio [Mc. Millan, ICSE’ 11]: considers relationships between functions p [Lu et al. SANER’ 15]: query expansion with Word. Net p Code. How [Lv et al. ASE’ 15]: API matching p Sourcerer 5
A fundamental problem of IR based code search Source code and natural language have heterogeneous representations Query: “how to read an object from an xml” public static <S> S deserialize(Class c, File xml) { try { JAXBContext context = JAXBContext. new. Instance(c); Unmarshaller unmarshaller = context. create. Unmarshaller(); S deserialized = (S) unmarshaller. unmarshal(xml); return deserialized; } catch (JAXBException ex) { log. error("Error-deserializing-object-from-XML", ex); return null; } } Mismatch between the high-level intent reflected in the queries and the low-level implementation details in the source code 6
Proposed Approach “read an object from an xml file” Code Embedding “read a text file line by line” Query/Description Embedding Joint Embedding of both Code and Natural Language into a unified vector representation public void read. Text(String text. File) { Buffered. Reader br = new Buffered. Reader( new File. Input. Stream(help. File)); String line = null; while ((line = br. read. Line()) != null) { System. out. println(line); } • • • br. close(); } public static < S > S deserialize(Class c, File xml) { try { JAXBContext context = JAXBContext. new. Instance(c); Unmarshaller unmarshaller =context. create. Unmarshaller(); S deserialized = (S) unmarshaller. unmarshal(xml); return deserialized; } catch (JAXBException ex) { log. error("Error-deserializing-object-from-XML", ex); return null; } } 7
CODEnn (Code-Description Embedding Neural Network) Description Vector Code Vector Cosine Similarity • Code Embedding Network (Co. NN) • Description Embedding Network (De. NN) Code Embedding • Similarity Module Network (Co. NN) Code Description Embedding Network(De. NN) Description 8
Cosine Similarity Fusion MLP RNN RNN read a text file method name [M] API sequence [A] Code Training with Ranking Loss: RNN close RNN buff RNN MLP max pooling reader Scanner. new Scanner. next Scanner. close str RNN max pooling text max pooling Tokens [Γ] [D] Description
Deep. CS – Deep Learning based Code Search Recommended Code Query Offline Training Instances Training aspect extraction code snippets (Java methods) Commented Code Snippets CODEnn Model embedding Query Vector em natural language descriptions aspect extraction 0101010 be Similarity Lookup dd ing code snippets (Java methods) Search Codebase Code Vectors Offline Embedding 10
Step 1 – Prepare a Training Corpus • Training Instances § <C, D+, D-> (<method name, api seq, tokens, correct desc, incorrect desc>) Method API Sequence Tokens Description Name 1 file reader Input. Stream. read→Output. Stream. write input, output, stream, write 2 open URL. new→URL. open. Connection url, open, conn 3 test exists File. new→File. exists file, create, exists (correct) copy a file open a url test file exists (incorrect) open an url test file exists copy a file • Collect Java projects from Git. Hub • Parse source files into ASTs using Eclipse JDT • Extract an API sequence, method name, tokens and a description for each method body (when Javadoc comment exists) 11
/** * read a text file line by line. * @param: path */ public void read. Content(String path){ … Buffered. Reader reader = new Buffered. Reader(…); while((line=reader. read. Line())!=null) … reader. close(); } Description: read a text file line by line. Method name: read context Tokens: {read, context, string, buffer, reader, line, close} Method. Definition Javadoc Comment Body Statement Variable Declaration Type Constructor Invocation Variable Buffered. Reader reader … While Statement Method Invocation Variable reader Block Statement API Sequence: Buffered. Reader. new Buffered. Reader. read. Line Buffered. Reader. close read. Line 12
Step 2 – Training CODEnn Model • Neural Network § Bi-LSTM, 200 hidden units § MLP: 100 hidden units for embedding and 400 for fusing § Word Embedding: 100 • Training Algorithm § Adadelta § Batch size: 128 § Vocabulary size: 10, 000 13
Step 3 – Searching Code Snippets Code Embedding Query Embedding All Java Code 14
Evaluation • Search Codebase § Java repositories from Git. Hub • Query Subjects § Top 50 Java-tagged Questions from Stack Overflow • Baselines § Code. How [Lv et al. ASE’ 15]: combines multiple code aspects such as method name and APIs using an extended boolean model § Lucene: a conventional search engine behind many existing code search tools such as Sourcerer [Linstead et al. DMKD’ 09] 15
Evaluation • Metrics FRank – the rank of the first hit result in the result list 16
Results 17
Results 18
Example – Associative Search Query: “read an object from an xml” public static <S> S deserialize(Class c, File xml) { try { JAXBContext context = JAXBContext. new. Instance(c); Unmarshaller unmarshaller = context. create. Unmarshaller(); S deserialized = (S) unmarshaller. unmarshal(xml); return deserialized; } catch (JAXBException ex) { log. error("Error-deserializing-object-from-XML", ex); return null; } } 19
Example – Query Understanding queue Query: “queue an event to be run on a thread” Query: “run an event on a thread queue” public boolean enqueue(Event. Handler handler, Event event) { synchronized(monitor) { • • • handlers[tail] = handler; events[tail] = event; tail++; if (handlers. length <= tail) tail = 0; monitor. notify(); } return true; } public void run() { while (!stop) { Dynamic. Model. Event evt; while ((evt = event. Queue. poll())!= null) { for (Dynamic. Model. Listener l: listeners. to. Array( new Dynamic. Model. Listener[0])) l. dynamic. Model. Changed(evt); } • • • } } 20
Conclusion Deep. CS –Deep Learning based Code Search – – Learns the representation of source code and NL with deep neural networks Jointly embeds source code and natural language into a unified vector space Future Work – Code embedding with more aspects (e. g. , structures) https: //github. com/guxd/deep-code-search 21





















