הרצת מסקנות עם Gemma באמצעות Keras

להצגה ב-ai.google.dev הפעלה ב-Google Colab פתיחה ב-Vertex AI הצגת המקור ב-GitHub

במדריך הזה תלמדו איך להשתמש ב-Gemma עם KerasNLP כדי להריץ מסקנות וליצור טקסט. Gemma היא משפחה של מודלים חד-פעמיים קלילים ופתוחים שנוצרו על ידי אותו מחקר וטכנולוגיה ששימשו ליצירת המודלים של Gemini. KerasNLP הוא אוסף של מודלים של עיבוד שפה טבעית (NLP) שמוטמעים ב-Keras וניתן להריץ אותם ב-JAX, ב-PyTorch וב-TensorFlow.

במדריך הזה נשתמש ב-Gemma כדי ליצור תשובות טקסט לכמה הנחיות. אם אתם משתמשים חדשים ב-Keras, כדאי לכם לקרוא את המאמר תחילת העבודה עם Keras לפני שמתחילים, אבל לא חייבים. במהלך העבודה במדריך הזה, תלמדו עוד על Keras.

הגדרה

הגדרת Gemma

כדי להשלים את המדריך, צריך קודם לבצע את הוראות ההגדרה שמפורטות במאמר הגדרת Gemma. בהוראות ההגדרה של Gemma מוסבר איך לבצע את הפעולות הבאות:

  • אפשר לקבל גישה ל-Gemma בכתובת kaggle.com.
  • בוחרים זמן ריצה של Colab עם מספיק משאבים כדי להריץ את המודל Gemma 2B.
  • יצירה והגדרה של שם משתמש ומפתח API ב-Kaggle.

אחרי שמשלימים את ההגדרה של Gemma, עוברים לקטע הבא, שבו מגדירים משתני סביבה לסביבת Colab.

הגדרה של משתני סביבה

הגדרה של משתני סביבה בשביל KAGGLE_USERNAME ו-KAGGLE_KEY.

import os
from google.colab import userdata

# Note: `userdata.get` is a Colab API. If you're not using Colab, set the env
# vars as appropriate for your system.
os.environ["KAGGLE_USERNAME"] = userdata.get('KAGGLE_USERNAME')
os.environ["KAGGLE_KEY"] = userdata.get('KAGGLE_KEY')

יחסי תלות בהתקנות

מתקינים את Keras ו-KerasNLP.

# Install Keras 3 last. See https://keras.io/getting_started/ for more details.
pip install -q -U keras-nlp
pip install -q -U keras>=3

בחירת קצה עורפי

Keras הוא ממשק API ללמידה עמוקה (Deras) ברמה גבוהה של כמה פריימים, שנועד לפשט וקלות שימוש. ב-Keras 3 אפשר לבחור את הקצה העורפי: TensorFlow, JAX או PyTorch. כל השלושה יתאימו למדריך הזה.

import os

os.environ["KERAS_BACKEND"] = "jax"  # Or "tensorflow" or "torch".
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "0.9"

ייבוא חבילות

לייבא את Keras ו-KerasNLP.

import keras
import keras_nlp

יצירת מודל

ב-KerasNLP יש הטמעות של ארכיטקטורות מודלים פופולריות. במדריך הזה תיצרו מודל באמצעות GemmaCausalLM, מודל Gemma מקצה לקצה לבניית מודל שפה סיבתי. מודל שפה סיבתי חוזה את האסימון הבא על סמך אסימונים קודמים.

יוצרים את המודל באמצעות השיטה from_preset:

gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset("gemma_2b_en")
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'model.weights.h5' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'tokenizer.json' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...
Attaching 'assets/tokenizer/vocabulary.spm' from model 'keras/gemma/keras/gemma_2b_en/1' to your Colab notebook...

הפונקציה GemmaCausalLM.from_preset() יוצרת את המודל מארכיטקטורה ומשקולות מוגדרות מראש. בקוד שלמעלה, המחרוזת "gemma_2b_en" מציינת את ההגדרה הקבועה מראש של מודל Gemma 2B עם 2 מיליארד פרמטרים. יש גם מודלים של Gemma עם פרמטרים של 7B, 9B ו-27B. מחרוזות הקוד של דגמים של Gemma מופיעים בכרטיסי המוצר של וריאציית המודל בכתובת kaggle.com.

אפשר להשתמש ב-summary כדי לקבל מידע נוסף על המודל:

gemma_lm.summary()

כמו שאפשר לראות בסיכום, למודל יש 2.5 מיליארד פרמטרים שניתן לאמן.

יצירת טקסט

עכשיו הגיע הזמן ליצור טקסט! למודל יש שיטה מסוג generate שיוצרת טקסט על סמך הנחיה. הארגומנט האופציונלי max_length מציין את האורך המקסימלי של הרצף שנוצר.

אפשר לנסות להשתמש בהנחיה "What is the meaning of life?".

gemma_lm.generate("What is the meaning of life?", max_length=64)
'What is the meaning of life?\n\nThe question is one of the most important questions in the world.\n\nIt’s the question that has been asked by philosophers, theologians, and scientists for centuries.\n\nAnd it’s the question that has been asked by people who are looking for answers to their own lives'

אפשר לנסות להתקשר שוב אל generate עם הנחיה אחרת.

gemma_lm.generate("How does the brain work?", max_length=64)
'How does the brain work?\n\nThe brain is the most complex organ in the human body. It is responsible for controlling all of the body’s functions, including breathing, heart rate, digestion, and more. The brain is also responsible for thinking, feeling, and making decisions.\n\nThe brain is made up'

אם אתם עובדים בקצוות עורפיים של JAX או של TensorFlow, תבחינו שהקריאה השנייה של generate מחזירה כמעט מיד. הסיבה לכך היא שכל קריאה ל-generate עבור גודל אצווה נתון ו-max_length מעובדת באמצעות XLA. ההפעלה הראשונה יקרה, אבל ההפעלות הבאות מהירות הרבה יותר.

אפשר גם לספק הנחיות באצווה באמצעות רשימה כקלט:

gemma_lm.generate(
    ["What is the meaning of life?",
     "How does the brain work?"],
    max_length=64)
['What is the meaning of life?\n\nThe question is one of the most important questions in the world.\n\nIt’s the question that has been asked by philosophers, theologians, and scientists for centuries.\n\nAnd it’s the question that has been asked by people who are looking for answers to their own lives',
 'How does the brain work?\n\nThe brain is the most complex organ in the human body. It is responsible for controlling all of the body’s functions, including breathing, heart rate, digestion, and more. The brain is also responsible for thinking, feeling, and making decisions.\n\nThe brain is made up']

אופציונלי: אפשר לנסות דוגם אחר

אפשר לשלוט באסטרטגיית היצירה של GemmaCausalLM על ידי הגדרת הארגומנט sampler ב-compile(). כברירת מחדל, המערכת תשתמש בדגימה של "greedy".

כניסוי, נסו להגדיר שיטת בידינג מסוג "top_k":

gemma_lm.compile(sampler="top_k")
gemma_lm.generate("What is the meaning of life?", max_length=64)
'What is the meaning of life? That was a question I asked myself as I was driving home from work one night in 2012. I was driving through the city of San Bernardino, and all I could think was, “What the heck am I doing?”\n\nMy life was completely different. I'

אלגוריתם החמדן שמוגדר כברירת מחדל תמיד בוחר את האסימון שיש לו את הסבירות הגבוהה ביותר, אבל האלגוריתם המוביל של K בוחרים באקראי את האסימון הבא מתוך האסימונים בעלי הסבירות הגבוהה ביותר K.

אתם לא חייבים לציין דוגם, ואתם יכולים להתעלם מקטע הקוד האחרון אם הוא לא מועיל בתרחיש לדוגמה שלכם. למידע נוסף על הדוגמנים הזמינים, מומלץ לעיין בקטע דוגמירים.

מה עושים אחר כך

במדריך הזה למדתם איך ליצור טקסט באמצעות KerasNLP ו-Gemma. הנה כמה הצעות לדברים הבאים שאפשר ללמוד: