深度学习-03-NLP强化训练

🚀 Day03 - NLP强化训练

📖 导读

第三天,强化训练,掌握更多细节。


🔄 核心知识点回顾

1. 分词与向量化

python 复制代码
import jieba
from tensorflow.keras.preprocessing.text import Tokenizer

# 分词
words = jieba.lcut("自然语言处理")

# 向量化
tokenizer = Tokenizer()
tokenizer.fit_on_texts(corpus)
X = tokenizer.texts_to_sequences(texts)

💪 深入RNN系列

2.1 双向RNN

python 复制代码
bi_rnn = nn.RNN(input_size, hidden_size, num_layers=2, bidirectional=True)
output, hidden = bi_rnn(x, h0)
# output.shape: (seq, batch, hidden*2)

2.2 深层RNN

python 复制代码
deep_rnn = nn.RNN(input_size, hidden_size, num_layers=3)
# 层数越多,梯度传播越困难

🎯 注意力机制详解

3.1 注意力公式

复制代码
Attention(Q, K, V) = softmax(QK^T / √d) * V

3.2 矩阵乘法BMM

python 复制代码
A = torch.randn(3, 4, 5)
B = torch.randn(3, 5, 6)
C = torch.bmm(A, B)  # (3, 4, 6)

🔥 Transformer深化

4.1 多头注意力

python 复制代码
class MultiHeadAttention(nn.Module):
    def __init__(self, embed_size, heads):
        super().__init__()
        self.heads = heads
        self.head_dim = embed_size // heads
        
        self.W_q = nn.Linear(embed_size, embed_size)
        self.W_k = nn.Linear(embed_size, embed_size)
        self.W_v = nn.Linear(embed_size, embed_size)
        
    def forward(self, q, k, v, mask=None):
        # 分头 -> 注意力 -> 拼接
        return output

4.2 位置编码

python 复制代码
class PositionalEncoding(nn.Module):
    def __init__(self, embed_size, max_len=5000):
        super().__init__()
        pe = torch.zeros(max_len, embed_size)
        position = torch.arange(0, max_len).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, embed_size, 2) * (-math.log(10000.0) / embed_size))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe)
    
    def forward(self, x):
        return x + self.pe[:x.size(0)]

🚀 实战技巧

5.1 梯度裁剪

python 复制代码
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)

5.2 学习率调度

python 复制代码
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.1)

📝 总结

Day03深入RNN系列和Transformer核心机制。

相关推荐
灵机一物2 小时前
灵机一物AI智能电商小程序(已上线)-Clawith开源多智能体平台:告别单一AI助手,打造专属AI数字员工团队
人工智能·ai员工·智能电商·开源智能体协作平台·clawith
刘大大Leo2 小时前
大模型、Prompt、Skill、MCP、Agent、OpenClaw啥关系?使用AI效率提升10倍的秘诀
人工智能·chatgpt·prompt
六月的可乐2 小时前
AI Agent:从零构建生产级AI智能体脚手架的架构思考
人工智能·ai·架构·langchain·前端框架·node.js·a
茫茫人海一粒沙2 小时前
从 Prompt 到 Harness:AI Agent 工程范式的三次演进
人工智能
LS_learner2 小时前
Node.js 与 npm 的版本对应关系表
人工智能
sin°θ_陈2 小时前
前馈式3D Gaussian Splatting 研究地图(路线二):几何优先的前馈式 3DGS——前馈式 3DGS 如何重新拥抱多视图几何
深度学习·3d·webgl·三维重建·空间计算·3dgs·空间智能
不知名的老吴2 小时前
思考:AI算法领域主流语言是什么?
人工智能
彭祥.2 小时前
基于计算机视觉的运动计数与饮食热量分析系统
人工智能·计算机视觉
超b小哥2 小时前
【超详细】Claude Code Ubuntu平台完整部署指南
linux·人工智能·ubuntu·ai·claude code