[Paper Review] Optimizing Large Language Model Hyperparameters for Code Generation
This study systematically evaluates the impact of four key hyperparameters—temperature, top-p, frequency penalty, and presence penalty—on code generation performance using the GPT-3.5 Turbo model across 13 Python tasks. It identifies optimal settings (temperature < 0.5, top-p < 0.75, frequency penalty ∈ [−1, 1.5], presence penalty > −1) that maximize functional code output, providing empirical evidence for hyperparameter tuning in LLM-based code generation.
Large Language Models (LLMs), such as GPT models, are increasingly used in software engineering for various tasks, such as code generation, requirements management, and debugging. While automating these tasks has garnered significant attention, a systematic study on the impact of varying hyperparameters on code generation outcomes remains unexplored. This study aims to assess LLMs' code generation performance by exhaustively exploring the impact of various hyperparameters. Hyperparameters for LLMs are adjustable settings that affect the model's behaviour and performance. Specifically, we investigated how changes to the hyperparameters: temperature, top probability (top_p), frequency penalty, and presence penalty affect code generation outcomes. We systematically adjusted all hyperparameters together, exploring every possible combination by making small increments to each hyperparameter at a time. This exhaustive approach was applied to 13 Python code generation tasks, yielding one of four outcomes for each hyperparameter combination: no output from the LLM, non executable code, code that fails unit tests, or correct and functional code. We analysed these outcomes for a total of 14,742 generated Python code segments, focusing on correctness, to determine how the hyperparameters influence the LLM to arrive at each outcome. Using correlation coefficient and regression tree analyses, we ascertained which hyperparameters influence which aspect of the LLM. Our results indicate that optimal performance is achieved with a temperature below 0.5, top probability below 0.75, frequency penalty above -1 and below 1.5, and presence penalty above -1. We make our dataset and results available to facilitate replication.
Motivation & Objective
- To investigate how hyperparameter tuning affects the correctness and functionality of code generated by large language models (LLMs).
- To identify optimal hyperparameter configurations that maximize the generation of correct and executable Python code.
- To address the lack of systematic empirical analysis on hyperparameter impact in LLM-based code generation, particularly in software engineering contexts.
- To provide a reproducible dataset and results to support future research and practical deployment of LLMs in code generation.
Proposed method
- An exhaustive hyperparameter search was conducted across 1,134 unique combinations of temperature, top-p, frequency penalty, and presence penalty.
- For each combination, the GPT-3.5 Turbo API generated code for 13 predefined Python code generation tasks.
- Generated code was evaluated using automated validation: no output, non-executable code, failing unit tests, or correct and functional code.
- Unit tests were implemented using Python’s unittest library and assertion checks to verify correctness.
- Statistical analysis using correlation coefficients and regression trees identified hyperparameter influence on code quality outcomes.
- The study excluded max tokens and logit bias to avoid artificial constraints on output length and content.
Experimental results
Research questions
- RQ1Does variation in LLM hyperparameters lead to a measurable impact on the quality of code generated by LLMs?
- RQ2What are the optimal hyperparameter settings for achieving the highest rate of correct and functional code generation in Python tasks?
- RQ3Which hyperparameters most significantly influence code correctness, executability, and generation success?
- RQ4How do default hyperparameter settings from OpenAI’s API compare to optimized configurations in terms of code generation performance?
Key findings
- Temperature has the strongest influence on code generation outcomes, with values below 0.5 yielding the highest rates of correct and functional code.
- Top-p values below 0.75 significantly reduce the likelihood of unit test failures, improving code correctness.
- Frequency penalty settings between −1 and 1.5 reduce the risk of generating non-executable code, with extreme values increasing failure rates.
- Presence penalty above −1 leads to marginal improvements in code quality, though its overall impact is limited.
- The default hyperparameter settings from OpenAI’s API do not consistently produce optimal results, indicating a need for task-specific tuning.
- A total of 14,742 code segments were generated and evaluated, forming a comprehensive dataset for replication and further study.
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.