深度学习-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核心机制。

相关推荐
极客编程玩家5 分钟前
SpringAI RAG 检索增强完整实战案例
人工智能·ai·aigc·ai编程
LaughingZhu13 分钟前
Product Hunt 热榜 2026-12-05:SoduWork、finerd split、Otari by Mozilla AI
java·人工智能·docker·chatgpt·容器
KingOTC40 分钟前
FDE的组织方式:团队怎么搭、仗怎么打
人工智能·fde
欣欣之王来了1 小时前
信息论基础:熵、交叉熵与损失函数的关系
人工智能·opencv·目标检测·计算机视觉·cv·核心技术
陈嘿萌1 小时前
TPAMI 2026 | 多模态自监督只做对齐够吗?MIIB同时学习冗余、独有与协同信息机制
图像处理·人工智能·深度学习·计算机视觉·学术研究与理论基础
Golden小狗种自己的花[哇]1 小时前
LLM/Token/Agent
人工智能·大语言模型·agent
编程见习者1 小时前
外卖员招聘助手
人工智能·python
zhaoshuzhaoshu1 小时前
通话降噪(ENC)技术详解
人工智能
极客先躯1 小时前
智能体开发高级面试每日一题|2026-05-31|什么是 AI 编排(Orchestration)?编排层负责什么?
人工智能·ai·agent·每日一题·高级面试·智能体开发·ai编排
玩转单片机与嵌入式2 小时前
深入浅出TinyML 36:如何验证一个TinyML系统真正具备工程应用价值?
网络·人工智能·算法