เริ่มต้นใช้งาน Gemma โดยใช้ KerasNLP

ดูใน 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 การเรียนรู้เชิงลึกแบบหลายเฟรมเวิร์กระดับสูงที่ออกแบบมาให้ใช้งานง่าย Keras 3 ให้คุณเลือกแบ็กเอนด์เป็น TensorFlow, JAX หรือ PyTorch ทั้ง 3 อย่างจะเหมือนกันกับบทแนะนำนี้

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 ครั้งที่ 2 ส่งคืนแทบจะทันที เนื่องจากการเรียกใช้ 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 คำแนะนำเล็กๆ น้อยๆ เกี่ยวกับสิ่งที่ควรเรียนรู้ถัดไปมีดังนี้