Buckets:
| # 学習済みモデルを使う | |
| {#if fw === 'pt'} | |
| <CourseFloatingBanner chapter={4} | |
| classNames="absolute z-10 right-0 top-0" | |
| notebooks={[ | |
| {label: "Google Colab", value: "https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/ja/chapter4/section2_pt.ipynb"}, | |
| {label: "Aws Studio", value: "https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/ja/chapter4/section2_pt.ipynb"}, | |
| ]} /> | |
| {:else} | |
| <CourseFloatingBanner chapter={4} | |
| classNames="absolute z-10 right-0 top-0" | |
| notebooks={[ | |
| {label: "Google Colab", value: "https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/ja/chapter4/section2_tf.ipynb"}, | |
| {label: "Aws Studio", value: "https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/ja/chapter4/section2_tf.ipynb"}, | |
| ]} /> | |
| {/if} | |
| モデルハブは適切なモデルを簡単に選択できるようにし、どのライブラリからでも数行のコードで使用できるようにします。では、実際にこれらのモデルをどのように使用し、どのようにコミュニティに貢献するかを見ていきましょう。 | |
| 例えば、マスクフィルを行えるフランス語のモデルを探しているとします。 | |
| <div class="flex justify-center"> | |
| <img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/camembert.gif" alt="Selecting the Camembert model." width="80%"/> | |
| </div> | |
| 試しに`camembert-base`チェックポイントを選択してみましょう。camembert-base`という識別子があれば、すぐに使い始めることができます。これまでの章で見てきたように、 `pipeline()` 関数を使用してインスタンスを作成することができます: | |
| ```py | |
| from transformers import pipeline | |
| camembert_fill_mask = pipeline("fill-mask", model="camembert-base") | |
| results = camembert_fill_mask("Le camembert est <mask> :)") | |
| ``` | |
| ```python out | |
| [ | |
| {'sequence': 'Le camembert est délicieux :)', 'score': 0.49091005325317383, 'token': 7200, 'token_str': 'délicieux'}, | |
| {'sequence': 'Le camembert est excellent :)', 'score': 0.1055697426199913, 'token': 2183, 'token_str': 'excellent'}, | |
| {'sequence': 'Le camembert est succulent :)', 'score': 0.03453313186764717, 'token': 26202, 'token_str': 'succulent'}, | |
| {'sequence': 'Le camembert est meilleur :)', 'score': 0.0330314114689827, 'token': 528, 'token_str': 'meilleur'}, | |
| {'sequence': 'Le camembert est parfait :)', 'score': 0.03007650189101696, 'token': 1654, 'token_str': 'parfait'} | |
| ] | |
| ``` | |
| ご覧の通り、パイプライン内でのモデルのロードは非常に簡単です。唯一気をつけなければならないのは、選択したチェックポイントが使用するタスクに適しているかということです。例えば、ここでは`camembert-base`というチェックポイントを`fill-mask`というパイプラインでロードしていますが、これは全く問題ありません。しかし、このチェックポイントを`text-classification`パイプラインでロードしたとすると、`camembert-base`の「ヘッド」がこのタスクに適していないため、結果が意味をなさないことになります!適切なチェックポイントを選択するために、ハギングフェイスハブインタフェースにあるタスクセレクタを使用することをお勧めします: | |
| <div class="flex justify-center"> | |
| <img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/tasks.png" alt="The task selector on the web interface." width="80%"/> | |
| </div> | |
| また、モデル・アーキテクチャを直接使用して、チェックポイントをインスタンス化することもできます: | |
| {#if fw === 'pt'} | |
| ```py | |
| from transformers import CamembertTokenizer, CamembertForMaskedLM | |
| tokenizer = CamembertTokenizer.from_pretrained("camembert-base") | |
| model = CamembertForMaskedLM.from_pretrained("camembert-base") | |
| ``` | |
| しかし、代わりに[`Auto*` classes](https://huggingface.co/transformers/model_doc/auto.html?highlight=auto#auto-classes)を使用することをお勧めします。これらは設計上、(モデル)アーキテクチャに依存しないためです。先ほどのコードサンプルでは、CamemBERT アーキテクチャでロード可能なチェックポイントに限定していましたが、 `Auto*`クラスを使用すると、チェックポイントを簡単に切り替えることができます: | |
| ```py | |
| from transformers import AutoTokenizer, AutoModelForMaskedLM | |
| tokenizer = AutoTokenizer.from_pretrained("camembert-base") | |
| model = AutoModelForMaskedLM.from_pretrained("camembert-base") | |
| ``` | |
| {:else} | |
| ```py | |
| from transformers import CamembertTokenizer, TFCamembertForMaskedLM | |
| tokenizer = CamembertTokenizer.from_pretrained("camembert-base") | |
| model = TFCamembertForMaskedLM.from_pretrained("camembert-base") | |
| ``` | |
| しかし、代わりに[`TFAuto*` classes](https://huggingface.co/transformers/model_doc/auto.html?highlight=auto#auto-classes)を使用することをお勧めします。これらは設計上、アーキテクチャに依存しないためです。先ほどのコードサンプルでは、CamemBERT アーキテクチャでロード可能なチェックポイントに限定していましたが、 `TFAuto*`クラスを使用すると、チェックポイントを簡単に切り替えることができます: | |
| ```py | |
| from transformers import AutoTokenizer, TFAutoModelForMaskedLM | |
| tokenizer = AutoTokenizer.from_pretrained("camembert-base") | |
| model = TFAutoModelForMaskedLM.from_pretrained("camembert-base") | |
| ``` | |
| {/if} | |
| > [!TIP] | |
| > 学習済みのモデルを使う場合は、どのように学習したのか、どのデータセットで学習したのか、その限界と偏りを必ず確認すること。これらの情報はすべて、モデルカードに記載されています。 | |
| <EditOnGithub source="https://github.com/huggingface/course/blob/main/chapters/ja/chapter4/2.mdx" /> |
Xet Storage Details
- Size:
- 6.14 kB
- Xet hash:
- a8a4635c70ed8b7763b7355eeaf3757d75328cf19e95645be478329a417e9697
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.