ゼロショット分類でラベルを適用する



この記事は、txtai、AI動力セマンティック検索プラットフォームのチュートリアルシリーズの一部です.
本稿では、テキスト分類、ラベリング、トピックモデリングを行うためにどのようにゼロショット分類を使用するかを示します.TXTIは、顔のトランスを抱き締めているゼロショット分類パイプラインのまわりで軽量ラッパーを提供します.この方法は箱から印象的によく働く.ゼロショット分類の驚異的な仕事のための抱擁フェイスチームへの功績!
この記事の例では、テキストのスニペットのラベルのリストを使用して、ベストマッチングラベルを選択します.
tldrstoryは、StreamRight、FastAPIとHugging顔トランスフォーマーを使用しているゼロショット分類システムの完全なスタック実装をします.Medium article describing tldrstoryとゼロショットの分類もあります.

依存関係のインストール

txtaiとすべての依存関係をインストールします.
pip install txtai

ラベルインスタンスの作成


ラベルインスタンスは、ゼロショット分類の主なエントリポイントです.これは、フェーストランスの抱擁のゼロショット分類パイプラインのまわりの軽量ラッパーです.
デフォルトモデルに加えて、追加モデルはHugging Face model hubで発見される.
from txtai.pipeline import Labels

# Create labels model
labels = Labels()

テキストへのラベルの適用


以下の例は、ゼロショット分類器がどのように仲裁テキストに適用されることができるかを示します.ゼロショット分類パイプラインのデフォルトモデルはbart大mnliである.
以下の結果を見てください.それは驚くべきことではない✨ どのようによく実行します.これらは人間でさえ単純ではない.たとえば、傍受されたバスケットボールよりもサッカーでより一般的なものとして選択されました.大きな変圧器モデルに格納された知識の量は、私を感動させ続けます.
data = ["Dodgers lose again, give up 3 HRs in a loss to the Giants",
        "Giants 5 Cardinals 4 final in extra innings",
        "Dodgers drop Game 2 against the Giants, 5-4",
        "Flyers 4 Lightning 1 final. 45 saves for the Lightning.",
        "Slashing, penalty, 2 minute power play coming up",
        "What a stick save!",
        "Leads the NFL in sacks with 9.5",
        "UCF 38 Temple 13",
        "With the 30 yard completion, down to the 10 yard line",
        "Drains the 3pt shot!!, 0:15 remaining in the game",
        "Intercepted! Drives down the court and shoots for the win",
        "Massive dunk!!! they are now up by 15 with 2 minutes to go"]

# List of labels
tags = ["Baseball", "Football", "Hockey", "Basketball"]

print("%-75s %s" % ("Text", "Label"))
print("-" * 100)

for text in data:
    print("%-75s %s" % (text, tags[labels(text, tags)[0][0]]))
Text                                                                        Label
----------------------------------------------------------------------------------------------------
Dodgers lose again, give up 3 HRs in a loss to the Giants                   Baseball
Giants 5 Cardinals 4 final in extra innings                                 Baseball
Dodgers drop Game 2 against the Giants, 5-4                                 Baseball
Flyers 4 Lightning 1 final. 45 saves for the Lightning.                     Hockey
Slashing, penalty, 2 minute power play coming up                            Hockey
What a stick save!                                                          Hockey
Leads the NFL in sacks with 9.5                                             Football
UCF 38 Temple 13                                                            Football
With the 30 yard completion, down to the 10 yard line                       Football
Drains the 3pt shot!!, 0:15 remaining in the game                           Basketball
Intercepted! Drives down the court and shoots for the win                   Basketball
Massive dunk!!! they are now up by 15 with 2 minutes to go                  Basketball

絵文字を試しましょう😀


モデルは絵文字の知識を持っていますか?以下の実行をチェックアウト、それのように確かに見える!ラベルが情報が提示される展望に基づいて適用されることに注意してください.
tags = ["😀", "😡"]

print("%-75s %s" % ("Text", "Label"))
print("-" * 100)

for text in data:
    print("%-75s %s" % (text, tags[labels(text, tags)[0][0]]))
Text                                                                        Label
----------------------------------------------------------------------------------------------------
Dodgers lose again, give up 3 HRs in a loss to the Giants                   😡
Giants 5 Cardinals 4 final in extra innings                                 😀
Dodgers drop Game 2 against the Giants, 5-4                                 😡
Flyers 4 Lightning 1 final. 45 saves for the Lightning.                     😀
Slashing, penalty, 2 minute power play coming up                            😡
What a stick save!                                                          😀
Leads the NFL in sacks with 9.5                                             😀
UCF 38 Temple 13                                                            😀
With the 30 yard completion, down to the 10 yard line                       😀
Drains the 3pt shot!!, 0:15 remaining in the game                           😀
Intercepted! Drives down the court and shoots for the win                   😀
Massive dunk!!! they are now up by 15 with 2 minutes to go                  😀