{"id":940,"date":"2024-09-02T13:30:18","date_gmt":"2024-09-02T13:30:18","guid":{"rendered":"https:\/\/tbekk.com\/devstream\/?p=940"},"modified":"2024-09-02T13:31:25","modified_gmt":"2024-09-02T13:31:25","slug":"a-visual-explanation-of-llm-hyperparameters","status":"publish","type":"post","link":"https:\/\/tbekk.com\/devstream\/2024\/09\/02\/a-visual-explanation-of-llm-hyperparameters\/","title":{"rendered":"A visual explanation of LLM hyperparameters"},"content":{"rendered":"\n<p class=\"has-medium-gray-color has-text-color has-normal-font-size\">Understand temperature, Top-k, Top-p, Frequency &amp; Precense Penalty once and for all.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-medium-gray-color has-alpha-channel-opacity has-medium-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em><strong>Link:<\/strong><\/em> <a href=\"https:\/\/towardsdatascience.com\/a-visual-explanation-of-llm-hyperparameters-daf61d3b006e\"><em>towardsdatascience.com<\/em><\/a><\/li>\n\n\n\n<li><em><strong>Author:<\/strong><\/em> <a href=\"https:\/\/medium.com\/@alexobidiegwu?source=post_page-----6942c7cafd56--------------------------------\"><em><a href=\"https:\/\/medium.com\/@jenn-j-dev?source=post_page-----daf61d3b006e--------------------------------\">Jenn J.<\/a><\/em><\/a><\/li>\n\n\n\n<li><em><strong>Publication date:<\/strong><\/em> <em>Aug 30, 2024<\/em><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-medium-gray-color has-alpha-channel-opacity has-medium-gray-background-color has-background is-style-wide\"\/>\n\n\n\n<p id=\"6215\">Getting a handle on temperature, Top-k, Top-p, frequency, and presence penalties can be a bit of a challenge, especially when you\u2019re just starting out with LLM hyperparameters. Terms like \u201cTop-k\u201d and \u201cpresence penalty\u201d can feel a bit overwhelming at first.<\/p>\n\n\n\n<p id=\"b3ee\">When you look up \u201cTop-k,\u201d you might find a definition like: \u201cTop-k sampling limits the model\u2019s selection of the next word to only the top-k most probable options, based on their predicted probabilities.\u201d That\u2019s a lot to take in! But how does this actually help when you\u2019re working on prompt engineering?<\/p>\n\n\n\n<p id=\"3e92\">If you\u2019re anything like me and learn best with visuals, let\u2019s break these down together and make these concepts easy to understand once and for all.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"79a3\">LLMs under the hood<\/h1>\n\n\n\n<p id=\"5a55\">Before we dive into LLM hyperparameters, let\u2019s do a quick thought experiment. Imagine hearing the phrase \u201cA cup of \u2026\u201d. Most of us would expect the next word to be something like \u201ccoffee\u201d (or \u201ctea\u201d if you\u2019re a tea person!) You probably wouldn\u2019t think of \u201cstars\u201d or \u201ccourage\u201d right away.<\/p>\n\n\n\n<p id=\"496b\">What\u2019s happening here is that we\u2019re instinctively&nbsp;<em>predicting<\/em>&nbsp;the most likely words to follow \u201cA cup of \u2026\u201d, with \u201ccoffee\u201d being a much higher likelihood than \u201cstars\u201d.<\/p>\n\n\n\n<p id=\"84f4\">This is similar to how LLMs work \u2014 they calculate the probabilities of possible next words and choose one based on those probabilities.<\/p>\n\n\n\n<p id=\"55b6\">So on a high level, the hyperparameters are ways to tune&nbsp;<em>how we select the next probable words<\/em>.<\/p>\n\n\n\n<p id=\"cc86\">Let\u2019s start with the most common hyperparameter:<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"8b34\">Temperature<\/h1>\n\n\n\n<p id=\"a281\">Temperature controls the randomness of the models\u2019 output. A lower temperature makes the output more deterministic, favoring more likely words, while a higher temperature allows for more creativity by considering less likely words.<\/p>\n\n\n\n<p id=\"8a7d\">In our \u201cA cup of\u2026\u201d example, setting the temperature to 0 makes the model favor the most likely word, which is \u201ccoffee\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*XHNwK9UBbrvVe8_Idcu7EA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image provided by the author<\/figcaption><\/figure>\n\n\n\n<p id=\"e3fc\">As temperature increases, the sampling probabilities between different words start to even out, prompting the model to generate highly unusal or unexpected outputs.<\/p>\n\n\n\n<p id=\"fd66\">Note, that setting the temperature to 0 still doesn\u2019t make the model completely deterministic, though it gets very close.<\/p>\n\n\n\n<p id=\"c2d4\"><strong>Use cases<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Low temperature (e.g. 0.2): Ideal for tasks requiring precise and predictable results, such as technical writing or formal documentation.<\/li>\n\n\n\n<li>High temperature (e.g. 0.8 or above): Useful for creative tasks like storytelling, poetry, or brainstorming<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"a40b\">Max Tokens<\/h1>\n\n\n\n<p id=\"9999\">Max tokens define the maximum number of tokens (which can be words or parts of words) the model can generate in its responses. Tokens are the smallest units of text that a model processes.<\/p>\n\n\n\n<p id=\"d8e3\"><strong>Relationship between tokens and words:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 word = 1~2 tokens: In English, a typical word is usually split into 1 to 2 tokens. For example, simple words like \u201ccat\u201d might be a single token, while more complex words like \u201cunbelievable\u201d might be split into multiple tokens.<\/li>\n\n\n\n<li>The general rule of thumb: You can roughly estimate the number of words by dividing tokens by 1.5 (as a rough average).<\/li>\n<\/ul>\n\n\n\n<p id=\"e159\"><strong>Use cases<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Low max tokens (e.g. 50): Ideal for tasks requiring brief responses, such as headlines, short summaries, or concise answers. (Be careful that the model might cut off the output response)<\/li>\n\n\n\n<li>High max tokens (e.g. 500): Useful for generating longer content like articles, stories, or detailed explanations.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"2245\">Top-k<\/h1>\n\n\n\n<p id=\"e49e\">Top-k sampling restricts the model from selecting from the top&nbsp;<em>k<\/em>&nbsp;most likely next words. By narrowing the choices, it helps reduce the chances of generating irrelevant or nonsensical outputs.<\/p>\n\n\n\n<p id=\"6235\">In the diagram below, if we set&nbsp;<em>k<\/em>&nbsp;to 2, the model will only consider the two most likely next words \u2014 in this case, \u2018coffee\u2019 and \u2018courage.\u2019 These two words are then resampled, with their probabilities adjusted to sum to 1, ensuring one of them is chosen.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*374nBkpu2vH81XxbKD96Bg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image provided by the author.<\/figcaption><\/figure>\n\n\n\n<p id=\"d458\"><strong>Use cases:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Low k (e.g., k=10): Best for structured tasks where you want to maintain focus and coherence, such as summarization or coding.<\/li>\n\n\n\n<li>High k (e.g., k=50): Suitable for creative or exploratory tasks where you want to introduce more variability without losing coherence.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"0e78\">Top-p<\/h1>\n\n\n\n<p id=\"d6aa\">Top-p sampling selects the smallest set of words whose combined probability exceeds a threshold&nbsp;<em>p<\/em>&nbsp;(e.g., 0.9), allowing for a more context-sensitive choice of words.<\/p>\n\n\n\n<p id=\"4c77\">In the diagram below, we start with the most probable word, \u2018coffee,\u2019 which has a probability of 0.6. Since this is less than our threshold of&nbsp;<em>p<\/em>&nbsp;= 0.9, we add the next word, \u2018courage,\u2019 with a probability of 0.2. Together, these give us a total probability of 0.8, which is still below 0.9. Finally, we consider the word \u2018dreams\u2019 with a probability of 0.13, bringing the total to 0.93, which exceeds 0.9. At this point, we stop, having selected the first two most probable words.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*VqKeizk-ODwudYjnQrO3VA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image provided by the author.<\/figcaption><\/figure>\n\n\n\n<p id=\"630a\"><strong>Use cases:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Low p (e.g., p=0.5): Effective for tasks that require concise and to-the-point outputs, like news headlines or instructional text.<\/li>\n\n\n\n<li>High p (e.g., p=0.95): Useful for more open-ended tasks, such as dialogue generation or creative content, where a wider variety of responses is desirable.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"bed1\">Frequency Penalty<\/h1>\n\n\n\n<p id=\"945e\">A frequency penalty reduces the likelihood of the model repeating the same word within the text, promoting diversity and minimizing redundancy in the output. By applying this penalty, the model is encouraged to introduce new words instead of reusing ones that have already appeared.<\/p>\n\n\n\n<p id=\"3c49\">The frequency penalty is calculated using the formula:<\/p>\n\n\n\n<p id=\"7b06\"><strong>Adjusted probability = initial probability \/ (1 + frequency penalty * count of appearance)<\/strong><\/p>\n\n\n\n<p id=\"6792\">For example, let\u2019s say that the word \u201csun\u201d has a probability of 0.5, and it has already appeared twice in the text. If we set the frequency penalty to 1, the adjusted probability for \u201csun\u201d would be:<\/p>\n\n\n\n<p id=\"63f0\">Adjusted probability = 0.5 \/ (1 + 1 * 2) = 0.5 \/ 3 = 0.16<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*Z1VeRHdraE9AXrJTCoWlcw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image provided by the author.<\/figcaption><\/figure>\n\n\n\n<p id=\"dbe8\"><strong>Use cases:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>High Penalty (e.g., 1.0): Ideal for generating content where repetition would be distracting or undesirable, such as essays or research papers.<\/li>\n\n\n\n<li>Low Penalty (e.g., 0.0): Useful when repetition might be necessary or beneficial, such as in poetry, mantras, or certain marketing slogans.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"f1dc\">Presence Penalty<\/h1>\n\n\n\n<p id=\"765a\">The presence penalty is similar to the frequency penalty but with one key difference: it penalizes the model for reusing any word or phrase that has already been mentioned,&nbsp;<em>regardless of how often it appears<\/em>.<\/p>\n\n\n\n<p id=\"483e\">In other words, repeating the word 2 times is as bad as repeating it 20 times.<\/p>\n\n\n\n<p id=\"9841\">The formula for adjusting the probability with a presence penalty is:<br><strong>Adjusted probability = initial probability \/ (1 + presence penalty * presence)<\/strong><\/p>\n\n\n\n<p id=\"1ca9\">Let\u2019s revisit the earlier example with the word \u201csun\u201d. Instead of multiplying the penalty by the frequency of how many times \u201csun\u201d has appeared, we simply check whether it has appeared at all \u2014 in this case, it has, so we count it as 1.<\/p>\n\n\n\n<p id=\"cdc1\">If we set the presence penalty to 1, the adjusted probability would be:<\/p>\n\n\n\n<p id=\"6bb4\">Adjusted probability = 0.5 \/ (1 + 1 * 1) = 0.5 \/ 2 = 0.25<\/p>\n\n\n\n<p id=\"12b7\">This reduction makes it less likely for the model to choose \u201csun\u201d again, encouraging the use of new words or phrases, even if \u201csun\u201d has only appeared once in the text.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:1400\/1*BoVtpFQKzSPbMAWAnaml3A.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image provided by author.<\/figcaption><\/figure>\n\n\n\n<p id=\"07f4\"><strong>Use cases:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>High Penalty (e.g., 1.0): Great for exploratory or brainstorming sessions where you want the model to keep introducing new ideas or topics.<\/li>\n\n\n\n<li>Low Penalty (e.g., 0.0): Suitable for tasks where reinforcement of key terms or ideas is important, such as technical documentation or instructional material.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"4607\">Frequency and Presence Penalties often go hand-in-hand<\/h1>\n\n\n\n<p id=\"dd69\">Now that we\u2019ve gone over the basics, let\u2019s dive into how frequency and presence penalties are often used together. Just a heads-up, though \u2014 they\u2019re powerful tools, but it\u2019s important to use them with a bit of caution to get the best results.<\/p>\n\n\n\n<p id=\"f852\"><strong>When to use them:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Content Generation<\/li>\n\n\n\n<li>Preventing Redundancy<\/li>\n<\/ul>\n\n\n\n<p id=\"d3d6\"><strong>When to not use them<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Technical Writing: In technical documentation or specific instructions where consistent terminology is crucial, using these penalties might be counterproductive.<\/li>\n\n\n\n<li>Brand messaging: If you\u2019re generating content that relies heavily on a specific brand tone or key phrases, reducing repetition might dilute the brand\u2019s voice.<\/li>\n<\/ul>\n\n\n\n<p id=\"afe9\">By now, you should have a clearer picture of how temperature, Top-k, Top-p, frequency, and presence penalties work together to shape the output of your language model. And if it still feels a bit tricky, that\u2019s totally okay \u2014 these concepts can take some time to fully click. Just keep experimenting and exploring, and you\u2019ll get the hang of it before you know it.<\/p>\n\n\n\n<p id=\"e676\">If you find visual content like this helpful and want more, we\u2019d love to see you in our&nbsp;<a href=\"https:\/\/discord.gg\/ETGYYsswTb\" rel=\"noreferrer noopener\" target=\"_blank\">Discord community.<\/a>&nbsp;It\u2019s a space where we share ideas, help each other out, and learn together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand temperature, Top-k, Top-p, Frequency &amp; Precense Penalty once and for all. Getting a handle on temperature, Top-k, Top-p, frequency, and presence penalties can be a bit of a challenge,&#8230; <a class=\"read-more-link\" href=\"https:\/\/tbekk.com\/devstream\/2024\/09\/02\/a-visual-explanation-of-llm-hyperparameters\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,203],"tags":[464,215,199],"class_list":["post-940","post","type-post","status-publish","format-standard","hentry","category-article","category-llm","tag-hyperparameters","tag-llm","tag-prompt-engineering"],"_links":{"self":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/comments?post=940"}],"version-history":[{"count":3,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/940\/revisions"}],"predecessor-version":[{"id":943,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/posts\/940\/revisions\/943"}],"wp:attachment":[{"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/media?parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/categories?post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tbekk.com\/devstream\/wp-json\/wp\/v2\/tags?post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}