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

program synthesis and description with structured machine
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Program Synthesis and Description with Structured Machine Learning Models

Graham Neubig
 
 
 @Stanford CS379C 5/1/2018

slide-2
SLIDE 2

Coding = Concept → Implementation

sort list x in descending

  • rder

x.sort(reverse=True)

slide-3
SLIDE 3

The (Famous) Stack Overflow Cycle

Formulate the Idea

sort my_list in descending order

Search the Web

python sort list in descending order

Browse thru. results Modify the result

sorted(my_list, reverse=True)

slide-4
SLIDE 4

Program Understanding: Implementation → Concept

x.sort(reverse=True)

sort list x in descending

  • rder
slide-5
SLIDE 5

Today’s Agenda:
 Can Natural Language Help?

  • Describing code with natural language
  • Synthesizing code from natural language
  • Bonus! Creating datasets to do so
slide-6
SLIDE 6

Natural Language vs. Programming Language

slide-7
SLIDE 7

Natural Language vs. Code

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

slide-8
SLIDE 8

Structure in Code

x Load % 5 ==

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

slide-9
SLIDE 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.

slide-10
SLIDE 10

In Code Description, What do we Describe?

def func2(t):
 my_list = range(1,t) my_val = 0 for x in my_list: my_val += x * x return my_val def func1(t):
 … class class1:

Single lines of code [Oda+ 2015] Single variables
 [Sridhara+ 2011a, Allamanis+ 2015] Code blocks
 [Sridhara+ 2011b, Wong+ 2013] Functions/Methods
 [Movshovitz-Attias+ 2013], others Classes
 [Moreno+ 2013]

slide-11
SLIDE 11

Why Generate
 NL Pseudo-code Descriptions?

Assisting Code Reading Pseudo-code can help explain functionality of code Debugging Could provide a sanity check for programmers

slide-12
SLIDE 12

Previous Work

Sophisticated and robust, but high-maintenance and language-specific

Data-driven and easy to construct, but lack generalizability and error prone

Rule-based methods e.g. [Buse+ 08, Sridhara+ 10, Sridhara+ 11, Moreno+ 13]

Information retrieval methods e.g. [Haiduc+10, Eddy+13, Wong+13, Rodeghero+14]

slide-13
SLIDE 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

slide-14
SLIDE 14

A First Attempt:
 Phrase-based Machine Translation

slide-15
SLIDE 15

A Better Attempt: Tree-based Machine Translation

slide-16
SLIDE 16

Trees don't Match NL!

slide-17
SLIDE 17

Transform 1: Heads

slide-18
SLIDE 18

Transform 2: Redundant Nodes

slide-19
SLIDE 19

Transform 3: Integrate Nodes

slide-20
SLIDE 20

Final Tree

slide-21
SLIDE 21

Experiments

slide-22
SLIDE 22

Django Dataset

  • Description: manually annotated descriptions for 18K lines
  • f code
  • Target code: one liners
  • Covers a wide range of real-world use cases like I/O
  • peration, string manipulation and exception handling

call the function _generator, join the result into a string, return the result Intent Target

slide-23
SLIDE 23

How Good are the Generated Descriptions?

  • Answer: Pretty good!
slide-24
SLIDE 24

How Useful are the Descriptions?

  • Generated pseudo-code improved readability

compared to no pseudo-code

slide-25
SLIDE 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.

slide-26
SLIDE 26

A Syntactic Neural Model for Code Synthesis from Natural Language

(ACL 2017) Joint Work w/ Pengcheng Yin

slide-27
SLIDE 27

Goal: Assistive Interfaces for Programmers

Interface by William Qian

slide-28
SLIDE 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)

slide-29
SLIDE 29

Sequence-to-sequence Models

(Sutskever et al. 2014, Bahadanau et al. 2015)

  • Neural network models for transducing sequences

sort list x backwards

RNN RNN RNN RNN RNN

</s>

RNN RNN RNN RNN

sort ( x , sort ( x , reverse ...

slide-30
SLIDE 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

sorted(my_list, reverse=True)

Surface Code

Deterministic transformation (using Python astor library)

Input Intent sort my_list in descending order Generated AST

Expr Call expr[func] expr*[args] keyword*[keywords] Name Name erpr

str(my_list)

keyword s tr(sorted) ....

NOTE: very nice contemporaneous work by Rabinovich et al. (2017)

slide-31
SLIDE 31

Generation Process

  • Factorize the AST into actions:
  • ApplyRule: generate an internal node in the AST
  • GenToken: generate (part of) a token
slide-32
SLIDE 32

Formulation as a Neural Model

NL Intent

Action Sequence LSTM Encoder LSTM Decoder Parent Feeding (Dong and Lapata, 2016) Action Flow

  • 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
slide-33
SLIDE 33

sort my_list in descending

  • rder

Pointer Net Softmax

...

Vocabulary

...

Softmax Input Words Generation Copy from Input

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. Copy prob. Final probability: marginalize over the two paths

Expr Call c] expr*[args] keyword*[keywords] Name erpr

str(my_list)

keyword ) ....

Derivation

slide-34
SLIDE 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

slide-35
SLIDE 35

HearthStone Dataset

<name> Divine Favor </name> <cost> 3 </cost> <desc> Draw cards until you have as many in hand as your [Ling et al., 2016] Intent (Card Property) Target (Python class, extracted from HearthBreaker)

  • Description: properties/fields of an HS card
  • Target code: implementation as a Python class from

HearthBreaker

slide-36
SLIDE 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]

slide-37
SLIDE 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 ☺

slide-38
SLIDE 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

slide-39
SLIDE 39

Learning to Mine NL/Code Pairs from Stack Overflow

(In Progress)

Joint Work w/
 Pengcheng Yin, Bowen Deng, Edgar Chen, Bogdan Vasilescu

slide-40
SLIDE 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
slide-41
SLIDE 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

slide-42
SLIDE 42

Mining Method

slide-43
SLIDE 43

Annotation

  • ~100 posts for Python/Java
slide-44
SLIDE 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?

slide-45
SLIDE 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.

slide-46
SLIDE 46

Main Results

  • On both Python and Java,

better results than heuristic strategies

  • Both structural and

correspondence features were necessary

slide-47
SLIDE 47

Transfer Learning

  • Can we perform classification w/ no labeled data for that

language?

Python Java

slide-48
SLIDE 48

Examples

slide-49
SLIDE 49

Future Work

  • Currently working on crowd-sourcing, where crowd

workers confirm or deny our model's extracted snippets

  • Will be released when it's ready! (very shortly?)
slide-50
SLIDE 50

Conclusion

slide-51
SLIDE 51

Conclusion

  • Data-driven language↔code within reach!
  • Modeling structure of the PL is important and

helpful

  • Data is difficult, but we're making progress
  • Let's do it together!
slide-52
SLIDE 52

Questions?