KerasNLP ব্যবহার করে Gemma দিয়ে শুরু করুন

ai.google.dev-এ দেখুন Google Colab-এ চালান Vertex AI-তে খুলুন GitHub-এ উৎস দেখুন

এই টিউটোরিয়ালটি আপনাকে দেখায় কিভাবে কেরাসএনএলপি ব্যবহার করে জেমা দিয়ে শুরু করতে হয়। জেমিনি মডেল তৈরি করতে ব্যবহৃত একই গবেষণা এবং প্রযুক্তি থেকে তৈরি হালকা ওজনের, অত্যাধুনিক ওপেন মডেলের একটি পরিবার। KerasNLP হল কেরাসে বাস্তবায়িত প্রাকৃতিক ভাষা প্রক্রিয়াকরণ (NLP) মডেলের একটি সংগ্রহ এবং JAX, PyTorch এবং TensorFlow-এ চালানো যায়।

এই টিউটোরিয়ালে, আপনি কয়েকটি প্রম্পটে পাঠ্য প্রতিক্রিয়া তৈরি করতে জেমা ব্যবহার করবেন। আপনি যদি কেরাসে নতুন হন, তাহলে আপনি শুরু করার আগে কেরাসের সাথে শুরু করা পড়তে চাইতে পারেন, কিন্তু আপনাকে এটি করতে হবে না। আপনি এই টিউটোরিয়ালটির মাধ্যমে কাজ করার সাথে সাথে আপনি কেরাস সম্পর্কে আরও শিখবেন।

সেটআপ

জেমা সেটআপ

এই টিউটোরিয়ালটি সম্পূর্ণ করতে, আপনাকে প্রথমে Gemma সেটআপে সেটআপ নির্দেশাবলী সম্পূর্ণ করতে হবে। জেমা সেটআপ নির্দেশাবলী আপনাকে দেখায় যে কীভাবে নিম্নলিখিতগুলি করতে হবে:

  • kaggle.com-এ Gemma-এ অ্যাক্সেস পান।
  • Gemma 2B মডেল চালানোর জন্য পর্যাপ্ত সম্পদ সহ একটি Colab রানটাইম বেছে নিন।
  • একটি Kaggle ব্যবহারকারীর নাম এবং API কী তৈরি এবং কনফিগার করুন।

আপনি জেমা সেটআপ সম্পূর্ণ করার পরে, পরবর্তী বিভাগে যান, যেখানে আপনি আপনার 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

একটি ব্যাকএন্ড নির্বাচন করুন

কেরাস হল একটি উচ্চ-স্তরের, মাল্টি-ফ্রেমওয়ার্ক ডিপ লার্নিং API যা সরলতা এবং ব্যবহারের সহজতার জন্য ডিজাইন করা হয়েছে। 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"

প্যাকেজ আমদানি করুন

কেরাস এবং কেরাসএনএলপি আমদানি করুন।

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...

from_preset একটি প্রিসেট আর্কিটেকচার এবং ওজন থেকে মডেলটিকে ইনস্ট্যান্টিয়েট করে। উপরের কোডে, "gemma_2b_en" স্ট্রিংটি প্রিসেট আর্কিটেকচারকে নির্দিষ্ট করে: 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 কলটি প্রায় তাৎক্ষণিকভাবে ফিরে আসে। এর কারণ হল একটি প্রদত্ত ব্যাচের আকার এবং 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 ব্যবহার করে পাঠ্য তৈরি করতে হয়। পরবর্তীতে কী শিখতে হবে তার জন্য এখানে কয়েকটি পরামর্শ রয়েছে: