ChatGPT は翻訳や要約、質疑応答まで、入力の仕方を変えるたけでシームレスにユーザーの要求に応えてくれます。
そんなAIを構築するときにお世話になるのが、Hugging Face です。
Hugging Face のサイトにはたくさんの学習済モデルがあり、いろいろなモデルを試してみることができます。モデルの一覧を眺めているだけで夢が膨らみます。
しかし、モデルを選ぶときに、いくつかの注意点があります。その一つが、「何をさせたいか」です。
ChatGPT が万能すぎるせいで、生成AIは翻訳も要約もなんでもできるように感じてしまいます。実際には、それぞれの目的ごとに学習が行われており、目的に沿った文章しか生成できません。
目的には、
Text Classification
Token Classification
Question Answering
Summarization
Text Generation
Fill-Mask
などがあります
これらの分類は、Hugging Face の Models を選ぶと、Tasks タブに表示されています。

文の一部から後の文章を作成(Text Generation)させたいのに、要約(Summarization)用に学習されたモデルを使用すると不思議な文が出力されます。
例えば、GPT2 は「Text Generation」用のモデルです。以下のコードで文章を作成させると、
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained('gpt2')
model = AutoModelForCausalLM.from_pretrained("gpt2", device_map="auto")
text = "What is AI"
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
tokens = model.generate(
**inputs,
max_new_tokens=64,
do_sample=True,
temperature=0.7,
pad_token_id=tokenizer.pad_token_id,
)
output = tokenizer.decode(tokens[0], skip_special_tokens=True)
print(output)
What is AI, exactly? In the last few years, artificial intelligence has been used to create artificial intelligence, such as robots and AI bots that can understand human speech. It is no longer a technical challenge for people to learn through reading, but rather it is an essential tool for human beings to learn new skills.
という出力が得られます。一方で、facebook/bart-large-cnn には「Summarization」のタグがついています。先ほどのコードの、モデルだけ変更して実行すると、
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-cnn')
model = AutoModelForCausalLM.from_pretrained("facebook/bart-large-cnn", device_map="auto")
text = "What is AI"
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
tokens = model.generate(
**inputs,
max_new_tokens=64,
do_sample=True,
temperature=0.7,
pad_token_id=tokenizer.pad_token_id,
)
output = tokenizer.decode(tokens[0], skip_special_tokens=True)
print(output)
What is AIace over over- over only only only over- a over over over” only only Only only only the Books one Bone hadke Bare the do one a these had Bare Bare Bare one one Bare one a one one one Ma Bare these the Lime La Physical eats propelled yardcoolgap————-freegood Vaeters104 Cube
…混乱しています。
生成AIに出来ることを理解することが必要です。

コメント