⚠️ This lesson is retired and might contain outdated information.

frequency_penalty Practice

Tom Chant
InstructorTom Chant
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 months ago

We explore the concept of Frequency Penalty in text generation using a chatbot. The goal is to reduce the repetition of phrases and words in generated text.

You'll be presented with a challenge to generate text with and without Frequency Penalty and compare the outputs. The differences in the generated responses are examined, highlighting the impact of the penalty. The importance of avoiding very high or negative penalty values is emphasized, as it can lead to nonsensical or poor-quality results. Additionally, the presence of strange language and grammatical errors when using high Frequency Penalty settings is noted.

[00:00] Okay, let's do some experimentation together with Frequency Penalty. And let's remind ourselves first what Frequency Penalty is supposed to do. We use a high frequency penalty to decrease the model's chances of repeating the same phrases.

[00:15] So what I'm going to do is set you a challenge which will involve generating some text which is likely to have some repeated words and phrases. Now because we're going to generate a lot of text and it will actually be pretty painful to watch the render typewriter text function trundle through all of it,

[00:33] I've actually commented out that function call right here and I've replaced it with this console.log so we can just log out the completion. Now I've also put a file up here called output.md and we can paste our completions in there and save them for comparison.

[00:50] So here is your challenge. I want you to set the Frequency Penalty to zero. Give the chatbot this query. Generate 20 ways to say you can't buy that because you're broke. Paste the results into output.md and then repeat that process with Frequency Penalty set to 2.

[01:10] Once you've done that you can examine the differences between the two outputs and see what Frequency Penalty is doing. Now I've just put a warning here. Do not set Frequency Penalty to minus 2. And I've said that because it actually breaks things quite spectacularly. It will just churn out the same word again and again and again

[01:28] until all of the tokens it's allowed have been used up and in fact it will probably crash the mini browser. OK, pause now. Have a go at that challenge. I'm going to go through the same process and we'll have a look at the two completions and compare them. OK, so hopefully you managed to do that just fine.

[01:51] So I'm just going to quickly go through that process and paste my completions into output.md. OK, so I've got my two outputs and if we compare them side by side, well, they both start OK. So we've got at Frequency Penalty zero. Your financial situation doesn't permit you to make that purchase.

[02:15] And then, unfortunately, your funds are insufficient for buying that item. Both of those are fine. Both great English. Let's have a look at the first two that we've got when Frequency Penalty was set to 2. Unfortunately, your current financial situation doesn't allow for that purchase.

[02:32] It seems your wallet is feeling a bit light to acquire that item. OK, a little bit quirky, but no problem. Now let's come to the very end because it's the last part that's going to be most difficult when the Frequency Penalty is high. Now, number 20 here was with your financial setting, it's infeasible to add that to your possessions.

[02:53] Well, that's OK. It's correct English. It's a little bit strange. Now, down here, we've got monetary scarcity dictates frugality and discipline. Despite passions give merit where it's due, nullifying all impulses towards luxury, at least for now.

[03:12] I can't decide if that's poetry or gibberish. I think it's just gibberish. And actually, if we look a little bit further up, we can see that by setting the Frequency Penalty high, we've really caused open AI problems because this is the first time that we're actually seeing bad English.

[03:29] We're actually getting completions that don't seem like they come from a human. Take, for example, number 17. It's apparent after analyzing existing revenue streams, financing the stream can simply just about only be squeezed from another worldly dimension entirely. Don't know what that means.

[03:47] On number 18, empty pockets rarely find footing when chasing certain ambitions. In our material world, might as well try catching stardust instead. Again, kind of poetic, but these weird little mistakes as well. There's a comma there, but no space. Now, one other thing I want to show you is this.

[04:06] I'm just going to highlight every instance of the word financial. Now we can see if I scroll down, that word appears seven times. But five of those times are when Frequency Penalty was set to zero, and only two of them were when it was set to two.

[04:22] So that shows us that the Frequency Penalty is penalizing the word financial. It's only letting it appear twice. And when we're talking about money, it is quite an important word. Let's do the same thing with the word purchase. Again, the word purchase is used seven times, five times with Frequency Penalty at zero,

[04:42] and just twice with Frequency Penalty set to two. So we can really see why the model is struggling to generate new language. Okay, so what is the bottom line? Well, here is my general advice for Presence Penalty and Frequency Penalty. And I'm going to express this as a flowchart.

[05:01] Firstly, ask yourself, is there a problem? If the answer's no, do nothing. If the answer's yes, then my advice is this. Don't go over one for either setting. Don't go under one for either setting. You can experiment outside of those parameters, but I would recommend staying inside them,

[05:21] else you run the risk of getting some pretty strange results. And lastly, it's all about making small changes and testing. If you just make a small change and test, you'll very quickly find the settings which work for you and get you the results you want. So what are we going to do in this app?

[05:39] Well, I'm going to leave Presence Penalty at zero. I don't think it's doing too much for us anyway. And Frequency Penalty, I'm going to put to 0.3. And I'm going to do that just because I've had a play around with this for a long time, and I think that is where we get the best results. You, of course, are free to differ.

[05:57] Okay, so the chatbot is working pretty well. So next, I want to go back to where we started, to this instruction right here, and have a bit of fun with it and just see how we can alter the chatbot's personality and why that might be useful. When you're ready for that, let's move on.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today