![]() |
![]() |
![]() |
![]() |
生成 AI プロダクトは比較的新しく、その動作は 使用していました。そのため、ML についてより詳しく調べなければならず、 使用されているモデルを確認し、モデルの動作例を確認して、 驚き。
Learning Interpretability Tool(LIT、ウェブサイト、GitHub) は、ML モデルのデバッグと分析を行い、理由と方法を理解するためのプラットフォームです。 確認する必要があります
ここでは、LIT を設定して Google の Gemma モデル: Sequence Salience モジュールを使用してさまざまな アプローチを学びました。
Gemma プロンプトをデバッグするための LIT の設定
ERROR: pip's dependency resolver does not currently take into account all the
packages that are installed. This behaviour is the source of the following
dependency conflicts.
bigframes 0.21.0 requires scikit-learn>=1.2.2, but you have scikit-learn 1.0.2
which is incompatible.
google-colab 1.0.0 requires ipython==7.34.0, but you have ipython 8.14.0
which is incompatible.
これらは無視しても問題ありません。
LIT と Keras NLP をインストールする
このノートブックでは、Gemma の KerasNLP 実装を使用します(
以下で設定します)。keras
の最新バージョン(3.0 以降)が必要です
keras-nlp
(0.12 以降)、lit-nlp
(1.2 以降)、および Kaggle アカウントが必要です。
ベースモデルです。
# Keras is included in Colab runtimes, but needs to be updated to to v3.0+.
# LIT and Keras NLP are not icnldued by default and must be installed.
# Running this cell may require you to restart your session to ensure the newer
# packages are imported correctly.
pip install -q -U "keras >= 3.0, <4.0" "keras-nlp >= 0.14" "lit-nlp >= 1.2"
Kaggle アクセス
KerasNLP は、事前にトレーニングされたモデルの重みを Kaggle に保存します。「
kagglehub
パッケージが使用されます。
認証情報が必要です。必ず使用許諾契約にも同意してください
Gemma にアクセスする。
Kaggle の設定方法の詳細については、最後の付録をご覧ください。 あります。
import kagglehub
kagglehub.login()
LIT の設定
LIT には、プロンプトを構成する関数 make_notebook_widget()
が用意されています。
デバッグツールについて説明します。
LIT には、サンプル プロンプトのデータセットが用意されており、リンク先のチュートリアルに付属しています 後ほど説明します。
さまざまなモデルやウィジェットを使用するようにウィジェットを構成する方法については、以下のコメントをご覧ください。 説明します。
from lit_nlp.examples.prompt_debugging import notebook as lit_pdbnb
# The following function initializes a LIT Notebook Widget. It's configured by
# two required positional arguments:
#
# * `datasets_config`: A list of strings containing the dataset names and
# paths to load from, as "dataset:path", where path can be a URL or a
# local file path. The example below uses a special value,
# `sample_prompts`, to load the example prompts provided in the LIT
# distribution; no other special values are supported.
# * `models_config`: A list of strings containing the model names and paths to
# load from, as "model:path", where path can be a URL, a local file path,
# or the name of a preset for the configured deep learning framework.
#
# LIT supports salience computation for KerasNLP and Hugging Face Transformers
# models running on TensorFlow or PyTorch. Note that all models passed to the
# `models_config` parameter will be loaded using the same framework and runtime.
# You can cofnigre these with the following keywork arguments.
#
# * `dl_framework`: Must be one of "kerasnlp" or "transformers".
# * `dl_runtime`: Must be one of "tensorflow" or "torch".
#
# Changing the `dl_framework` value will affect the authentication method used
# to access Gemma model weights.
lit_widget = lit_pdbnb.make_notebook_widget(
['sample_prompts'],
["gemma_2b_it:gemma_1.1_instruct_2b_en"],
dl_framework="kerasnlp",
dl_runtime="tensorflow",
batch_size=1,
max_examples=5,
precision="bfloat16",
)
これで、Colab のセルで UI をレンダリングできるようになりました。
lit_widget.render()
<IPython.core.display.Javascript object>
シーケンスの顕著性によるプロンプト デバッグ
Gemma などのテキストからテキストへの大規模言語モデル(LLM)は、入力シーケンスを トークン化されたテキストの形式で変換され、新しいトークンを生成して 補完するものです
顕著性メソッドを使用すると、 入力と出力が重要であるという点です。 LIT の Sequence Salience モジュールは、これらのメソッドを拡張して説明するものです。 シーケンスの重要性を複数の粒度(トークンから 生成 AI です。
上のセルで LIT を使用して、Sequence Salience を試すことができます。 独自のモジュールですよりガイド付きの学習体験については、 シーケンス サリエンスを使用したプロンプト デバッグのチュートリアルをご覧ください。 見てみましょう
Sequence Salience の仕組みに関する学術上および技術的な詳細については、 Google のホワイトペーパーをご覧ください。
付録: Kaggle Hub での Gemma へのアクセス
このノートブックでは、このドキュメントの Gemma の KerasNLP 実装を使用します。 KerasNLP は事前にトレーニングされたモデルの重みを Kaggle に保存しますが、Gemma では ライセンス確認応答が必要です。
次の手順では、Kaggle アカウントの設定方法と、
kagglehub
パッケージを使用して Kaggle での認証を行います。
- Kaggle アカウントをお持ちでない場合は、アカウントを作成してください
- https://www.kaggle.com/account/login?phase=startRegisterTab にアクセスします。
- アカウントを設定する登録方法を使用してください。
- Gemma へのアクセス権をリクエストする
- 上記のアカウントで Kaggle にログインしていることを確認してください
- 同意ページ(https://www.kaggle.com/models/google/gemma/license/consent)に移動します。
- [Kaggle アカウントを使用して確認] を選択します。オプション(デフォルトの選択)を選択して [次へ] をクリックします。
- 同意フォームに入力する(上部の姓と名の欄)
- 下部のチェックボックスを使用してポリシーを承認します
- [同意する] をクリックします。アクセスするには、
- モデルページ(https://www.kaggle.com/models/google/gemma)にリダイレクトされます。
- API トークンを作成する
- 上記で作成したアカウントで Kaggle にログインしていることを確認します。
- [Settings] ページ(https://www.kaggle.com/settings)に移動します。
- [API] セクションまで下にスクロール
- [Create New Token] を使用するトークン生成をトリガーするボタン
- 画面上のメニューを使用して、kaggle.json という名前の JSON ファイルを保存します。このファイルは、サービスによってマシンに生成されます。
- JSON ファイルは、Username と Key という 2 つのプロパティを持つオブジェクトです。後で両方のプロパティをサービスで認証する必要があります。
- API トークンの認証情報を使用して、Colab の kagglehub で認証します
- LIT Sequence Saleince Colab(https://colab.sandbox.google.com/github/google/generative-ai-docs/blob/main/site/en/gemma/docs/lit_gemma.ipynb#scrollTo=yKw8gDsh_nVR)に移動します。
- GPU ランタイムへの接続
- Gemma 2B では、無料枠の T4 ランタイムを使用できます。
- Gemma 7B で V100、L4、または A100 GPU を使用するには、前払いの Colab コンピューティング クレジットまたは Colab Pro アカウントが必要です。
kagglehub
コードセルを実行して、ユーザー名とトークンを要求する HTML フォームを表示します。- 前の手順でダウンロードした
kaggle.json
ファイルからusername
フィールドをコピーし、フォームのusername
フィールドに貼り付けます。 - 前の手順でダウンロードした
kaggle.json
ファイルからkey
フィールドをコピーし、フォームのtoken
フィールドに貼り付けます。 - ログインボタンをクリックして、これらの認証情報をランタイムに保存します
接続を解除すると、認証情報が保存されているキャッシュが消去されるため、Colab ランタイムの接続を解除する場合は、毎回最後の手順を繰り返す必要があります。