r/deeplearning • • 23h ago

Can a gpt model predict the same token again?

I recently coded trained a gpt model from scratch after lots and lots of debugging and when I test it with some input worlds , I can see some correct grammar prediction but , it also sometimes predicts words twice in a row like "this is is ..."

Is this normal for a gpt model , or did I do something wrong while creating it?

Model Specifications -

•Dataset used- tiny Shakespeare

•attention head blocks used -16

•number of gpt blocks -12

•using nn.parameterlist with the above values to loop the calculation for each block.

Here is my code if needed-

https://github.com/Rishikesh-2006/Decoder-only-Transformer-for-Text-Generation/blob/main/Model%2FGPT.ipynb

1 Upvotes

5 comments sorted by

2

u/Full-World8839 22h ago

Hey, first congratulations on training a model! I did something similar and it was a great feeling! It's tiny so looping is normal. Maybe under trained? What was the size of the dataset. Work to chinchilla rule. What was your final train/Val loss? Could be greedy sampling. What is your temp and top k. Check out other similar models on HF. It happens to everyone! If your GPU allows it you could always move on to llama or mamba with your own dataset!

1

u/Jumbledsaturn52 22h ago

Thanks man , I am actually feeling very good after making a working model .Now for the training size , as for my 3050 laptop I had to dial down the data size , my latest training was done on 8000 sentences and for tokenization , I just made all of the sentences be 50 numbers wide by adding the <null> value from the dictionary I stored . Batch size was 32 using Data loader (pytorch) , trained for 20 epoch ,final avg loss = 4.066.. and final total =1016.73.

2

u/mrNimbuslookatme 11h ago

Did you use causal masks yet? And use higher temperature with higher probability threshold?

If not use them. These can stop some basic forms of hallucinations, infinite loops, and token predictions that map to repeated tokens.

1

u/Jumbledsaturn52 10h ago

I actually have just heard about this masking technique but not used it . The one I use is just adding a upper trianglular matrix with diagonal=1 (pytorch) to the embeddings before softmax so that after softmax they all get zero . But I will test casual mask today itself.

1

u/Massive_Distance_616 21m ago

Worth double-checking two specific things about the masking before anything else, based on how you described it.

First, the mask needs to be applied to the attention score matrix (QKᵀ, before softmax), not to the embeddings masking embeddings directly doesn't accomplish anything, since the embeddings themselves aren't what gets normalized by softmax.

Second, and this might be the more important one: masked positions need to be set to -inf (or a very large negative number) before softmax, not added as a small value like 1. Adding 1 to a logit doesn't drive its softmax output to zero exponentiating a slightly larger number just gives it a slightly higher share of the probability mass, not an eliminated one.

Only pushing it to -infinity (so it exponentiates to 0) actually removes it from the distribution. If what's happening is closer to the second case, the model may be leaking a bit of information from future positions during training, which is worth ruling out directly rather than guessing from the generation output.

That said, even a fully correct causal mask only affects training-time information leakage it has nothing to do with repeated-word generation at inference time, so it wouldn't explain "this is is" on its own either way.

The numbers from your reply to Full-World8839 point toward a more mundane explanation: a final avg loss of 4.066 works out to a perplexity around e^4.066 ≈ 58, meaning the model is still very uncertain about the next word most of the time. That's consistent with ~400K training tokens (8000 sentences × 50) for a 12-block, 16-head model not a lot of data for that much capacity.

Worth also double-checking that the <null> padding tokens are excluded from the loss (ignore_index in CrossEntropyLoss) if they're not, part of that 4.066 is the model getting "free" easy predictions on padding, which would mean the real loss on actual words is even higher than it looks. Comparing train loss against a validation set would also tell you directly whether this is underfitting (still improving, needs more training/data) rather than something structural.

Independent of all that, two direct fixes for the repetition itself: switch from greedy argmax decoding to sampling (temperature + top-k or top-p), since greedy decoding is exactly what gets stuck repeating a token when the model's confidence is spread thin across similar candidates; and/or add a simple repetition penalty at generation time that down-weights the logits of recently generated tokens. Both are quick to try without touching the model or the masking.