+ All Categories
Home > Documents > Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset •...

Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset •...

Date post: 28-May-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
52
Program Synthesis and Description with Structured Machine Learning Models Graham Neubig @Stanford CS379C 5/1/2018
Transcript
Page 1: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Program Synthesis and Description with Structured Machine Learning Models

Graham Neubig

@Stanford CS379C 5/1/2018

Page 2: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Coding =Concept → Implementation

sort list xin descending

orderx.sort(reverse=True)

Page 3: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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)

Page 4: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Program Understanding:Implementation → Concept

x.sort(reverse=True)

sort list xin descending

order

Page 5: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Today’s Agenda:Can Natural Language Help?

• Describing code with natural language

• Synthesizing code from natural language

• Bonus! Creating datasets to do so

Page 6: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Natural Language vs. Programming Language

Page 7: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Natural Language vs. Code

Note: Good summary in Allamanis et al. (2017)

Natural Language CodeHuman interpretable Human and machine interpretable

Ambiguous Precise in interpretation

Structured, but flexible Structured w/o flexibility

Page 8: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Structure in Code

x Load % 5 == 0

If

Compare

BinOp

Name Num Num

if x % 5 == 0:

AST ParserCan we takeadvantage ofthis for better

NL-code interfaces?

(used in models of Maddison & Tarlow 2014)

Page 9: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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.

Page 10: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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]

Page 11: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Why Generate NL Pseudo-code Descriptions?

Assisting Code ReadingPseudo-code can help

explain functionality of code

DebuggingCould provide a sanity check

for programmers

Page 12: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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]

Page 13: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Our Proposal: Treat Code Description as Translation!

Machine Translationもし x を 5 で 割り切れる なら

if x is divisible by 5

Code Descriptionif x % 5 == 0 :

if x is divisible by 5

Page 14: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

A First Attempt:Phrase-based Machine Translation

Page 15: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

A Better Attempt:Tree-based Machine Translation

Page 16: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Trees don't Match NL!

Page 17: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Transform 1: Heads

Page 18: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Transform 2: Redundant Nodes

Page 19: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Transform 3: Integrate Nodes

Page 20: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Final Tree

Page 21: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Experiments

Page 22: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

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

Intent

Target

Page 23: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

How Good are the Generated Descriptions?

• Answer: Pretty good!

Page 24: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

How Useful are the Descriptions?

• Generated pseudo-code improved readability compared to no pseudo-code

Page 25: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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.

Page 26: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

A Syntactic Neural Model for Code Synthesis from Natural Language

(ACL 2017)

Joint Work w/ Pengcheng Yin

Page 27: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Goal: Assistive Interfaces for Programmers

Interface by William Qian

Page 28: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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)

Page 29: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

...

Page 30: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

str(sorted)

....

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

Page 31: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Generation Process• Factorize the AST into actions:

• ApplyRule: generate an internal node in the AST• GenToken: generate (part of) a token

Page 32: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

Page 33: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

sort my_list in descending order

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

expr[func] expr*[args] keyword*[keywords]

Name

Name

erpr

str(my_list)

keyword

str(sorted)

....

Derivation

Page 34: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

Page 35: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

Page 36: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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]

Page 37: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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 ☺

Page 38: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

ExamplesIntent 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

Page 39: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Learning to Mine NL/Code Pairs from Stack Overflow

(In Progress)

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

Page 40: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

Page 41: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

Page 42: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Mining Method

Page 43: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Annotation

• ~100 posts for Python/Java

Page 44: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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?

Page 45: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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.

Page 46: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Main Results• On both Python and Java,

better results than heuristic strategies

• Both structural and correspondence features were necessary

Page 47: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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

language?Python Java

Page 48: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Examples

Page 49: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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?)

Page 50: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Conclusion

Page 51: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

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!

Page 52: Program Synthesis and Description with Structured Machine ... · 5/1/2018  · IFTTT Dataset • Over 70K user-generated task completion snippets crawled from ifttt.com • Wide variety

Questions?


Recommended