How LLMs Predict the Next Word
45sUses a simple analogy to explain the core mechanism of LLMs, making a complex topic instantly understandable.
▶ Play ClipThe video explains how large language models (LLMs) work, focusing on their core function as next-word predictors. It describes the training process, the transformer architecture, and how models are fine-tuned to become useful conversational agents.
An LLM is a complex mathematical function that predicts the next word in any text, outputting all possible words with probabilities.
By providing a prompt that sets up a conversation, the model predicts the assistant's response word by word, repeating until the reply is complete.
Allowing random selection among lower-probability words makes responses more natural and varied, even though the underlying calculations are deterministic.
Models like GPT-3 are trained on vast internet text corpora, equivalent to over 2,600 years of continuous reading.
The model's behavior is determined by billions of parameters (weights), which are initially random and then adjusted through training.
For each example, the model predicts the last word, compares its probabilities to the actual word, and uses backpropagation to update parameters.
Training the largest models requires over 100 million years of computation at a billion operations per second.
After pretraining, models undergo RLHF where human workers flag unhelpful or problematic outputs, further tuning the model.
Massive parallel computations are made possible by GPUs, which perform many calculations simultaneously.
Introduced by Google in 2017, transformers process all words in parallel rather than sequentially, enabling efficient training.
Words are converted into numerical vectors (embeddings) because neural networks operate on continuous values.
Attention allows embeddings to interact and update based on context, enabling parallel understanding of word relationships.
A second type of computation in transformers stores learned linguistic patterns.
Information flows through repeated attention and feed-forward layers, enriching embeddings for accurate next-word prediction.
The last embedding in the sequence is used to produce a probability distribution over all possible next words.
Overall model behavior emerges from billions of parameter adjustments during training, making it difficult to pinpoint why a specific output is produced.
LLMs are powerful next-word predictors trained on massive data using transformers and GPUs. Despite their complexity, they produce fluent and useful text, though their inner workings remain largely emergent and not fully explainable.
"The title accurately reflects the content: a concise explanation of large language models."
What is the core function of a large language model?
It predicts the next word in any text, outputting all possible words with probabilities.
0:37
How does a chatbot generate responses using an LLM?
By providing a prompt that sets up a conversation, the model predicts the assistant's response word by word, repeating until the reply is complete.
0:51
Why can the same question lead to different answers from an LLM?
Because randomness is introduced by allowing selection among lower-probability words, making responses more natural.
1:13
How much text was used to train GPT-3?
Equivalent to over 2,600 years of continuous reading.
1:39
What are the parameters (weights) in an LLM?
Numbers that determine the model's behavior, initially random and then adjusted through training.
1:56
What is backpropagation used for in training?
To update all parameters in the model so that probabilities favor the correct word over other options.
2:43
How long would it take to compute the training of the largest LLMs at a billion operations per second?
Over 100 million years.
3:39
What is reinforcement learning from human feedback (RLHF)?
A training stage where human workers flag unhelpful or problematic outputs, further tuning the model.
3:56
What hardware enables the massive parallel computations needed for LLM training?
GPUs (Graphics Processing Units).
4:23
What was the key innovation of the transformer architecture introduced by Google in 2017?
It processes all words in parallel rather than sequentially.
4:36
Why are words converted into numerical vectors (embeddings) in LLMs?
Because neural networks operate on continuous values, not words.
4:57
What does the attention mechanism do in a transformer?
It allows embeddings to interact and update based on context, enabling parallel understanding of word relationships.
5:13
What is the role of the feed-forward network in a transformer?
It stores learned linguistic patterns.
5:41
How is the final next-word prediction produced?
The last embedding in the sequence is used to produce a probability distribution over all possible next words.
6:07
Why is it difficult to explain why an LLM makes a specific choice?
Because overall behavior emerges from billions of parameter adjustments during training.
6:42
LLMs are next-word predictors
Fundamental insight that demystifies LLMs as simple mathematical functions.
0:37Vast training data
Illustrates the immense scale of data required for training.
1:28Computational scale of training
Highlights the enormous computational resources needed.
3:09Transformer parallel processing
Key architectural innovation that enabled modern LLMs.
4:32Attention mechanism
Core technique for contextual understanding in transformers.
5:10Emergent behavior
Explains why LLM outputs are not fully predictable.
6:28[00:01] تخيّل أنّك وجدت نص
[00:03] يصف مشهداً بين شخص ومساعده يصف حديثًا بين شخص و مساعد وهمي يعمل بواسطة الذكاء الاصطناعي.
[00:06] يعمل بالذكاء الاصطناعي.
[00:07] يحتوي النّص على السؤال الّذي طرحه الشّخص، و لكن قد تمّ حذف إجابة المساعد.
[00:13] لنفترض أيضًا أنّ لديك هذه الآلة السّحريّة اللّتي تأخذ
[00:16] أي نص و تقدّم تخمين منطقي للكلمة التّالية في ذاك النّص.
[00:21] اذًا، يمكنك إكمال المشهد عن طريق استعمال الآلة
[00:25] للتنبّؤ بأوّل كلمة من الجواب،
[00:28] و من ثمّ إعادة التّكرار مع النّصوص المحدّثة حتّى يكتمل الحوار.
[00:33] عندما تتفاعل مع محادث آلي، هذا بالضبط ما يحدث.
[00:37] أي نموذج لغوي كبير هو مجرّد دالة رياضيّة معقّدة
[00:40] تتنبّأ بالكلمة الّتالية لأي نص.
[00:44] و لكن، بدلًا من أن يقدّم النّموذج كلمة واحدة فقط،
[00:47] هو يقدّم جميع الخيارات مع احتمالاتها المختلفة.
[00:51] لبناء محادث آلي، عليك تقديم نص صغير يفترض محادثة بين شخص
[00:56] و مساعده الوهمي الّذكي. تضيف في أوّل النص سؤال الشّخص.
[01:02] و من بعدها، تدع النموذج يتنبّأ بالكلمة التّالية من جواب المساعد الوهمي.
[01:07] يتم إعادة تكرار هذه الآليّة، و يقدّم الجواب الكامل للمستخدم.
[01:13] بل و يمكن جعل رد المحادث الآلي أشبه بالحديث الطبيعي
[01:16] من خلال السماح لـه على بالاختيار عشوائيًّا بعض الكلمات ذو احتمالات أصغر.
[01:20] إذن، رغم أنّ حسابات النّموذج حتميّة ،
[01:23] نفس السؤال قد يؤدّي إلى إجابات مختلفة.
[01:28] تتعلّم النّماذج كيفيّة إجراء التنبؤات من خلال تدريبهم بكميّات هائلة من النصوص،
[01:32] عادةً تنزّل عن الإنترنت.
[01:34] مثلًا، النصوص الّلتي استعملت لتدريب GPT-3،
[01:39] تستغرق الانسان أكثر من ٢٦٠٠ عام من القراءة المتواصلة.
[01:44] و النماذج الأحدث يتم تدريبها على كميّات أكبر فأكبر.
[01:48] يمكنك مقارنة عمليّة التّدريب بعمليّة تعديل المفاتيح على آلة كبيرة.
[01:52] يتم تحديد عمل النّموذج كاملًا جرّاء هذه الدّوزنة
[01:56] والأرقام الموزّعة على المفاتيح وهي تلقّب بالمعايير أو الأوزان.
[02:01] تغيير هذه المعايير يؤدّي إلى تعديل الإحتمالات
[02:04] اللَتي يحتسبها النّموذج لكلمته التّالية في نص معيّن.
[02:07] و يتم وصفها بنماذج لغويّة كبيرة لأنّها
[02:10] قد تحتوي على مئات المليارات من هذه المعايير.
[02:15] لا تحدّد قيم هذه المعايير باليد،
[02:18] بل هي توزّع عشوائيًّا قي بداية الأمر، أي خيارات النموذج خالية من أي معنى.
[02:22] ثم يتم دوزنتها بناءً على العديد من الأمثلة النصّيّة.
[02:27] قد يشمل المثل مجرّد عبارة قصيرة
[02:30] و نص كبيرجدّا. و لكن في كلا الحالتين
[02:34] يعمل النّموذج من خلال حذف كلمة النص الأخيرة،
[02:38] التنبّؤ بالخيارات و من بعدها مقارنة الخيارات و احتمالاتها مع كلمة النص الأصليّة.
[02:43] عندها، تستعمل آلية تعرف بالانتشار الخلفي لتحديث قيم جميع المعايير في النموذج.
[02:47] و الهدف تعديل الاحتمالات في صالح الكلمة الصحيحة
[02:51] على حساب الخيارات الأخرى.
[02:55] بعد تكرار عمليّة التّدريب على آلاف المليارات من الأمثلة،
[02:58] يبدأ النّموذج بتقديم تنبّؤات أدق على بيانات التّدريب،
[03:03] بل و يبدأ بتقديم تخمينات منطقيّة و صحيحة على نصوص جديدة
[03:07] لم يدرّب عليها.
[03:09] نظرًا للعدد الهائل من الأوزان و الكم الهائل من بيانات التّدريب،
[03:13] حجم العمليات الحسابيّة الّتي يشملها تدريب نموذج لغوي كبير يفوق الاستيعاب.
[03:19] على سبيل المثل، لنفترض أنّه باستطاعتك
[03:22] حساب مليار عمليّة رياضيّة في كل ثانية.
[03:26] كم تعتقد سيستغرق الأمر إن أجريت جميع
[03:29] الحسابات المطلوبة لتدريب أكبر النماذج الغوية؟
[03:33] سنة؟
[03:36] أم ١٠،٠٠٠ سنة؟
[03:39] الجواب الفعلي أضخم بكثي
[03:41] وهو في الواقع يفوق ١٠٠ مليون سنة.
[03:45] و لكن هذا فقط جزء من القصّة.
[03:47] هذه العمليّة بأكملها تسمّى التّدريب المسبق.
[03:49] قدرة النّموذج على أكمال نص عشوائي
[03:52] عن الإنترنت يختلف بكثير كونه مساعد ذكي فعّال و مفيد.
[03:56] لمعالجة ذلك، تخضع المحادثات الآليّة لنوع آخر من التّدريب،
[04:00] و هو يعرف بالتعليم المعزّز بالمراجعة البشريّة.
[04:04] يقوم العاملون بالإبلاغ عن تنبؤات غير مفيدة أو إشكاليّة.
[04:07] تصليحها يساهم في تعديل قيم المعايير
[04:11] مما يجعل تنبؤات النموذج أنسب للمستخدمين.
[04:14] إجراء الحسابات الهائلة في التدريب المسبق
[04:18] كان مستحيل لولا رقاقات حاسوبيّة خاصة تجري عدّة حسابات بالتوازي أي سويًّا.
[04:23] و هي تعرف ب"وحدات معالجة الرسوميّات" أو GPUs.
[04:28] و لكن، ليست كل النماذج قابلة لهذة الحسابات المتوازية.
[04:32] قبل ٢٠١٧، كانت نماذج اللّغات تعالج كل كلمة على حدة.
[04:36] عندها، قام فريق باحثين من Google بتقديم نموذج جديد يعرف كال"محوّل".
[04:43] و هو لا يقرأ النص من البداية حتّى النّهاية،
[04:46] بل يعالج النّص كاملًا بالتوازي.
[04:49] الخطوة الأولى داخل المحوّل، و معظم النّماذج اللّغوية ،
[04:54] هي تعيين قائمة طويلة من الأرقام لكل كلمة.
[04:57] وذلك لأن عمليّة التدريب تعمل فقط مع قيم مستمرّة خاضعة للحسابات الرياضيّة،
[05:02] على عكس الكلمات. لذا نرمّز اللّغة من خلال الأرقام.
[05:05] و بالعكس، كل قائمة تدل بطريقة ما إلى معنى
[05:09] الكلمة المتّصلة بها.
[05:10] ما يجعل المحوّلات فريدة هو اعتمادها على
[05:13] عمليّة خاصة تعرف بالانتباه.
[05:16] هذه العمليّة تمنح القوائم فرصة على التّفاعل مع بعضهما البعض.
[05:21] بالتّالي، تحدّث معاني الكلمات الّتي ترمز إليها بحسب السياق المحيط بالكلمة.
[05:25] و كل ذلك يتم بالتوازي.
[05:27] مثلًا قائمة الأرقام اللّتي ترمز إلى كلمة مصرف قد تتعدّل بناءً
[05:31] على سياق جديد لتضيف إلى الكلمة معنى آخر، وهو مصرف مياه.
[05:37] المحوّلات أيضّا تتضمّن نوع ثاني من الحسابات يعرف
[05:41] كشبكة عصبيّة أمامية الاتجاه. ذلك يخوّل النموذج
[05:44] على تخزين المزيد من الأنماط اللغوية المكتسبة أثناء التدريب.
[05:49] تتدفق كل هذه المعلومات في تكرارتٍ عدّة
[05:53] لهاتين الحسابتين الأساسيتين. الهدف
[05:56] أن تغتني كل قائمة من الأرقام، فترمز لمعلومات جديدة
[06:00] قد يحتاجها النّموذج ليقدّم تنبّؤ دقيق للكلمة
[06:04] التّالية في أي نص يدخب إليه.
[06:07] في النّهاية، يتم إجراء دالة حسابيّة أخيرة على آخر قائمة في تسلسل الكلمات من النص.
[06:11] و هي قد تأثّرت بسياق النص المدخول،
[06:16] بالإضافة إلى كل ما تعلّمه النّموذج خلال التّدريب
[06:19] لإنتاج تنبّؤ عن الكلمة التّالية في النص.
[06:22] و هذا التنبّؤ هو مكوّن من جميع الخيارات مع احتمالاتها المختلفة.
[06:28] على الرغم من أن الباحثين يصممون إطار عمل كل خطوة,
[06:32] من المهم أن نفهم أنّ سلوك النّموذج الإجمالي هو ظاهرة تنشأ
[06:37] بناءً على دوزنة مئات المليارات من المعايير خلال التّدريب.
[06:42] بالتّالي، من الصّعب للغاية تحديد
[06:45] سبب قيام النّموذج بأي خيار معيّن.
[06:48] ما نعرفه معرفته هو أنّه عند استخدام النّماذج اللّغويّة الكبيرة لإكمال نص،
[06:53] الكلمات الّتي تنتج تتسم بطلاقة مفاجأة و رائعة و حتّى مفيدة.
[07:05] إن كنت مشاهد جديد، و لديك الفضول لمعرفة المزيد
[07:08] حول كيفيّة عمل المحّولات والانتباه، فلدّي بعض الموارد لك.
[07:12] إحدى الخيارات هي سلسلة أعددتها عن التعلّم العميق.
[07:16] نقوم بتصوير وشرح تفاصيل الانتباه و كل الخطوات الأخرى
[07:20] في المحوّل.
[07:22] الإضافة، على قناتي الثّانية، نشرت
[07:25] محاضرة ألقيتها حول الموضوع لشركة TNG في ميونخ.
[07:29] أحيانًا، أفضل المحتوى اللّذي أعدّه في سياق حديثًا عاديًّا بدلًا من
[07:33] فيديو منتجًا. و لكنّك حر لتختارما يناسبك أكثر.
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.