Keras का इस्तेमाल करके Gemma की मदद से अनुमान लगाना

ai.google.dev पर देखें Google Colab में चलाएं Vertex AI में खोलें GitHub पर सोर्स देखें

इस ट्यूटोरियल में, अनुमान लगाने और टेक्स्ट जनरेट करने के लिए, KerasNLP के साथ Gemma को इस्तेमाल करने का तरीका बताया गया है. Gemma एक लाइटवेट और बेहतरीन ओपन मॉडल है. इसे Gemini मॉडल में इस्तेमाल की गई रिसर्च और टेक्नोलॉजी का इस्तेमाल करके बनाया गया है. KerasNLP, नैचुरल लैंग्वेज प्रोसेसिंग (एनएलपी) मॉडल का एक कलेक्शन है. इसे Keras में लागू किया गया है. इसे JAX, PyTorch, और TensorFlow पर चलाया जा सकता है.

इस ट्यूटोरियल में, कई प्रॉम्प्ट के लिए टेक्स्ट रिस्पॉन्स जनरेट करने के लिए Gemma का इस्तेमाल किया जाएगा. अगर आपने Keras का इस्तेमाल पहले कभी नहीं किया है, तो शुरू करने से पहले, Keras के साथ काम शुरू करना लेख पढ़ें. हालांकि, आपको ऐसा करने की ज़रूरत नहीं है. इस ट्यूटोरियल की मदद से, आपको Keras के बारे में ज़्यादा जानकारी मिलेगी.

सेटअप

Gemma का सेटअप

इस ट्यूटोरियल को पूरा करने के लिए, आपको सबसे पहले Gemma सेटअप में दिए गए सेटअप के निर्देशों को पूरा करना होगा. Gemma के सेटअप से जुड़े निर्देशों में बताया गया है कि ये काम कैसे किए जा सकते हैं:

  • kaggle.com पर Gemma का ऐक्सेस पाएँ.
  • Gemma 2B मॉडल को चलाने के लिए, ज़रूरत के मुताबिक संसाधनों वाला Colab रनटाइम चुनें.
  • 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 का इस्तेमाल करके मॉडल बनाया जाएगा. यह मॉडल, भाषा के सामान्य मॉडल के लिए शुरू से आखिर तक एक जेमा मॉडल है. कॉज़ल लैंग्वेज मॉडल, पिछले टोकन के आधार पर अगले टोकन का अनुमान लगाता है.

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" स्ट्रिंग में 2 अरब पैरामीटर के साथ Gemma 2B मॉडल को पहले से सेट किया गया है. 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 कॉल तुरंत, लौट आया है. इसकी वजह यह है कि किसी दिए गए बैच साइज़ और max_length के लिए, generate को किया गया हर कॉल, 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']

ज़रूरी नहीं: कोई दूसरा नमूना आज़माएं

आप compile() पर sampler आर्ग्युमेंट सेट करके, GemmaCausalLM के लिए जनरेट करने की रणनीति को कंट्रोल कर सकते हैं. डिफ़ॉल्ट रूप से, "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 का इस्तेमाल करके टेक्स्ट जनरेट करने का तरीका सीखा. आगे क्या करना है, इसके लिए यहां कुछ सुझाव दिए गए हैं: