rasbt / LLMs-from-scratch

Implement a ChatGPT-like LLM in PyTorch from scratch, step by step
https://www.amazon.com/Build-Large-Language-Model-Scratch/dp/1633437167
Other
32.84k stars 3.95k forks source link

Incorrect formatting of the text as code (5.3.1 Temperature scaling) #317

Closed labdmitriy closed 3 months ago

labdmitriy commented 3 months ago

Bug description

Hi @rasbt,

Probably the sentence between two code blocks was mistakenly merged with this blocks:

def softmax_with_temperature(logits, temperature):
    scaled_logits = logits / temperature
    return torch.softmax(scaled_logits, dim=0)
Temperatures greater than 1 result in more uniformly distributed token probabilities,
and Temperatures smaller than 1 will result in more confident (sharper or more peaky)
distributions. Let's illustrate this by plotting the original probabilities alongside
probabilities scaled with different temperature values:
temperatures = [1, 0.1, 5]     #A
scaled_probas = [softmax_with_temperature(next_token_logits, T) for T in temperatures]
x = torch.arange(len(vocab))
bar_width = 0.15
fig, ax = plt.subplots(figsize=(5, 3))
for i, T in enumerate(temperatures):
    rects = ax.bar(x + i * bar_width, scaled_probas[i],
                           bar_width, label=f'Temperature = {T}')
ax.set_ylabel('Probability')
ax.set_xticks(x)
ax.set_xticklabels(vocab.keys(), rotation=90)
ax.legend()
plt.tight_layout()
plt.show()

Thank you.

What operating system are you using?

None

Where do you run your code?

None

Environment

d-kleine commented 3 months ago

in live book

labdmitriy commented 3 months ago

And also in PDF.

rasbt commented 3 months ago

Ah yes, you are right. It's fine in the layouted PDF but definitely looks incorrect online. One of the things that hopefully gets resolved soon when the layouts are synced. Thanks!

Screenshot 2024-08-12 at 7 05 12 PM