program synthesis and description with structured machine
play

Program Synthesis and Description with Structured Machine Learning - PowerPoint PPT Presentation

Program Synthesis and Description with Structured Machine Learning Models Graham Neubig @Stanford CS379C 5/1/2018 Coding = Concept Implementation sort list x in descending order x.sort(reverse=True) The (Famous) Stack


  1. 
 
 Program Synthesis and Description with Structured Machine Learning Models Graham Neubig 
 @Stanford CS379C 5/1/2018

  2. Coding = Concept → Implementation sort list x in descending order x.sort(reverse=True)

  3. The (Famous) Stack Overflow Cycle sort my_list in descending order Formulate the Idea python sort list in descending order Search the Web Browse thru. results Modify the sorted(my_list, reverse=True) result

  4. Program Understanding: Implementation → Concept sort list x in descending order x.sort(reverse=True)

  5. Today’s Agenda: 
 Can Natural Language Help? • Describing code with natural language • Synthesizing code from natural language • Bonus! Creating datasets to do so

  6. Natural Language vs. Programming Language

  7. Natural Language vs. Code Natural Language Code Human interpretable Human and machine interpretable Ambiguous Precise in interpretation Structured, but flexible Structured w/o flexibility Note: Good summary in Allamanis et al. (2017)

  8. Structure in Code if x % 5 == 0: AST Parser Can we take If advantage of this for better Compare NL-code interfaces? BinOp Name Num Num x Load % 5 == 0 (used in models of Maddison & Tarlow 2014)

  9. Learning to Generate Pseudo-code from Source Code w/ Machine Translation (ASE 2015) Joint Work w/ Yusuke Oda, Hiroyuki Fudaba, Hideaki Hata, Sakriani Sakti, Tomoki Toda, Satoshi Nakamura.

  10. In Code Description, What do we Describe? Classes 
 class class1: [Moreno+ 2013] def func1( t ): 
 Functions/Methods 
 [Movshovitz-Attias+ 2013], others … def func2( t ): 
 Single lines of code [Oda+ 2015] my_list = range(1,t) Single variables 
 my_val = 0 [Sridhara+ 2011a, Allamanis+ 2015] for x in my_list : Code blocks 
 my_val += x * x [Sridhara+ 2011b, Wong+ 2013] return my_val

  11. Why Generate 
 NL Pseudo-code Descriptions? Assisting Code Reading Debugging Pseudo-code can help Could provide a sanity check explain functionality of code for programmers

  12. Previous Work Rule-based methods e.g. [Buse+ 08, Sridhara+ 10, Sridhara+ 11, Moreno+ 13] Sophisticated and robust , but high-maintenance and language-specific Information retrieval method s e.g. [Haiduc+10, Eddy+13, Wong+13, Rodeghero+14] Data-driven and easy to construct , but lack generalizability and error prone

  13. Our Proposal: Treat Code Description as Translation! Machine Translation もし x を 5 で 割り切れる なら if x is divisible by 5 Code Description if x % 5 == 0 : if x is divisible by 5

  14. A First Attempt: 
 Phrase-based Machine Translation

  15. A Better Attempt: Tree-based Machine Translation

  16. Trees don't Match NL!

  17. Transform 1: Heads

  18. Transform 2: Redundant Nodes

  19. Transform 3: Integrate Nodes

  20. Final Tree

  21. Experiments

  22. Django Dataset • Description: manually annotated descriptions for 18K lines of code • Target code : one liners • Covers a wide range of real-world use cases like I/O operation, string manipulation and exception handling Intent call the function _generator, join the result into a string, return the result Target

  23. How Good are the Generated Descriptions? • Answer: Pretty good!

  24. How Useful are the Descriptions? • Generated pseudo-code improved readability compared to no pseudo-code

  25. Succeeding Work • Lots of work after this on data-driven (neural) models, e.g. • Summarizing Source Code using a Neural Attention Model, Iyer et al. 2016 • A Convolutional Attention Network for Extreme Summarization of Source Code, Allamanis et al. 2016.

  26. A Syntactic Neural Model for Code Synthesis from Natural Language (ACL 2017) Joint Work w/ Pengcheng Yin

  27. Goal: Assistive Interfaces for Programmers Interface by William Qian

  28. Previous Work • Lots of work on rule-based methods for natural language programming (e.g. see Balzer 1985) • Lots of work on semantic parsing w/ grammar- based statistical models (e.g. Wong & Mooney 2007) • One work on using neural sequence-to-sequence models for code generation in Python (Ling et al. 2016)

  29. Sequence-to-sequence Models (Sutskever et al. 2014, Bahadanau et al. 2015) • Neural network models for transducing sequences sort list x backwards </s> RNN RNN RNN RNN RNN sort ( x , ... RNN RNN RNN RNN sort ( x , reverse

  30. Proposed Method: Syntactic Neural Models for Code Synthesis • Key idea: use the grammar of the programming language (Python) as prior knowledge in a neural model sort my_list in descending order Input Intent Expr Generated AST Call expr[func] keyword*[keywords] expr*[args] Name erpr keyword .... s tr(sorted) Name str(my_list) Deterministic transformation (using Python astor library) sorted(my_list, reverse=True) Surface Code NOTE: very nice contemporaneous work by Rabinovich et al. (2017)

  31. Generation Process • Factorize the AST into actions: • ApplyRule : generate an internal node in the AST • GenToken : generate (part of) a token

  32. Formulation as a Neural Model • Encoder: summarize the semantics of the NL intent • Decoder: • Hidden state keeps track of the generation process of the AST • Based on the current state, predict an action to grow the AST Action Sequence LSTM Decoder NL Intent Action Flow LSTM Encoder Parent Feeding (Dong and Lapata, 2016)

  33. Computing Action Probabilities • ApplyRule[r]: apply a production rule r to the current derivation • GenToken[v]: append a token v to the current terminal node • Deal with OOV: learning to generate a token or directly copy it from the input Generation prob. Final probability: Vocabulary marginalize over Softmax Generation the two paths ... Softmax Input Words Pointer Copy from Input Net ... Expr Call Copy prob. c] keyword*[keywords] expr*[args] sort my_list in descending order erpr keyword .... ) Name str(my_list) Derivation

  34. Experiments • Natural Language ⟼ Python code: • HearthStone (Ling et al., 2016): card game implementation • Django (Oda et al., 2015): web framework • Natural Language ⟼ Domain Specific Language (Semantic Parsing) • IFTTT (Quirk et al., 2015): personal task automation APP

  35. HearthStone Dataset • Description: properties/fields of an HS card • Target code: implementation as a Python class from HearthBreaker Intent (Card Property) <name> Divine Favor </name> <cost> 3 </cost> <desc> Draw cards until you have as many in hand as your Target (Python class, extracted from HearthBreaker) [Ling et al. , 2016]

  36. IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety of topics: home automation, productivity, etc . • Domain-Specific Language (DSL): IF-THIS-THEN- THAT structure, much simpler grammar Intent Autosave your Instagram photos to Dropbox Target IF Instagram.AnyNewPhotoByYou THEN Dropbox.AddFileFromURL https://ifttt.com/applets/1p-autosave- your-instagram-photos-to-dropbox [Quirk et al. , 2015]

  37. Results • Baseline systems ( do not model syntax a priori ): –Latent Predictor Network [Ling et al. , 2016] –Seq2Tree [Dong and Lapata., 2016] –Doubly recurrent RNN [Alvarez-Melis and Jaakkola . , 2017] • Take Home Msg: –Modeling syntax helps for code generation and semantic parsing ☺

  38. Examples Intent join app_config.path and string 'locale' into a file path, substitute it for localedir. Pred. Intent self.plural is an lambda function with an argument n, which returns result of boolean expression n not equal to integer 1 Pred. Ref. Intent <name> Burly Rockjaw Trogg </name> <cost> 5 </cost> <attack> 3 </attack> <defense> 5 </defense> <desc> Whenever your opponent casts a spell, gain 2 Attack. </desc> <rarity> Common </rarity> ... Ref. tokens copied from input

  39. Learning to Mine NL/Code Pairs from Stack Overflow (In Progress) Joint Work w/ 
 Pengcheng Yin, Bowen Deng, Edgar Chen, Bogdan Vasilescu

  40. Datasets are Important! • Our previous work used Django, HearthStone, IFTTT, manually curated datasets • It couldn't have been done without these • But these are extremely specific, and small

  41. StackOverflow is Promising! • StackOverflow promises a large data source for code synthesis • But code snippets don’t necessarily reflect the answer to the original question

  42. Mining Method

  43. Annotation • ~100 posts for Python/Java

  44. Features (1): Structural Features • "does this look like a valid snippet?" –Position: Is the snippet a full block? The start/end of a block? The only block in an answer? –Code Features: Contains import? Starts w/ assignment? Is value? –Answer Quality: Answer is accepted? Answer is rank 1, 2, 3? –Length: What is the number of lines?

  45. Features (2): Correspondence Features • "do the intent and snippet look like they match?" –Train an RNN to predict P(intent | snippet) and P(snippet | intent) given heuristically extracted noisy data –Use log probabilities and normalized by z score over post, etc.

  46. Main Results • On both Python and Java, better results than heuristic strategies • Both structural and correspondence features were necessary

Recommend


More recommend