Text2Reward: Automated Dense Reward Function Generation for Reinforcement Learning

Tianbao Xie, Siheng Zhao, Chen Henry Wu, Yitao Liu, Qian Luo, Victor Zhong, Yanchao Yang, Tao Yu · DOI

Introduction

Reinforcement Learning (RL) relies heavily on reward functions to guide agent behavior. However, designing these functions presents a significant challenge. Sparse rewards are simple to define but lead to inefficient learning, while dense rewards require meticulous "reward shaping" by experts to prevent unintended behaviors. Text2Reward addresses this by leveraging Large Language Models (LLMs) to automatically generate interpretable, dense reward functions as Python code from natural language instructions. This framework bridges the gap between human intent and the mathematical requirements of RL, enabling agents to master complex robotic tasks and allowing for refinement through conversational feedback.

Methodology

The Text2Reward framework utilizes a three-stage pipeline to transform high-level instructions into high-performing RL policies.

Overview of the Text2Reward framework, illustrating the pipeline from environment description and goal to reward generation and human-in-the-loop refinement.
  1. Expert Abstraction: The environment is presented to the LLM using a "Pythonic" representation. By defining classes for robots and objects with clear properties (e.g., pose, velocity), the LLM can reason about the task using familiar coding structures rather than raw sensor data.
  2. Reward Generation: Using models like GPT-4, the system generates a compute_reward function. This process incorporates few-shot prompting—retrieving relevant examples from a library—and code execution feedback to ensure the generated code is syntactically correct before training begins.
  3. Human-in-the-Loop Refinement: If the initial policy exhibits suboptimal behavior, a human can provide natural language feedback (e.g., "keep the chair standing"). The LLM then iteratively updates the reward code based on this critique, effectively acting as a translator between human observation and reward logic.

Experimental Evaluation

The framework was evaluated across several rigorous RL benchmarks, including ManiSkill2, MetaWorld, and Gym MuJoCo. These environments cover a wide range of tasks from robotic manipulation to creative locomotion.

Performance comparison on ManiSkill2 tasks. Text2Reward (Zero-shot and Few-shot) often matches or exceeds the performance of hand-written Oracle rewards.

Key results include:

  • Competitive Performance: In 13 out of 17 manipulation tasks, Text2Reward matched or outperformed "Oracle" rewards—functions manually tuned by human experts.
  • Creative Locomotion: The system successfully taught agents novel behaviors in MuJoCo environments, such as front flips, with success rates exceeding 94%.
  • Efficient Iteration: Human feedback proved highly effective; for tasks where initial rewards failed, success rates improved from 0% to nearly 100% in fewer than three iterations.
  • Real-World Transfer: Policies trained in simulation were successfully deployed on a physical Franka Panda robot arm for tasks like cube stacking without additional tuning.

Discussion and Key Insights

A primary strength of Text2Reward is its use of "Pythonic" grounding, which allows LLMs to generate staged rewards using logical blocks (e.g., if-else statements). This mirrors the approach taken by human experts and results in highly interpretable code. Unlike "black-box" neural reward models, the generated code can be audited and manually adjusted for safety and alignment.

However, limitations remain. The system currently relies on pre-defined abstract variables, creating a perception bottleneck. Future work aims to integrate neural perception models to enable end-to-end "pixels-to-reward" generation. Additionally, while the iterative process is robust, it is sensitive to the quality of the initial code generation.

Conclusion

Text2Reward demonstrates that LLMs can effectively automate the complex process of reward shaping. By treating reward design as a code generation and conversational debugging task, the framework makes RL more accessible and interpretable, paving the way for more intuitive human-robot interaction.

Summary updated

BibTeX
@inproceedings{xie2024text,
  author = {Tianbao Xie and Siheng Zhao and Chen Henry Wu and Yitao Liu and Qian Luo and Victor Zhong and Yanchao Yang and Tao Yu},
  title = {{Text2Reward: Automated Dense Reward Function Generation for Reinforcement Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=tUM39YTRxH},
}
Figure