להתחיל לעבוד עם Gemma באמצעות KerasNLP

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

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

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

הגדרה

הגדרת Gemma

כדי להשלים את המדריך הזה, מבצעים קודם את הוראות ההגדרה בקטע Gemma setup (הגדרת 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 ללמידה עמוקה (Deep Learning) ברמה גבוהה ובכמה מסגרות שמיועדות להיות פשוטות וקלות לשימוש. 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...

from_preset יוצר את המודל מארכיטקטורה ומשקולות מוגדרות מראש. בקוד שלמעלה, המחרוזת "gemma_2b_en" מציינת את הארכיטקטורה שהוגדרה מראש: מודל Gemma עם 2 מיליארד פרמטרים.

אפשר להשתמש ב-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. הנה כמה הצעות למידע הבא שכדאי ללמוד: