Inference Unlimited

مقارنة طرق مختلفة لتوليد المحتوى في نماذج LLM

في الوقت الحالي، أصبحت نماذج اللغة الكبيرة (LLM) عنصرًا لا غنى عنه في العديد من التطبيقات، من الروبوتات الدردشة إلى أنظمة توليد المحتوى. أحد الجوانب الرئيسية لهذه النماذج هو قدرتها على توليد النص. في هذا المقال، سنناقش طرقًا مختلفة لتوليد المحتوى في نماذج LLM، ومقارنة مزاياها وعيوبها وتطبيقاتها.

  1. البحث الجشع (Greedy Search)

البحث الجشع هو واحدة من أبسط طرق توليد النص. يعتمد على اختيار كل حرف (وحدة) التالية بأعلى احتمال، دون النظر إلى السياق.

المزايا:

العيوب:

مثال على الكود:

def greedy_search(model, prompt, max_length):
    output = prompt
    for _ in range(max_length):
        next_token = model.predict_next_token(output)
        output += next_token
    return output
  1. البحث الشعاعي (Beam Search)

البحث الشعاعي هو نسخة محسنة من البحث الجشع، والتي تأخذ في الاعتبار عدة خيارات أفضل في كل خطوة.

المزايا:

العيوب:

مثال على الكود:

def beam_search(model, prompt, max_length, beam_width):
    beams = [{"text": prompt, "score": 0.0}]
    for _ in range(max_length):
        new_beams = []
        for beam in beams:
            for _ in range(beam_width):
                next_token = model.predict_next_token(beam["text"])
                new_text = beam["text"] + next_token
                new_score = beam["score"] + model.get_token_score(next_token)
                new_beams.append({"text": new_text, "score": new_score})
        beams = sorted(new_beams, key=lambda x: x["score"], reverse=True)[:beam_width]
    return beams[0]["text"]
  1. العينة العليا-k (Top-k Sampling)

العينة العليا-k هي طريقة تحدد عشوائيًا وحدة من أعلى k خيارات الأكثر احتمالًا.

المزايا:

العيوب:

مثال على الكود:

def top_k_sampling(model, prompt, max_length, k):
    output = prompt
    for _ in range(max_length):
        probabilities = model.predict_next_token_probabilities(output)
        top_k = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)[:k]
        tokens, scores = zip(*top_k)
        next_token = random.choices(tokens, weights=scores, k=1)[0]
        output += next_token
    return output
  1. العينة العليا-p (Top-p Sampling)

العينة العليا-p، المعروفة أيضًا باسم العينة النووية (Nucleus Sampling)، هي طريقة تحدد عشوائيًا وحدة من مجموعة الوحدات التي يبلغ مجموع احتمالاتها p على الأقل.

المزايا:

العيوب:

مثال على الكود:

def top_p_sampling(model, prompt, max_length, p):
    output = prompt
    for _ in range(max_length):
        probabilities = model.predict_next_token_probabilities(output)
        sorted_probs = sorted(probabilities.items(), key=lambda x: x[1], reverse=True)
        cumulative_probs = []
        current_sum = 0.0
        for token, prob in sorted_probs:
            current_sum += prob
            cumulative_probs.append(current_sum)
            if current_sum >= p:
                break
        tokens = [token for token, _ in sorted_probs[:len(cumulative_probs)]]
        scores = cumulative_probs
        next_token = random.choices(tokens, weights=scores, k=1)[0]
        output += next_token
    return output
  1. فك التشفير المقارن (Contrastive Decoding)

فك التشفير المقارن هو طريقة جديدة تولد عدة نسخ من النص وتختار الأفضل بناءً على المقارنة.

المزايا:

العيوب:

مثال على الكود:

def contrastive_decoding(model, prompt, max_length, k):
    candidates = []
    for _ in range(k):
        candidate = greedy_search(model, prompt, max_length)
        candidates.append(candidate)
    scores = [model.evaluate_text(candidate) for candidate in candidates]
    best_candidate = candidates[scores.index(max(scores))]
    return best_candidate

الخلاصة

اختيار طريقة توليد المحتوى يعتمد على التطبيق المحدد. البحث الجشع والبحث الشعاعي هما أبسط، لكنهما أقل تنوعًا. العينة العليا-k والعينة العليا-p تقدم أكثر تنوعًا، لكنهما قد يولدان نصًا أقل تماسكًا. فك التشفير المقارن هو الأكثر تقدمًا، لكن يتطلب أكثر من حسابات.

في الممارسة العملية، غالبًا ما يتم استخدام مزيج من هذه الطرق لتحقيق أفضل النتائج. من المهم أيضًا تعديل المعاملات وفقًا للنموذج والمهمة المحددة.

Język: AR | Wyświetlenia: 8

← Powrót do listy artykułów