-
Notifications
You must be signed in to change notification settings - Fork 33.7k
[i18n-ar] Translated file : docs/source/ar/perplexity.md into Arabic
#33063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
AhmedAlmaghz
wants to merge
28
commits into
huggingface:main
from
AhmedAlmaghz:Add_docs_source_ar_perplexity.md
Closed
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
46f76e5
Add docs/source/ar/perplexity.md to Add_docs_source_ar_perplexity.md
AhmedAlmaghz 1ca6764
Create _toctree.yml
AhmedAlmaghz 1a94839
Update _toctree.yml
AhmedAlmaghz 41d72ef
Update _toctree.yml - tasks_explained
AhmedAlmaghz 7765d6c
Update _toctree.yml - tokenizer_summary
AhmedAlmaghz 5ccdf41
Update _toctree.yml - model_summary
AhmedAlmaghz 60ccba8
Update docs/source/ar/perplexity.md
AhmedAlmaghz 5827048
Update docs/source/ar/perplexity.md
AhmedAlmaghz aa843c5
Update docs/source/ar/perplexity.md
AhmedAlmaghz ffcc7f2
Update docs/source/ar/perplexity.md
AhmedAlmaghz 1cca100
Update docs/source/ar/perplexity.md
AhmedAlmaghz 65f1df4
Update docs/source/ar/perplexity.md
AhmedAlmaghz 3df9dea
Update docs/source/ar/perplexity.md
AhmedAlmaghz b89495b
Update docs/source/ar/perplexity.md
AhmedAlmaghz f980403
Update docs/source/ar/perplexity.md
AhmedAlmaghz 9b66837
Update docs/source/ar/perplexity.md
AhmedAlmaghz eebcf8d
Update docs/source/ar/perplexity.md
AhmedAlmaghz 28b561b
Update docs/source/ar/perplexity.md
AhmedAlmaghz 95012d6
Update docs/source/ar/perplexity.md
AhmedAlmaghz 1a9b163
Update docs/source/ar/perplexity.md
AhmedAlmaghz db51c35
Update docs/source/ar/perplexity.md
AhmedAlmaghz d61ca1e
Update docs/source/ar/perplexity.md
AhmedAlmaghz 32563f1
Update docs/source/ar/perplexity.md
AhmedAlmaghz 75a5f60
Update _toctree.yml - attention
AhmedAlmaghz 9a99d59
Update _toctree.yml - pad_truncation
AhmedAlmaghz e0f2d6c
Update _toctree.yml - bertology
AhmedAlmaghz acc1351
Update _toctree.yml - perplexity
AhmedAlmaghz 979c196
Merge pull request #28 from AhmedAlmaghz/Update_toctree_yml
AhmedAlmaghz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # حيرة النماذج ذات الطول الثابت | ||
|
|
||
| [[open-in-colab]] | ||
|
|
||
| حيرة (PPL) هي واحدة من أكثر المقاييس شيوعًا لتقييم نماذج اللغة. قبل الغوص في التفاصيل، يجب أن نلاحظ أن المقياس ينطبق تحديدًا على نماذج اللغة الكلاسيكية (يُطلق عليها أحيانًا نماذج اللغة الذاتية الترجعية أو السببية) وهي غير محددة جيدًا لنماذج اللغة المقنعة مثل BERT (راجع [ملخص النماذج](model_summary)). | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| تُعرَّف الحيرة على أنها الأس المطروح من متوسط اللوغاريتم الاحتمالي لمتتالية. إذا كان لدينا تسلسل مميز \\(X = (x_0, x_1, \dots, x_t)\\)، فإن حيرة \\(X\\) هي، | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| $$\text{PPL}(X) = \exp \left\{ {-\frac{1}{t}\sum_i^t \log p_\theta (x_i|x_{<i}) } \right\}$$ | ||
|
|
||
| حيث \\(\log p_\theta (x_i|x_{<i})\\) هو اللوغاريتم الاحتمالي للرمز i المشروط بالرموز السابقة \\(x_{<i}\\) وفقًا لنموذجنا. ومن الناحية البديهية، يمكن اعتبارها تقييمًا لقدرة النموذج على التنبؤ بشكل موحد بين مجموعة من الرموز المحددة في مجموعة من البيانات. ومن المهم أن نلاحظ أن هذا يعني أن إجراء التمييز له تأثير مباشر على حيرة النموذج، والتي يجب أن تؤخذ دائمًا في الاعتبار عند مقارنة النماذج المختلفة. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| هذا يعادل أيضًا أس قيمة الانتروبيا المشتركة بين البيانات وتنبؤات النموذج. لمزيد من البديهيات حول الحيرة وعلاقتها بـ Bits Per Character (BPC) وضغط البيانات، تحقق من هذه [التدوينة الرائعة على The Gradient](https://thegradient.pub/understanding-evaluation-metrics-for-language-models/). | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## حساب PPL مع النماذج ذات الطول الثابت | ||
|
|
||
| إذا لم نكن مقيدين بحجم سياق النموذج، فسنقوم بتقييم حيرة النموذج عن طريق تفكيك تسلسل بشكل ذاتي ترجعي والاشتقاق الشرطي للتسلسل الفرعي السابق بالكامل في كل خطوة، كما هو موضح أدناه. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| <img width="600" alt="Full decomposition of a sequence with unlimited context length" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/ppl_full.gif"/> | ||
|
|
||
| ومع ذلك، عند العمل مع النماذج التقريبية، عادة ما يكون لدينا قيد على عدد الرموز التي يمكن للنموذج معالجتها. على سبيل المثال، تحتوي أكبر نسخة من [GPT-2](model_doc/gpt2) على طول ثابت يبلغ 1024 رمزًا، لذلك لا يمكننا حساب \\(p_\theta(x_t|x_{<t})\\) مباشرة عندما \\(t\\) أكبر من 1024. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| بدلاً من ذلك، يتم عادةً تقسيم التسلسل إلى تسلسلات فرعية تساوي حجم الإدخال الأقصى للنموذج. إذا كان حجم الإدخال الأقصى للنموذج هو \\(k\\)، فإننا نقوم بعد ذلك بتقريب احتمال الرمز \\(x_t\\) عن طريق الاشتقاق الشرطي فقط بالنسبة إلى \\(k-1\\) من الرموز التي تسبقه بدلاً من السياق بأكمله. عند تقييم حيرة النموذج لتسلسل ما، فإن أحد الأساليب المغوية ولكنه دون المستوى الأمثل هو تقسيم التسلسل إلى قطع غير متصلة وإضافة اللوغاريتمات المفككة لكل جزء بشكل مستقل. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| <img width="600" alt="Suboptimal PPL not taking advantage of full available context" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/ppl_chunked.gif"/> | ||
| <img width="600" alt="Suboptimal PPL not taking advantage of full available context" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/ppl_chunked.gif"/> | ||
|
AhmedAlmaghz marked this conversation as resolved.
|
||
|
|
||
| هذا سريع الحساب لأن حيرة كل جزء يمكن حسابها في تمرير واحد، ولكنه يمثل تقريبًا ضعيفًا لحيرة التفكيك الكامل وسيؤدي عادةً إلى PPL أعلى (أسوأ) لأن النموذج سيكون لديه سياق أقل في معظم خطوات التنبؤ. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| بدلاً من ذلك، يجب تقييم حيرة النماذج ذات الطول الثابت باستخدام إستراتيجية النافذة المنزلقة. وينطوي هذا على تحريك نافذة السياق بشكل متكرر بحيث يكون للنموذج سياق أكبر عند إجراء كل تنبؤ. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| <img width="600" alt="Sliding window PPL taking advantage of all available context" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/ppl_sliding.gif"/> | ||
|
|
||
| هذا تقريب أوثق للتفكيك الحقيقي لاحتمالية التسلسل وسيؤدي عادةً إلى نتيجة أفضل. الجانب السلبي هو أنه يتطلب تمريرًا للأمام لكل رمز في مجموعة البيانات. حل وسط عملي جيد هو استخدام نافذة منزلقة ذات خطوة، حيث يتم تحريك السياق بخطوات أكبر بدلاً من الانزلاق بمقدار 1 رمز في كل مرة. يسمح ذلك بإجراء الحساب بشكل أسرع مع إعطاء النموذج سياقًا كبيرًا للتنبؤات في كل خطوة. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## مثال: حساب الحيرة مع GPT-2 في 🤗 Transformers | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| دعونا نوضح هذه العملية مع GPT-2. | ||
|
|
||
| ```python | ||
| from transformers import GPT2LMHeadModel, GPT2TokenizerFast | ||
|
|
||
| device = "cuda" | ||
| model_id = "openai-community/gpt2-large" | ||
| model = GPT2LMHeadModel.from_pretrained(model_id).to(device) | ||
| tokenizer = GPT2TokenizerFast.from_pretrained(model_id) | ||
| ``` | ||
|
|
||
| سنقوم بتحميل مجموعة بيانات WikiText-2 وتقييم الحيرة باستخدام بعض إستراتيجيات النافذة المنزلقة المختلفة. نظرًا لأن هذه المجموعة صغيرة ونقوم فقط بتمرير واحد على المجموعة، فيمكننا ببساطة تحميل المجموعة وترميزها بالكامل في الذاكرة. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```python | ||
| from datasets import load_dataset | ||
|
|
||
| test = load_dataset("wikitext", "wikitext-2-raw-v1", split="test") | ||
| encodings = tokenizer("\n\n".join(test["text"]), return_tensors="pt") | ||
| ``` | ||
|
|
||
| مع 🤗 Transformers، يمكننا ببساطة تمرير `input_ids` كـ `labels` إلى نموذجنا، ويتم إرجاع متوسط اللوغاريتم الاحتمالي السلبي لكل رمز كخسارة. ومع ذلك، مع نهج النافذة المنزلقة لدينا، هناك تداخل في الرموز التي نمررها إلى النموذج في كل تكرار. لا نريد تضمين اللوغاريتم الاحتمالي للرموز التي نتعامل معها كسياق في خسارتنا، لذا يمكننا تعيين هذه الأهداف إلى `-100` بحيث يتم تجاهلها. ما يلي هو مثال على كيفية القيام بذلك مع خطوة من `512`. وهذا يعني أن النموذج سيكون لديه 512 رمزًا على الأقل للسياق عند حساب الاحتمالية الشرطية لأي رمز واحد (شريطة أن تكون هناك 512 رمزًا سابقًا متاحًا للاشتقاق). | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```python | ||
| import torch | ||
| from tqdm import tqdm | ||
|
|
||
| max_length = model.config.n_positions | ||
| stride = 512 | ||
| seq_len = encodings.input_ids.size(1) | ||
|
|
||
| nlls = [] | ||
| prev_end_loc = 0 | ||
| for begin_loc in tqdm(range(0, seq_len, stride)): | ||
| end_loc = min(begin_loc + max_length, seq_len) | ||
| trg_len = end_loc - prev_end_loc # قد تكون مختلفة عن الخطوة في الحلقة الأخيرة | ||
| input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device) | ||
| target_ids = input_ids.clone() | ||
| target_ids[:, :-trg_len] = -100 | ||
|
|
||
| with torch.no_grad(): | ||
| outputs = model(input_ids, labels=target_ids) | ||
|
|
||
| # يتم حساب الخسارة باستخدام CrossEntropyLoss الذي يقوم بالمتوسط على التصنيفات الصحيحة | ||
| # لاحظ أن النموذج يحسب الخسارة على trg_len - 1 من التصنيفات فقط، لأنه يتحول داخليًا إلى اليسار بواسطة 1. | ||
| neg_log_likelihood = outputs.loss | ||
|
|
||
| nlls.append(neg_log_likelihood) | ||
|
|
||
| prev_end_loc = end_loc | ||
| if end_loc == seq_len: | ||
| break | ||
|
|
||
| ppl = torch.exp(torch.stack(nlls).mean()) | ||
| ``` | ||
|
|
||
| إن تشغيل هذا مع طول الخطوة يساوي طول الإدخال الأقصى يعادل إستراتيجية النافذة غير المنزلقة وغير المثلى التي ناقشناها أعلاه. وكلما صغرت الخطوة، زاد السياق الذي سيكون لدى النموذج في إجراء كل تنبؤ، وكلما كانت الحيرة المبلغ عنها أفضل عادةً. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
|
|
||
| عندما نقوم بتشغيل ما سبق باستخدام `stride = 1024`، أي بدون تداخل، تكون نتيجة PPL هي `19.44`، وهو ما يماثل `19.93` المبلغ عنها في ورقة GPT-2. من خلال استخدام `stride = 512` وبالتالي استخدام إستراتيجية النافذة المنزلقة لدينا، ينخفض هذا إلى `16.45`. هذه النتيجة ليست فقط أفضل، ولكنها محسوبة بطريقة أقرب إلى التفكيك الذاتي الترجعي الحقيقي لاحتمالية التسلسل. | ||
|
AhmedAlmaghz marked this conversation as resolved.
Outdated
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.