1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DeepSeek-R1 を LLaMA-Factory でファインチューニングする完全ガイド

Posted at

DeepSeek-R1 を LLaMA-Factory でファインチューニングする完全ガイド

image.png

DeepSeek-R1 をファインチューニングすると、その応答をカスタマイズでき、特定のタスクにより適したモデルにすることができます。本ガイドでは、強力なファインチューニングツールである LLaMA-Factory を使用して DeepSeek-R1 を調整する方法を説明します。LLaMA-Factory は LoRA, QLoRA, SFT などのファインチューニングをサポートしています。


🔹 なぜ DeepSeek-R1 をファインチューニングするのか?

以下のような場合にファインチューニングが必要です:

  • 専門的な知識 を必要とする場合(例:金融、医療、カスタマーサポート)
  • モデルを 組織のトーンや文章スタイル に適合させたい場合
  • 特定のタスク において高いパフォーマンスが求められる場合(例:コード生成、翻訳、チャットボットの応答)

🔹 ステップ 1: LLaMA-Factory と依存関係のインストール

ファインチューニングを始める前に、CUDA 対応の NVIDIA GPU(推奨:VRAM 24GB 以上)が搭載されたシステムを用意してください。

1️⃣ LLaMA-Factory リポジトリをクローン

まず、GitHub から LLaMA-Factory のリポジトリをダウンロードします:

git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory

2️⃣ Python 仮想環境を作成

仮想環境を設定することで、依存関係の競合を防ぎます:

python3 -m venv venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate  # Windows

3️⃣ 必要なパッケージをインストール

LLaMA-Factory ディレクトリ内で以下のコマンドを実行してください:

pip install --upgrade pip
pip install -r requirements.txt

🔹 ステップ 2: ファインチューニング用のデータセットを準備

DeepSeek-R1 のファインチューニングには、SFT(Supervised Fine-Tuning), LoRA(Low-Rank Adaptation), QLoRA などが使用できます。

1️⃣ JSONL 形式のデータセットを作成

データセットは以下のような構造にします:

{"instruction": "今日の天気を教えて", "input": "", "output": "今日は晴れです!"}
{"instruction": "プログラミングについて説明してください", "input": "", "output": "プログラミングとは、コンピューターに指示を与えることです。"}
{"instruction": "好きな食べ物は?", "input": "", "output": "私は寿司が好きです!"}

このファイルを data/custom_train.jsonl として保存:

mkdir -p data
nano data/custom_train.jsonl

🔹 ステップ 3: DeepSeek-R1 をファインチューニング

データセットが準備できたら、LoRA を使って DeepSeek-R1 をファインチューニングしましょう。

1️⃣ ファインチューニングスクリプトを実行

以下のコマンドでトレーニングを開始:

CUDA_VISIBLE_DEVICES=0 python src/train.py \
    --model_name_or_path deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B \
    --dataset data/custom_train.jsonl \
    --template deepseek \
    --cutoff_len 512 \
    --output_dir output \
    --num_train_epochs 3 \
    --per_device_train_batch_size 4 \
    --gradient_accumulation_steps 4 \
    --learning_rate 2e-4 \
    --logging_steps 10 \
    --save_steps 100 \
    --save_total_limit 2 \
    --bf16 True \
    --do_train \
    --finetuning_type lora \
    --lora_rank 8 \
    --lora_alpha 32 \
    --evaluation_strategy "no" \
    --cache_dir /mnt/data/huggingface_cache \
    --overwrite_output_dir

🔹 ステップ 4: ファインチューニングしたモデルをテスト

ファインチューニング完了後、以下のコマンドでモデルをテスト:

python src/test_model.py --model_dir output --test_prompt "おすすめの本を教えて"

モデルが適切にファインチューニングされていれば、データセットに基づいた応答が返ってきます。


🔹 ステップ 5: ファインチューニングしたモデルをデプロイ

Ollama でファインチューニング済みモデルを使用する場合:

ollama create deepseek-custom output

その後、以下のコマンドで実行:

ollama run deepseek-custom

これでファインチューニング済みのモデルが動作するようになります! 🎉


1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?