Solving Challenging Math Word Problems Using GPT-4 Code Interpreter with Code-based Self-Verification

Aojun Zhou, Ke Wang, Zimu Lu, Weikang Shi, Sichun Luo, Zipeng Qin, Shaoqing Lu, Anya Jia, Linqi Song, Mingjie Zhan, Hongsheng Li

TL;DR

This paper introduces Explicit Code-based Self-Verification (CSV), a prompting strategy that leverages GPT-4’s Code Interpreter to not only solve complex math problems but also write code to verify its own answers. By combining this self-correction loop with a novel weighted majority voting system, the researchers achieved a state-of-the-art 84.3% accuracy on the challenging MATH dataset, a massive leap over standard GPT-4 performance.

What Problem Does This Paper Address?

Large Language Models (LLMs) often struggle with multi-step mathematical reasoning. Even powerful models like GPT-4 frequently "hallucinate" incorrect logic or make simple arithmetic errors. While techniques like Chain-of-Thought (CoT) help, they don't provide a mechanism for the model to realize it has made a mistake mid-calculation.

Comparison of GPT-4 solving a math problem without code, with limited code, and with full code interpreter usage.

OpenAI’s GPT-4 Code Interpreter (now known as Advanced Data Analysis) showed a natural aptitude for math by writing Python scripts to handle calculations. However, there was no systematic understanding of why it was better or how to maximize its potential. This paper seeks to bridge that gap by formalizing how code usage frequency impacts reasoning and how to turn the code interpreter into an autonomous "self-checker."

Methods

The authors propose two primary innovations:

  1. Explicit Code-based Self-Verification (CSV): Instead of just asking for an answer, the model is given a zero-shot prompt: "Solve the problem using code interpreter step by step... and following your answer, please verify it using code interpreter by yourself." This forces the model into a loop:
  2. Solve: Generate a solution ($C$).
  3. Verify: Generate code to check the answer ($V$).
  4. Rectify: If the verification returns "False," the model automatically treats its previous logic as flawed and attempts a new solution ($C_{new}$).
  5. Verification-guided Weighted Majority Voting (VW-Voting): Standard majority voting (taking the most frequent answer from $k$ attempts) treats all paths equally. The authors realized that a solution verified as "True" by the code is much more likely to be correct than one that is "Uncertain" or "False." They assign different weights ($w_T, w_U, w_F$) to these states, significantly improving the final answer selection.

Datasets

The researchers evaluated their method on three major benchmarks:

  • MATH: 5,000 hard competition-level problems across subjects like Algebra, Geometry, and Number Theory.
  • GSM8K: 8,500 grade-school math word problems.
  • MMLU-Math: A subset of the Massive Multitask Language Understanding benchmark focusing specifically on mathematics.
  • Open Source Check: They also tested the CSV method on CodeLlama (7B and 34B) to see if the benefits generalized to smaller, open-source models.

Key Insights and Ideas

  • The "Self-Debugging" Phenomenon: The authors found that GPT-4 Code Interpreter’s success isn't just about calculation; it's about the feedback loop. When the Python interpreter throws an error or an "illogical" result, the model uses that signal to rethink its strategy—a process they call "self-debugging."
  • Code Usage Correlation: There is a direct positive correlation between how often a model uses code and its accuracy, especially as problem difficulty increases.
  • High Precision Verification: When the model's self-verification code returns "True," it is correct 95.88% of the time. This high precision is what makes the weighted voting system so effective.
  • Natural Language vs. Code: The paper proves that verifying via code is far superior to verifying via natural language. In fact, asking the model to verify in plain English sometimes decreased performance because the model would just repeat its own flawed logic.

Key Results

The results represent a significant step forward in LLM reasoning:

  • MATH Dataset: The CSV + VW-Voting method reached 84.32% accuracy. For context, the base GPT-4 (without code) scored 42.2%, and the standard GPT-4 Code Interpreter scored 69.7%.
  • GSM8K: Achieved 97.0% accuracy, nearing a "ceiling" for this dataset.
  • MMLU-Math: Achieved 94.5% accuracy.
  • Open Source: While CodeLlama saw improvements (e.g., CodeLlama-34B jumped from 9.12% to 13.36% on MATH), the gap between open-source and GPT-4 remains vast, likely because self-verification requires a high baseline level of reasoning to be effective.

Limitations and Future Work

  • Geometry: The model still struggles with Geometry (only 64.9% accuracy) compared to Algebra (95.6%). This is because geometry often requires spatial reasoning and multi-modality (vision), which the text-based code interpreter lacks.
  • Computational Cost: Running multiple reasoning paths ($k=16$) with self-correction loops is computationally expensive and increases latency.
  • Dependency on Model Strength: The "self-verification" relies on the model being smart enough to write a valid test for its own answer. Weaker models often write buggy verification code, leading to "False Positives."

Future research may focus on bringing these advanced "self-correcting" capabilities to smaller, more efficient open-source models or integrating vision-language models to tackle the geometry gap.

Summary updated

BibTeX
@inproceedings{zhou2024solving,
  author = {Aojun Zhou and Ke Wang and Zimu Lu and Weikang Shi and Sichun Luo and Zipeng Qin and Shaoqing Lu and Anya Jia and Linqi Song and Mingjie Zhan and Hongsheng Li},
  title = {{Solving Challenging Math Word Problems Using GPT-4 Code Interpreter with Code-based Self-Verification}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=c8McWs4Av0},
}
Figure