تنفيذ الاستنتاجات باستخدام Gemma باستخدام Keras

العرض على ai.google.dev التنفيذ في Google Colab الفتح في Vertex AI عرض المصدر على GitHub

يشرح لك هذا البرنامج التعليمي كيفية استخدام Gemma مع KerasNLP لتنفيذ الاستنتاجات وإنشاء النصوص. "جيما" هي مجموعة من النماذج المتطوّرة والخفيفة المتاحة للجميع، والتي تم إنشاؤها بناءً على الأبحاث والتكنولوجيا نفسها المستخدمة في إنشاء نماذج Gemini. KerasNLP هي مجموعة من نماذج معالجة اللغات الطبيعية (NLP) التي تم تنفيذها في نظام Keras والقابلة للتشغيل على JAX وPyTorch وTensorFlow.

في هذا البرنامج التعليمي، ستستخدم Gemma لإنشاء ردود نصية على العديد من المطالبات. إذا كنت مستخدمًا جديدًا لـ Keras، ننصحك بقراءة بدء استخدام Keras قبل أن تبدأ، ولكنك لست مضطرًا لذلك. ستتعرف على المزيد حول Keras أثناء العمل من خلال هذا البرنامج التعليمي.

ضبط إعدادات الجهاز

إعداد Gemma

لإكمال هذا الدليل التعليمي، عليك أولاً إكمال تعليمات الإعداد في صفحة إعداد Gemma. توضح لك تعليمات إعداد Gemma كيفية إجراء ما يلي:

  • يمكنك الوصول إلى Gemma على kaggle.com.
  • اختَر بيئة تشغيل Colab مع موارد كافية لتشغيل نموذج Gemma 2B.
  • إنشاء وتكوين اسم مستخدم ومفتاح واجهة برمجة تطبيقات 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 هي واجهة برمجة تطبيقات عالية المستوى ومتعددة الإطارات للتعلُّم المعمّق تم تصميمها لتبسيط عملية استخدامها وتسهيل استخدامها. يتيح لك 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 الذي يتضمّن مليارَي مَعلمة. وتتوفّر أيضًا نماذج 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 بشكل عشوائي الرمز المميز التالي من الرموز المميزة لاحتمالية أعلى التصنيف.

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

الخطوات التالية

تعلمت في هذا البرنامج التعليمي كيفية إنشاء النص باستخدام KerasNLP وGemma. إليك بعض الاقتراحات لما يجب تعلمه بعد ذلك: