センテンストランスフォーマー¶
Snowflakeモデルレジストリは、センテンストランスフォーマー(sentence_transformers.SentenceTransformer)を使用するモデルをサポートします。詳細については、センテンストランスフォーマーのドキュメント をご参照ください。
レジストリがターゲットメソッドの署名を把握するには、サンプル入力データ、またはモデルのメソッドの入力スキーマと出力スキーマを定義する署名のいずれかを指定する必要があります。
サンプル入力データの場合は、sample_input_data パラメーターの値としてSnowpark DataFrame を指定します。例えば、sample_input = pd.DataFrame(["This is a sample sentence."], columns=["TEXT"]) などの値を指定できます。
署名パラメーターを使用している場合は、signatures パラメーターの値としてディクショナリを指定します。ディクショナリはモデルの入力メソッドと出力メソッドを定義します。例えば、次のコードは、モデルの encode メソッドの入力スキーマと出力スキーマを定義します。
from snowflake.ml.model.model_signature import ModelSignature, FeatureSpec, DataType
signatures = {
"encode": ModelSignature(
inputs=[FeatureSpec(dtype=DataType.STRING, name='TEXT')],
outputs=[FeatureSpec(dtype=DataType.FLOAT, name='EMBEDDINGS', shape=(-1,))]
)
}
log_model を呼び出す場合は、options ディクショナリで次の追加オプションを使用できます。
オプション |
説明 |
|---|---|
|
モデルオブジェクトで利用可能なメソッドの名前のリスト。センテンストランスフォーマーモデルには、メソッドが存在すると仮定して、デフォルトで次のターゲットメソッドが設定されています。 |
|
CUDA を持つプラットフォームにデプロイするときに使用する GPU ランタイムのバージョン; デフォルトは 11.8 です。手動で |
次の例では、
事前にトレーニング済みのセンテンストランスフォーマーモデルをロードします。
Snowflake ML モデルレジストリにログを記録します。
推論にログモデルを使用します。
注釈
この例では reg は snowflake.ml.registry.Registry のインスタンスです。レジストリオブジェクトの作成については、 Snowflakeモデルレジストリ をご参照ください。
from sentence_transformers import SentenceTransformer
import pandas as pd
# 1. Initialize the model
# This example uses the 'all-MiniLM-L6-v2' model, which is a popular
# and efficient model for generating sentence embeddings.
model = SentenceTransformer('all-MiniLM-L6-v2')
# 2. Prepare sample input data
# Sentence Transformers expect a single column of text data for the 'encode' method.
sentences = ["This is an example sentence", "Each sentence is converted into a vector"]
sample_input = pd.DataFrame(sentences, columns=["TEXT"])
# 3. Log the model
# Provide the model object, a name, and a version.
# Including sample_input_data allows the registry to infer the input/output signatures.
model_ref = reg.log_model(
model=model,
model_name="my_sentence_transformer",
version_name="v1",
sample_input_data=sample_input,
)
# 4. Use the model for inference
# The 'run' method executes the default 'encode' function on the input data.
result_df = model_ref.run(sample_input, function_name="encode")
# The result is a DataFrame where the output column (usually named 'outputs')
# contains the embeddings as arrays of floats.
print(result_df)