Skip to content

ClasCat® AI Research

クラスキャット – 生成 AI, AI エージェント, MCP

Menu
  • ホーム
    • ClassCat® AI Research ホーム
    • クラスキャット・ホーム
  • OpenAI API
    • OpenAI Python ライブラリ 1.x : 概要
    • OpenAI ブログ
      • GPT の紹介
      • GPT ストアの紹介
      • ChatGPT Team の紹介
    • OpenAI platform 1.x
      • Get Started : イントロダクション
      • Get Started : クイックスタート (Python)
      • Get Started : クイックスタート (Node.js)
      • Get Started : モデル
      • 機能 : 埋め込み
      • 機能 : 埋め込み (ユースケース)
      • ChatGPT : アクション – イントロダクション
      • ChatGPT : アクション – Getting started
      • ChatGPT : アクション – アクション認証
    • OpenAI ヘルプ : ChatGPT
      • ChatGPTとは何ですか?
      • ChatGPT は真実を語っていますか?
      • GPT の作成
      • GPT FAQ
      • GPT vs アシスタント
      • GPT ビルダー
    • OpenAI ヘルプ : ChatGPT > メモリ
      • FAQ
    • OpenAI ヘルプ : GPT ストア
      • 貴方の GPT をフィーチャーする
    • OpenAI Python ライブラリ 0.27 : 概要
    • OpenAI platform
      • Get Started : イントロダクション
      • Get Started : クイックスタート
      • Get Started : モデル
      • ガイド : GPT モデル
      • ガイド : 画像生成 (DALL·E)
      • ガイド : GPT-3.5 Turbo 対応 微調整
      • ガイド : 微調整 1.イントロダクション
      • ガイド : 微調整 2. データセットの準備 / ケーススタディ
      • ガイド : 埋め込み
      • ガイド : 音声テキスト変換
      • ガイド : モデレーション
      • ChatGPT プラグイン : イントロダクション
    • OpenAI Cookbook
      • 概要
      • API 使用方法 : レート制限の操作
      • API 使用方法 : tiktoken でトークンを数える方法
      • GPT : ChatGPT モデルへの入力をフォーマットする方法
      • GPT : 補完をストリームする方法
      • GPT : 大規模言語モデルを扱う方法
      • 埋め込み : 埋め込みの取得
      • GPT-3 の微調整 : 分類サンプルの微調整
      • DALL-E : DALL·E で 画像を生成して編集する方法
      • DALL·E と Segment Anything で動的マスクを作成する方法
      • Whisper プロンプティング・ガイド
  • Gemini API
    • Tutorials : クイックスタート with Python (1) テキスト-to-テキスト生成
    • (2) マルチモーダル入力 / 日本語チャット
    • (3) 埋め込みの使用
    • (4) 高度なユースケース
    • クイックスタート with Node.js
    • クイックスタート with Dart or Flutter (1) 日本語動作確認
    • Gemma
      • 概要 (README)
      • Tutorials : サンプリング
      • Tutorials : KerasNLP による Getting Started
  • Keras 3
    • 新しいマルチバックエンド Keras
    • Keras 3 について
    • Getting Started : エンジニアのための Keras 入門
    • Google Colab 上のインストールと Stable Diffusion デモ
    • コンピュータビジョン – ゼロからの画像分類
    • コンピュータビジョン – 単純な MNIST convnet
    • コンピュータビジョン – EfficientNet を使用した微調整による画像分類
    • コンピュータビジョン – Vision Transformer による画像分類
    • コンピュータビジョン – 最新の MLPモデルによる画像分類
    • コンピュータビジョン – コンパクトな畳込み Transformer
    • Keras Core
      • Keras Core 0.1
        • 新しいマルチバックエンド Keras (README)
        • Keras for TensorFlow, JAX, & PyTorch
        • 開発者ガイド : Getting started with Keras Core
        • 開発者ガイド : 関数型 API
        • 開発者ガイド : シーケンシャル・モデル
        • 開発者ガイド : サブクラス化で新しい層とモデルを作成する
        • 開発者ガイド : 独自のコールバックを書く
      • Keras Core 0.1.1 & 0.1.2 : リリースノート
      • 開発者ガイド
      • Code examples
      • Keras Stable Diffusion
        • 概要
        • 基本的な使い方 (テキスト-to-画像 / 画像-to-画像変換)
        • 混合精度のパフォーマンス
        • インペインティングの簡易アプリケーション
        • (参考) KerasCV – Stable Diffusion を使用した高性能画像生成
  • TensorFlow
    • TF 2 : 初級チュートリアル
    • TF 2 : 上級チュートリアル
    • TF 2 : ガイド
    • TF 1 : チュートリアル
    • TF 1 : ガイド
  • その他
    • 🦜️🔗 LangChain ドキュメント / ユースケース
    • Stable Diffusion WebUI
      • Google Colab で Stable Diffusion WebUI 入門
      • HuggingFace モデル / VAE の導入
      • LoRA の利用
    • Diffusion Models / 拡散モデル
  • クラスキャット
    • 会社案内
    • お問合せ
    • Facebook
    • ClassCat® Blog
Menu

Gemma : Tutorials : KerasNLP による Getting Started

Posted on 03/07/2024 by Sales Information

Gemma : Tutorials : KerasNLP による Getting Started (翻訳/解説)

翻訳 : クラスキャット セールスインフォメーション
作成日時 : 03/07/2024

* 本ページは、google の generative-ai-docs レポジトリの以下のドキュメントを翻訳した上で適宜、補足説明したものです :

  • Get started with Gemma using KerasNLP

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

Gemma : Tutorials : KerasNLP による Getting Started

このチュートリアルは KerasNLP を使用して Gemma を始める方法を示します。Gemma は Gemini モデルを作成するために使用されたのと同じ研究やテクノロジーから構築された、軽量で最先端のオープンモデルのファミリーです。KerasNLP は Keras で実装された自然言語処理 (NLP) モデルのコレクションで、JAX, PyTorch と TensorFlow で動作します。

このチュートリアルでは、幾つかのプロンプトに対するテキスト応答を生成するために Gemma を使用します。Keras が初めてであるなら、始める前に Getting started with Keras を読みたいかもしれませんが、必要ありません。このチュートリアルを進めるにつれて keras について更に学習できます。

 

セットアップ

Gemma セットアップ

このチュートリアルを完了するには、最初に Gemma Setup のセットアップ手順を完了する必要があります。Gemma セットアップ手順は以下を行う方法を示します :

  • kaggle.com で Gemma へアクセスする。

  • Gemma 2B モデルを実行するために十分なリソースを持つ Colab ランタイムを選択する。

  • Kaggle ユーザ名と API キーを生成して構成する。

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 と KerasNLP をインポートします。

import keras
import keras_nlp

 

バックエンドの選択

Keras は単純さと使いやすさのために設計された高位、マルチフレームワークの深層学習 API です。Keras 3 は TensorFlow, JAX または PyTorch のバックエンドを選択できます。3 つすべてがこのチュートリアルで動作します。

import os

os.environ["KERAS_BACKEND"] = "jax"  # Or "tensorflow" or "torch".

 

モデルの作成

KerasNLP は多くのポピュラーな モデルアーキテクチャ の実装を提供しています。このチュートリアルでは、causal 言語モデリング用の end-to-end な Gemma モデルである、GemmaCausalLM を使用してモデルを作成します。causal 言語モデルは前のトークン群に基づいて次のトークンを予測します。

from_preset メソッドを使用してモデルを作成します :

gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset("gemma_2b_en")

from_preset はプリセットされたアーキテクチャと重みからモデルをインスタンス化します。上のコードでは、文字列 “gemma_2b_en” はプリセット・アーキテクチャとして 20 億のパラメータを持つ Gemma モデルを指定しています。(70 億パラメータを持つ Gemma モデルもまた利用可能です。大きなモデルを Colab で実行するには、有料プランで利用できるプレミアム GPU へのアクセスが必要です。あるいは、Kaggle や Google Cloud 上 Gemma 7B モデルで分散調整 を実行できます。)

モデルについて詳細情報を取得するために summary を使用します :

gemma_lm.summary()

summary からわかるように、モデルは 25 億の訓練可能なパラメータを持ちます。

 

テキストの生成

Now it’s time to generate some text! モデルはプロンプトに基づいてテキストを生成する 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 バックエンドで実行している場合、2 回目の 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'

デフォルトの greedy アルゴリズムが常に最大確率でトークンを選択する一方で、top-K アルゴリズムは top K 確率のトークン群から次のトークンをランダムに選択します。

You don’t have to specify a sampler, and you can ignore the last code snippet if it’s not helpful to your use case. If you’d like learn more about the available samplers, see Samplers.

 

以上



クラスキャット

最近の投稿

  • LangGraph Platform : 概要
  • LangGraph : Prebuilt エージェント : ユーザインターフェイス
  • LangGraph : Prebuilt エージェント : 配備
  • LangGraph : Prebuilt エージェント : マルチエージェント
  • LangGraph : Prebuilt エージェント : メモリ

タグ

AutoGen (13) ClassCat Press Release (20) ClassCat TF/ONNX Hub (11) DGL 0.5 (14) Eager Execution (7) Edward (17) FLUX.1 (16) Gemini (20) HuggingFace Transformers 4.5 (10) HuggingFace Transformers 4.6 (7) HuggingFace Transformers 4.29 (9) Keras 2 Examples (98) Keras 2 Guide (16) Keras 3 (10) Keras Release Note (17) Kubeflow 1.0 (10) LangChain (45) LangGraph (19) MediaPipe 0.8 (11) Model Context Protocol (16) NNI 1.5 (16) OpenAI Agents SDK (8) OpenAI Cookbook (13) OpenAI platform (10) OpenAI platform 1.x (10) OpenAI ヘルプ (8) TensorFlow 2.0 Advanced Tutorials (33) TensorFlow 2.0 Advanced Tutorials (Alpha) (15) TensorFlow 2.0 Advanced Tutorials (Beta) (16) TensorFlow 2.0 Guide (10) TensorFlow 2.0 Guide (Alpha) (16) TensorFlow 2.0 Guide (Beta) (9) TensorFlow 2.0 Release Note (12) TensorFlow 2.0 Tutorials (20) TensorFlow 2.0 Tutorials (Alpha) (14) TensorFlow 2.0 Tutorials (Beta) (12) TensorFlow 2.4 Guide (24) TensorFlow Deploy (8) TensorFlow Get Started (7) TensorFlow Graphics (7) TensorFlow Probability (9) TensorFlow Programmer's Guide (22) TensorFlow Release Note (18) TensorFlow Tutorials (33) TF-Agents 0.4 (11)
2024年3月
月 火 水 木 金 土 日
 123
45678910
11121314151617
18192021222324
25262728293031
« 2月   4月 »
© 2025 ClasCat® AI Research | Powered by Minimalist Blog WordPress Theme