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

相关推荐
zhbcddxr5 小时前
北京企业GEO防御风控能力测评:投毒监测与偏差修正排行
网络·人工智能·安全
正经人_x5 小时前
学习日记46:LISA: Reasoning Segmentation via Large Language Model
人工智能·学习·语言模型
i晟6 小时前
对齐:让模型学会“做人“
人工智能
小飞猪。。7 小时前
笔记十四:从零开始搞懂 DPO——大模型偏好对齐的“直球”方案
人工智能·深度学习·机器学习
OceanBase数据库官方博客12 小时前
OceanBase DataPilot AIP:Ontology 承载AI能力面的另一条路
人工智能·oceanbase
笨鸟先飞,勤能补拙13 小时前
AI 赋能网络安全:技术全景、成熟度评估与实战案例
人工智能·python·安全·web安全·网络安全·sqlite·github
一次旅行14 小时前
AI 前沿日报 | 2026年07月31日
人工智能
2601_9637491014 小时前
标题:越华环保集团|面向美丽河湖项目的数字化污水治理云边协同采集架构设计
人工智能
沐籽李14 小时前
从溶剂可及表面积SASA理解抗体结构与工程改造
人工智能·药物设计·aidd·sasa
智慧物业老杨14 小时前
物业如何做好预算管理?落地架构逻辑
人工智能·架构