Decoding LLM Parameters, Part 1: Temperature

LLM Parameters

Like any machine learning model, large language models have various parameters that control the variance of the generated text output. We have started a multi-part series to explain the impact of these parameters in detail. We will conclude by striking the perfect balance in content generation using all of these parameters discussed in our multi-part series.

Welcome to the first part, where we discuss the most well-known parameter, “Temperature.”

Temperature

If the goal is to control the randomness of the predictions, then temperature is the one for you. Lower temperature values make the output more deterministic, while higher values will make it more creative by allowing diverse outcomes.

Let us look at temperature in action using the following code and output. To present the importance simply, we chose to use hugging face transformers and the GPT2 model in particular.

Python

 

Output:

PowerShell

 

Let us understand the output:

  • Low Temperature (0.3): The model will focus on the most likely word choices. If precision and consistency matter to you, then give the temperature around this range. However, keep in mind that the model might get stuck repeating similar phrases, as is the case with our output here.
  • Medium Temperature (0.5): This temperature perfectly balances coherence and creativity. It is an excellent middle ground if you want a fair amount of variation without losing structure. As you can see in the output, a little balance has been added, but you could still see some repetition in the output.
  • High Temperature (0.9): This temperature blows up the LLM to be as creative as possible. As you can see, this output differs from the previous two, bringing in a lot of randomness and variance in the content.

The above example sets a fundamental understanding of temperature. Let us now look at it in a slightly more detailed way with a couple of use cases: “Creative Story Generation” and “Technical Explanation.”

Let us look at that with the following code to understand how temperature impacts the above 2 use cases.

Python

 

Output:

PowerShell

 

Now let us break and analyze the output for creative story generation and technical explanation based on the temperature settings and how the output was impacted. Also, we will observe how a temperature setting works perfectly for one use case and does the exact opposite for another use case.

Creative Story Generation

  • Low Temperature (Negative Impact): As you can see, the story output is highly repetitive and lacks variety. This result is not satisfying for a creative task, and the extreme repetitiveness caused by the model’s inability to introduce novel and innovative ideas makes it undesirable for storytelling.
  • High Temperature (Perfect Impact): As you can see from the output, the story takes interesting directions and is very creative. The output also adds multiple aspects to the story, which makes it varied, imaginative, and perfect for innovative storytelling.

Technical Explanation

  • High Temperature (Negative Impact): It is important to remember that maintaining factual accuracy is very important for a use case such as a technical explanation. High temperature leads to much randomness and less probable words being introduced into the generated content, making it unsatisfactory for technical writing. The same can also be inferred from the output above that it is too vague and includes irrelevant ideas.
  • Adjusted Temperature (Perfect Impact): We have adjusted the temperature to a setting that strikes a perfect balance for generating technical content. As you can see, the output is much more sorted right now. At this temperature setting, the model avoids repetitiveness like it does at lower temperatures and does not lose coherence like in higher temperatures.

Conclusion

You have seen all the ways in which temperature can affect content generation and which temperature setting is perfect for which use case. Also, note that adjusting the temperature is not the be-all and end-all of content generation; you will have to tweak other parameters as well. We will look at all of that in the upcoming articles in the series.

Source:
https://dzone.com/articles/decoding-llm-parameters-temperature