見出し画像

小型インストラクションモデル「Gemma 2B」をColabで動かす

google/gemma-2b-it

20億パラメータと、軽量型の「Gemma 2B」のインストラクションモデルを試しました。

小型LLMなので、今回はGoogle ColabT4 GPUで動かしてみます。

参考にさせて頂いています。↓

Transformersインストール

事前学習済みの深層学習モデルを利用するためのフレームワーク「Transformers 」ライブラリと、高速化計算用のPythonライブラリ「Accelerate」をインストール。

#Transformersインストール。「-U」オプションで最新版
#Accelerateインストール
!pip install -U transformers
!pip install accelerate

HuggingFaceにログイン

トークンを取得して、HuggingFaceにログイン。

# Hugging Faceにログイン
# トークンを使用
from huggingface_hub import HfFolder
hf = HfFolder()
hf.save_token("自分のトークン")

別の方法としてはColabのシークレット(左メニューの鍵アイコン)を利用。

# HuggingFaceへトークンでログイン
!huggingface-cli login

gemma-2b-itを指定

テキスト生成のためのパイプラインを構築。

#トークナイザーとモデル
from transformers import AutoTokenizer, pipeline
import torch

model = "google/gemma-2b-it"

tokenizer = AutoTokenizer.from_pretrained(model)
pipeline = pipeline(
    "text-generation",
    model=model,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device="cuda",
)

推論を実行

プロンプト(messages)を設定。

#推論実行
messages = [
        {"role": "user", "content": "What changes in the world will AI bring?."}, # プロンプト
]
prompt = pipeline.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
outputs = pipeline(
    prompt,
    max_new_tokens=512,
    add_special_tokens=True,
    do_sample=True,
    temperature=0.7,
    top_k=50,
    top_p=0.95
)
print(outputs[0]["generated_text"][len(prompt):])

What changes in the world will AI bring?
(AIがもたらす世界の変化を教えてください。)

**Advancements in AI will bring about significant changes in the world across several domains:**

**1. Automation and Efficiency:**
- Automation of tasks and processes, leading to increased efficiency and productivity.
- Robots and AI systems can handle repetitive and mundane tasks, freeing up human workers for more complex and creative jobs.

**2. Enhanced Decision-Making:**
- Data-driven AI can analyze vast amounts of data to identify patterns, predict outcomes, and make more informed decisions.
- This leads to better predictions, risk management, and problem-solving.

**3. Personalized Experiences:**
- AI can tailor products, services, and marketing to individual preferences, improving customer experiences.
- Personalized recommendations, virtual assistants, and chatbots provide tailored information and support.

**4. Healthcare Advancement:**
- AI-powered medical devices can diagnose diseases early, improve patient outcomes, and facilitate remote monitoring.
- AI-driven research and drug discovery accelerate medical advancements.

**5. Education Revolution:**
- AI-powered personalized learning platforms provide customized educational experiences.
- Chatbots and virtual assistants offer support and guidance outside of traditional classroom settings.

**6. Transportation and Logistics:**
- Autonomous vehicles and drone delivery systems improve safety and efficiency in transportation.
- AI-powered logistics systems optimize supply chains and logistics processes.

**7. Cybersecurity and Privacy:**
- AI can detect and prevent cyber threats, monitor network activity, and improve cybersecurity measures.
- Enhanced data privacy and protection are crucial considerations in AI development.

**8. Climate Change Mitigation:**
- AI can assist in renewable energy development, resource management, and carbon emissions reduction.
- Early detection and prediction of natural disasters can save lives and property.

**9. Social Good and Inclusion:**
- AI can combat social isolation, provide language translation, and promote inclusion for people with disabilities.
- AI can also be used to address issues such as poverty, inequality, and discrimination.

**10. New Industries and Jobs:**
- AI creates new industries and job opportunities in areas such as AI development, data science, and robotics.
- It also disrupts existing industries, leading to job automation and reskilling requirements.

**Challenges to Consider:**

- Ethical considerations and biases in AI systems.
- Job displacement and economic inequality due to automation.
- Privacy and security risks associated with AI.
- Ensuring access to AI benefits for all communities.

(**AIの進歩は、いくつかの領域にわたって世界に大きな変化をもたらすだろう。

**1. 自動化と効率化:** - 作業やプロセスの自動化により、効率性と生産性が向上する。- ロボットやAIシステムは反復的で平凡な仕事を処理し、人間の労働者をより複雑で創造的な仕事に解放する。

**2. 意思決定の強化:*** - データ駆動型AIは、膨大な量のデータを分析してパターンを特定し、結果を予測し、より多くの情報に基づいた意思決定を行うことができる。- これは、より良い予測、リスク管理、問題解決につながります。

**3. パーソナライズされた体験:** - AIは、製品、サービス、マーケティングを個人の嗜好に合わせて調整し、顧客体験を向上させることができます。- パーソナライズされたレコメンデーション、バーチャル・アシスタント、チャットボットは、個人に合わせた情報やサポートを提供します。

**4. ヘルスケアの進歩:** - AIを搭載した医療機器は、病気を早期に診断し、患者の予後を改善し、遠隔監視を容易にする。- AI主導の研究と創薬は、医療の進歩を加速する。

**5. 教育革命:** - AIを活用したパーソナライズされた学習プラットフォームは、カスタマイズされた教育体験を提供する。- チャットボットやバーチャル・アシスタントは、従来の教室外でのサポートやガイダンスを提供する。

**6. 輸送とロジスティクス:**- 自律走行車とドローン配送システムは、輸送の安全性と効率を向上させる。- AIを活用した物流システムは、サプライチェーンと物流プロセスを最適化する。

**7. サイバーセキュリティとプライバシー:** - AIはサイバー脅威を検知・防止し、ネットワーク活動を監視し、サイバーセキュリティ対策を改善することができる。- データのプライバシーと保護の強化は、AI開発において極めて重要な検討事項である。

**8. 気候変動の緩和:** - AIは再生可能エネルギー開発、資源管理、二酸化炭素排出削減を支援できる。- 自然災害の早期発見と予測は、人命と財産を救うことができる。

**9. 社会貢献とインクルージョン:*** - AI は社会的孤立と闘い、言語翻訳を提供し、障害を持つ人々のインクルージョンを促進することができる。- AIはまた、貧困、不平等、差別などの問題に対処するためにも利用できる。

**10. 新たな産業と雇用:** - AIは、AI開発、データサイエンス、ロボット工学などの分野で新たな産業と雇用機会を創出する。- また、既存の産業を破壊し、仕事の自動化や再教育の必要性につながる。

**考慮すべき課題:**

- AIシステムにおける倫理的配慮と偏見。
- 自動化による雇用の移動と経済的不平等。
- AIに関連するプライバシーとセキュリティのリスク。
- すべてのコミュニティがAIの恩恵を受けられるようにすること。)
(翻訳:DeepL)

この記事が気に入ったらサポートをしてみませんか?