星辰大海 AI星辰大海 AI
首页
  • ChatGPT
  • Claude
  • Midjourney
  • Stable Diffusion
  • 大语言模型
  • 图像生成模型
  • 语音模型
Demo 示例
开发笔记
GitHub
首页
  • ChatGPT
  • Claude
  • Midjourney
  • Stable Diffusion
  • 大语言模型
  • 图像生成模型
  • 语音模型
Demo 示例
开发笔记
GitHub
  • AI 模型

    • 大语言模型研究笔记
    • 图像生成模型研究笔记
    • 语音模型研究笔记

大语言模型研究笔记

简介

大语言模型(Large Language Model, LLM)是基于 Transformer 架构的深度学习模型,能够理解和生成人类语言。这是我研究大语言模型时整理的笔记。

大语言模型(Large Language Model, LLM)是基于 Transformer 架构的深度学习模型,能够理解和生成人类语言。

主流模型

GPT 系列

GPT-3.5 / GPT-4

  • 开发公司: OpenAI
  • 参数量: GPT-3.5 (175B), GPT-4 (未公开,估计 1T+)
  • 特点: 强大的推理能力,支持多模态
  • 应用: ChatGPT, API 服务

GPT-3.5 Turbo

  • 优化的推理版本
  • 更快的响应速度
  • 更低的成本

Claude 系列

Claude 3

  • 开发公司: Anthropic
  • 版本: Opus, Sonnet, Haiku
  • 特点: 长上下文(100K+ tokens),安全性高
  • 优势: 代码能力、推理能力优秀

LLaMA 系列

LLaMA 2

  • 开发公司: Meta
  • 参数量: 7B, 13B, 70B
  • 特点: 开源,可商用
  • 优势: 可在本地部署

LLaMA 3

  • 最新版本
  • 改进的推理能力
  • 更好的多语言支持

其他开源模型

  • Mistral: 7B 参数,性能优秀
  • Falcon: 40B/180B 参数
  • Vicuna: 基于 LLaMA 的微调版本
  • ChatGLM: 清华大学开发的中文模型

模型架构

Transformer 架构

输入 → Tokenization → Embedding → Transformer Blocks → Output

关键组件

  1. Self-Attention: 捕捉序列中的依赖关系
  2. Feed-Forward Networks: 非线性变换
  3. Layer Normalization: 稳定训练
  4. Positional Encoding: 位置信息

本地部署

使用 Ollama

# 安装 Ollama
curl -fsSL https://ollama.ai/install.sh | sh

# 下载模型
ollama pull llama2
ollama pull mistral

# 运行模型
ollama run llama2

使用 llama.cpp

# 编译
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make

# 下载模型权重
# 转换模型格式
./convert-llama-ggml-to-gguf.py model.ggml model.gguf

# 运行
./llama-cli -m model.gguf -p "你的提示词"

Python API 示例

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# 加载模型
model_name = "meta-llama/Llama-2-7b-chat-hf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float16,
    device_map="auto"
)

# 生成文本
prompt = "解释一下什么是机器学习"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_length=200)
print(tokenizer.decode(outputs[0]))

模型对比

模型参数量开源商用特点
GPT-4~1T❌✅最强性能
Claude 3~100B+❌✅长上下文
LLaMA 27B-70B✅✅开源可商用
Mistral7B✅✅性能优秀
ChatGLM6B✅✅中文优化

微调方法

LoRA (Low-Rank Adaptation)

轻量级微调方法,只训练少量参数:

from peft import LoraConfig, get_peft_model

config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.05,
)

model = get_peft_model(model, config)

Full Fine-tuning

全参数微调:

from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=3,
    per_device_train_batch_size=4,
    learning_rate=2e-5,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
)
trainer.train()

QLoRA

量化 + LoRA,降低显存需求:

from transformers import BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.float16
)

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=quantization_config
)

评估指标

常见指标

  • BLEU: 机器翻译质量
  • ROUGE: 文本摘要质量
  • Perplexity: 语言模型困惑度
  • Human Evaluation: 人工评估

评估框架

from lm_eval import simple_eval

results = simple_eval(
    model="gpt2",
    tasks=["hellaswag", "arc"],
    num_fewshot=0,
)

应用场景

1. 文本生成

  • 内容创作
  • 代码生成
  • 翻译

2. 对话系统

  • 客服机器人
  • 个人助手
  • 教育辅导

3. 信息提取

  • 文档摘要
  • 实体识别
  • 关系抽取

4. 代码辅助

  • 代码补全
  • Bug 修复
  • 代码审查

最佳实践

  1. 选择合适的模型: 根据任务和资源选择
  2. Prompt 工程: 优化提示词提升效果
  3. 上下文管理: 合理管理对话历史
  4. 成本控制: 监控 token 使用量
  5. 安全考虑: 实现内容过滤和审核

相关资源

  • Hugging Face Models
  • Papers with Code
  • LLM 排行榜
在 GitHub 上编辑此页
Next
图像生成模型研究笔记