[Paper Review] Language Models of Code are Few-Shot Commonsense Learners
This paper proposes CoCoGen, a method that frames structured commonsense reasoning tasks—such as script and reasoning graph generation—as code generation problems using large language models of code (Code-LLMs). By converting structured outputs into executable Python code, CoCoGen enables Code-LLMs like Codex to outperform natural language LMs (e.g., T5, GPT-3) in few-shot settings across three diverse tasks, demonstrating superior structural accuracy and reasoning quality without task-specific fine-tuning.
We address the general task of structured commonsense reasoning: given a natural language input, the goal is to generate a graph such as an event -- or a reasoning-graph. To employ large language models (LMs) for this task, existing approaches ``serialize'' the output graph as a flat list of nodes and edges. Although feasible, these serialized graphs strongly deviate from the natural language corpora that LMs were pre-trained on, hindering LMs from generating them correctly. In this paper, we show that when we instead frame structured commonsense reasoning tasks as code generation tasks, pre-trained LMs of code are better structured commonsense reasoners than LMs of natural language, even when the downstream task does not involve source code at all. We demonstrate our approach across three diverse structured commonsense reasoning tasks. In all these natural language tasks, we show that using our approach, a code generation LM (CODEX) outperforms natural-LMs that are fine-tuned on the target task (e.g., T5) and other strong LMs such as GPT-3 in the few-shot setting.
Motivation & Objective
- To address the challenge of generating structured commonsense outputs (e.g., event graphs, reasoning graphs) using large language models.
- To identify the limitations of existing approaches that serialize structured outputs into flat, unnatural text formats unsuitable for natural language models.
- To explore whether code generation models, pre-trained on programming syntax and structure, can better handle structured reasoning tasks.
- To demonstrate that framing commonsense reasoning as code generation improves model performance in few-shot settings.
Proposed method
- Convert structured commonsense outputs (e.g., event or reasoning graphs) into executable Python code representations, preserving hierarchical and relational structure.
- Use pre-trained Code-LLMs (e.g., Codex) to generate these code representations directly from natural language prompts.
- Represent nodes as class instances and edges as parent-child relationships using object-oriented syntax in Python.
- Train or prompt Code-LLMs in a few-shot setting without task-specific fine-tuning, leveraging their pre-training on code corpora.
- Parse the generated code into valid structured outputs (e.g., graphs) for downstream evaluation.
- Use standard evaluation metrics such as F1, exact match, and structural accuracy to compare performance against natural language LMs.
Experimental results
Research questions
- RQ1Can Code-LLMs generate structured commonsense outputs more accurately than natural language LMs when fine-tuned on the same tasks?
- RQ2Does representing structured outputs as executable code improve model performance in few-shot reasoning settings?
- RQ3How does the structural fidelity of generated outputs compare when using code vs. serialized text formats (e.g., DOT or edge lists)?
- RQ4Can a single Code-LLM generalize across diverse structured commonsense reasoning tasks without task-specific fine-tuning?
Key findings
- In all three structured commonsense reasoning tasks—script generation, event graph construction, and reasoning graph generation—Codex outperformed fine-tuned T5 and GPT-3 models in the few-shot setting.
- The performance gain was particularly significant in structural accuracy, with Codex generating fewer syntactic and topological errors compared to natural language LMs.
- CoCoGen achieved higher F1 and exact match scores than baseline models, demonstrating that code representation better preserves graph structure than serialized text formats.
- The method showed robustness across diverse tasks, indicating that Code-LLMs can generalize to structured reasoning even when the input is purely natural language.
- The results suggest that the pre-training of Code-LLMs on programming syntax and semantics equips them with superior structural reasoning capabilities for structured generation.
- The authors observed that natural language LMs struggled with maintaining graph topology when generating serialized formats, while Code-LLMs preserved relationships through proper code structure.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.