เรียกใช้การอนุมานกับ 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 การเรียนรู้เชิงลึกที่มีหลายกรอบและมีระดับสูงซึ่งออกแบบมาให้ใช้งานง่าย 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...

ฟังก์ชัน 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 ครั้งที่ 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 จะสุ่มเลือกโทเค็นถัดไปจากโทเค็นของความน่าจะเป็นอันดับต้นๆ สำหรับ K

คุณไม่จำเป็นต้องระบุตัวอย่าง และไม่สนใจข้อมูลโค้ดล่าสุดได้หากไม่เป็นประโยชน์กับกรณีการใช้งานของคุณ ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวอย่างวิดีโอที่พร้อมใช้งานได้ที่วิดีโอตัวอย่าง

ขั้นตอนถัดไป

ในบทแนะนำนี้ คุณได้เรียนรู้วิธีสร้างข้อความโดยใช้ KerasNLP และ Gemma คำแนะนำบางส่วนเกี่ยวกับสิ่งที่ควรเรียนรู้ต่อไปมีดังนี้