深度学习十大核心算法:从CNN到扩散模型的实战指南 如果你正在学习深度学习可能会被各种算法名词搞得晕头转向CNN、RNN、Transformer、GAN、扩散模型……每个算法看起来都很重要但学完之后却不知道在实际项目中该怎么选择和使用。更让人困惑的是网上教程要么过于理论化要么只讲某个特定算法缺乏系统性的对比和实践指导。这篇文章将用最直白的方式带你一次性掌握深度学习的十大核心算法。不同于传统的理论讲解我们将从实际应用场景出发通过代码实例和项目案例让你真正理解每个算法的适用场景和实现方法。学完本文你将能够清晰理解CNN、RNN、Transformer、GAN、扩散模型等核心算法的本质差异掌握注意力机制、自注意力、交叉注意力等关键技术的实现原理在实际项目中正确选择和使用合适的深度学习模型避免常见的技术选型误区和实现陷阱1. 这篇文章真正要解决的问题深度学习算法看似复杂但核心问题其实很明确如何根据不同的数据类型和任务需求选择最合适的模型架构。很多初学者会陷入学了很多算法但用起来总是不对的困境根本原因在于没有理解每个算法设计的初衷和适用边界。比如处理图像数据时CNN是不二选择但为什么Transformer在视觉领域也能表现出色RNN擅长处理序列数据但在长序列任务中为什么会被Transformer取代GAN和扩散模型都用于生成任务但它们各自适合什么场景本文将解决这些实际问题算法原理与实现细节的平衡既要理解数学原理又要能写出可运行的代码技术选型的系统性指导提供清晰的决策框架避免用大炮打蚊子实战中的常见陷阱指出每个算法在实际使用中容易踩的坑最新技术的整合涵盖从传统CNN到最新扩散模型的完整技术栈2. 基础概念与核心原理2.1 深度学习算法的发展脉络深度学习算法的发展经历了从专门化到通用化的演变过程。早期算法如CNN、RNN都是针对特定数据类型设计的专用架构而Transformer的出现标志着通用架构的兴起。关键转折点2012年CNN在ImageNet竞赛中突破开启深度学习时代2014年GAN提出生成模型迎来爆发2017年Transformer架构发布奠定大模型基础2020年扩散模型在图像生成领域超越GAN2.2 十大核心算法概览算法类型核心思想主要应用优势局限CNN局部连接权重共享图像处理、计算机视觉平移不变性、参数效率高对序列数据效果差RNN时序信息传递自然语言处理、时间序列能处理变长序列梯度消失/爆炸问题Transformer自注意力机制机器翻译、文本生成并行计算、长距离依赖计算复杂度高GAN生成器-判别器对抗图像生成、数据增强生成质量高训练不稳定扩散模型逐步去噪过程图像生成、音频合成训练稳定、生成多样推理速度慢注意力机制动态权重分配所有序列任务解决信息瓶颈计算开销大LSTM门控机制长序列建模缓解梯度消失参数较多Autoencoder编码-解码结构特征提取、降维无监督学习生成质量有限ResNet残差连接深度网络训练解决梯度消失网络结构复杂BERT双向Transformer语言理解上下文感知需要大量数据3. 环境准备与前置条件在开始实战之前需要准备好开发环境。本文所有代码示例基于Python和PyTorch框架。3.1 基础环境配置# 创建conda环境 conda create -n dl-tutorial python3.9 conda activate dl-tutorial # 安装核心依赖 pip install torch2.0.1 torchvision0.15.2 pip install numpy pandas matplotlib seaborn pip install jupyter notebook # 可选安装GPU版本PyTorch如果有NVIDIA GPU # pip install torch2.0.1cu117 torchvision0.15.2cu117 -f https://download.pytorch.org/whl/torch_stable.html3.2 验证安装# test_environment.py import torch import torchvision import numpy as np print(fPyTorch版本: {torch.__version__}) print(fCUDA是否可用: {torch.cuda.is_available()}) print(fGPU数量: {torch.cuda.device_count()}) if torch.cuda.is_available(): print(f当前GPU: {torch.cuda.get_device_name(0)}) # 测试基本张量操作 x torch.randn(3, 3) print(f随机张量形状: {x.shape})3.3 项目结构规划deep-learning-tutorial/ ├── data/ # 数据集目录 ├── models/ # 模型定义 │ ├── cnn.py │ ├── rnn.py │ ├── transformer.py │ └── gan.py ├── utils/ # 工具函数 ├── notebooks/ # Jupyter笔记本 ├── configs/ # 配置文件 └── requirements.txt4. CNN卷积神经网络实战4.1 CNN核心原理详解CNN的核心思想是通过卷积操作提取图像的局部特征再通过池化层降低维度最后用全连接层进行分类。关键创新在于权重共享和局部连接这大大减少了参数数量。通俗理解想象你要识别一张猫的图片不需要看完整张图就能通过耳朵、胡须等局部特征判断。CNN就是这样工作的——先用小窗口扫描局部特征再组合这些特征进行最终判断。4.2 基础CNN实现# models/cnn_basic.py import torch import torch.nn as nn import torch.nn.functional as F class BasicCNN(nn.Module): def __init__(self, num_classes10): super(BasicCNN, self).__init__() # 卷积层输入通道1输出通道32卷积核3x3 self.conv1 nn.Conv2d(1, 32, kernel_size3, padding1) self.conv2 nn.Conv2d(32, 64, kernel_size3, padding1) # 池化层2x2最大池化 self.pool nn.MaxPool2d(2, 2) # 全连接层 self.fc1 nn.Linear(64 * 7 * 7, 128) # 假设输入是28x28经过两次池化后为7x7 self.fc2 nn.Linear(128, num_classes) # Dropout防止过拟合 self.dropout nn.Dropout(0.5) def forward(self, x): # 第一层卷积激活池化 x self.pool(F.relu(self.conv1(x))) # 28x28 - 14x14 # 第二层卷积激活池化 x self.pool(F.relu(self.conv2(x))) # 14x14 - 7x7 # 展平 x x.view(-1, 64 * 7 * 7) # 全连接层 x F.relu(self.fc1(x)) x self.dropout(x) x self.fc2(x) return x # 测试网络 if __name__ __main__: model BasicCNN(num_classes10) sample_input torch.randn(1, 1, 28, 28) # 批量大小1通道1高28宽28 output model(sample_input) print(f输入形状: {sample_input.shape}) print(f输出形状: {output.shape}) print(f网络参数量: {sum(p.numel() for p in model.parameters())})4.3 CNN在MNIST数据集上的实战# notebooks/cnn_mnist_demo.py import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms from torch.utils.data import DataLoader from models.cnn_basic import BasicCNN # 数据预处理 transform transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) # MNIST数据集的均值和标准差 ]) # 加载数据集 train_dataset datasets.MNIST(./data, trainTrue, downloadTrue, transformtransform) test_dataset datasets.MNIST(./data, trainFalse, transformtransform) train_loader DataLoader(train_dataset, batch_size64, shuffleTrue) test_loader DataLoader(test_dataset, batch_size1000, shuffleFalse) # 训练函数 def train_model(model, train_loader, criterion, optimizer, epochs5): model.train() for epoch in range(epochs): running_loss 0.0 for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() output model(data) loss criterion(output, target) loss.backward() optimizer.step() running_loss loss.item() if batch_idx % 100 0: print(fEpoch: {epoch1}, Batch: {batch_idx}, Loss: {loss.item():.6f}) print(fEpoch {epoch1}完成, 平均损失: {running_loss/len(train_loader):.6f}) # 测试函数 def test_model(model, test_loader): model.eval() correct 0 total 0 with torch.no_grad(): for data, target in test_loader: output model(data) _, predicted torch.max(output.data, 1) total target.size(0) correct (predicted target).sum().item() accuracy 100 * correct / total print(f测试准确率: {accuracy:.2f}%) return accuracy # 主训练流程 if __name__ __main__: model BasicCNN(num_classes10) criterion nn.CrossEntropyLoss() optimizer optim.Adam(model.parameters(), lr0.001) print(开始训练CNN模型...) train_model(model, train_loader, criterion, optimizer, epochs5) print(开始测试...) test_model(model, test_loader)5. RNN循环神经网络深度解析5.1 RNN的核心机制与局限RNN的设计初衷是处理序列数据通过循环连接使网络具有记忆能力。但传统的RNN存在梯度消失问题难以学习长距离依赖。关键洞察RNN就像一个有短期记忆的人能记住最近发生的事情但时间一长就会忘记重要的早期信息。5.2 LSTM与GRU的改进# models/rnn_advanced.py import torch import torch.nn as nn class LSTMModel(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes): super(LSTMModel, self).__init__() self.hidden_size hidden_size self.num_layers num_layers # LSTM层 self.lstm nn.LSTM(input_size, hidden_size, num_layers, batch_firstTrue, dropout0.2) # 全连接层 self.fc nn.Linear(hidden_size, num_classes) def forward(self, x): # 初始化隐藏状态 h0 torch.zeros(self.num_layers, x.size(0), self.hidden_size) c0 torch.zeros(self.num_layers, x.size(0), self.hidden_size) # LSTM前向传播 out, (hn, cn) self.lstm(x, (h0, c0)) # 取最后一个时间步的输出 out self.fc(out[:, -1, :]) return out class GRUModel(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes): super(GRUModel, self).__init__() self.hidden_size hidden_size self.num_layers num_layers # GRU层参数比LSTM少训练更快 self.gru nn.GRU(input_size, hidden_size, num_layers, batch_firstTrue, dropout0.2) self.fc nn.Linear(hidden_size, num_classes) def forward(self, x): h0 torch.zeros(self.num_layers, x.size(0), self.hidden_size) out, hn self.gru(x, h0) out self.fc(out[:, -1, :]) return out # 对比测试 if __name__ __main__: # 模拟序列数据批量大小32序列长度10特征维度50 sample_input torch.randn(32, 10, 50) lstm_model LSTMModel(input_size50, hidden_size128, num_layers2, num_classes10) gru_model GRUModel(input_size50, hidden_size128, num_layers2, num_classes10) lstm_output lstm_model(sample_input) gru_output gru_model(sample_input) print(fLSTM参数量: {sum(p.numel() for p in lstm_model.parameters())}) print(fGRU参数量: {sum(p.numel() for p in gru_model.parameters())}) print(fLSTM输出形状: {lstm_output.shape}) print(fGRU输出形状: {gru_output.shape})5.3 RNN在文本分类中的应用# notebooks/rnn_text_classification.py import torch import torch.nn as nn from torchtext.legacy import data, datasets import spacy # 准备文本数据处理 TEXT data.Field(tokenizespacy, include_lengthsTrue) LABEL data.LabelField(dtypetorch.float) # 加载IMDB电影评论数据集 train_data, test_data datasets.IMDB.splits(TEXT, LABEL) # 构建词汇表 MAX_VOCAB_SIZE 25000 TEXT.build_vocab(train_data, max_sizeMAX_VOCAB_SIZE) LABEL.build_vocab(train_data) # 创建数据迭代器 BATCH_SIZE 64 device torch.device(cuda if torch.cuda.is_available() else cpu) train_iterator, test_iterator data.BucketIterator.splits( (train_data, test_data), batch_sizeBATCH_SIZE, sort_within_batchTrue, devicedevice ) class TextRNN(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, dropout): super().__init__() self.embedding nn.Embedding(vocab_size, embedding_dim) self.rnn nn.LSTM(embedding_dim, hidden_dim, n_layers, dropoutdropout, batch_firstTrue) self.fc nn.Linear(hidden_dim, output_dim) self.dropout nn.Dropout(dropout) def forward(self, text, text_lengths): embedded self.dropout(self.embedding(text)) # 打包序列以提高效率 packed_embedded nn.utils.rnn.pack_padded_sequence( embedded, text_lengths.cpu(), batch_firstTrue, enforce_sortedFalse) packed_output, (hidden, cell) self.rnn(packed_embedded) output, output_lengths nn.utils.rnn.pad_packed_sequence(packed_output, batch_firstTrue) # 取最后一个有效时间步的输出 hidden self.dropout(hidden[-1, :, :]) return self.fc(hidden) # 模型实例化 INPUT_DIM len(TEXT.vocab) EMBEDDING_DIM 100 HIDDEN_DIM 256 OUTPUT_DIM 1 N_LAYERS 2 DROPOUT 0.5 model TextRNN(INPUT_DIM, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM, N_LAYERS, DROPOUT) print(f文本分类模型参数量: {sum(p.numel() for p in model.parameters()):,})6. Transformer架构彻底解析6.1 自注意力机制的本质Transformer的核心创新是自注意力机制它允许模型在处理每个位置时关注输入序列的所有位置从而更好地捕捉长距离依赖。通俗理解传统的RNN像是一个只能看到前面几个词的人而Transformer像是能够同时看到整个句子的人可以在理解每个词时参考句子中的所有其他词。6.2 Transformer编码器实现# models/transformer_core.py import torch import torch.nn as nn import math class MultiHeadAttention(nn.Module): def __init__(self, d_model, num_heads, dropout0.1): super(MultiHeadAttention, self).__init__() assert d_model % num_heads 0 self.d_model d_model self.num_heads num_heads self.d_k d_model // num_heads self.w_q nn.Linear(d_model, d_model) self.w_k nn.Linear(d_model, d_model) self.w_v nn.Linear(d_model, d_model) self.w_o nn.Linear(d_model, d_model) self.dropout nn.Dropout(dropout) self.softmax nn.Softmax(dim-1) def forward(self, query, key, value, maskNone): batch_size query.size(0) # 线性变换并分头 Q self.w_q(query).view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2) K self.w_k(key).view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2) V self.w_v(value).view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2) # 计算注意力分数 scores torch.matmul(Q, K.transpose(-2, -1)) / math.sqrt(self.d_k) if mask is not None: scores scores.masked_fill(mask 0, -1e9) attn_weights self.softmax(scores) attn_weights self.dropout(attn_weights) # 应用注意力权重 context torch.matmul(attn_weights, V) context context.transpose(1, 2).contiguous().view( batch_size, -1, self.d_model) output self.w_o(context) return output, attn_weights class TransformerEncoderLayer(nn.Module): def __init__(self, d_model, num_heads, dim_feedforward2048, dropout0.1): super(TransformerEncoderLayer, self).__init__() self.self_attn MultiHeadAttention(d_model, num_heads, dropout) self.linear1 nn.Linear(d_model, dim_feedforward) self.dropout nn.Dropout(dropout) self.linear2 nn.Linear(dim_feedforward, d_model) self.norm1 nn.LayerNorm(d_model) self.norm2 nn.LayerNorm(d_model) self.dropout1 nn.Dropout(dropout) self.dropout2 nn.Dropout(dropout) self.activation nn.ReLU() def forward(self, src, src_maskNone): # 自注意力子层 src2, attn_weights self.self_attn(src, src, src, src_mask) src src self.dropout1(src2) src self.norm1(src) # 前馈神经网络子层 src2 self.linear2(self.dropout(self.activation(self.linear1(src)))) src src self.dropout2(src2) src self.norm2(src) return src, attn_weights class TransformerEncoder(nn.Module): def __init__(self, vocab_size, d_model, num_heads, num_layers, max_seq_length, dropout0.1): super(TransformerEncoder, self).__init__() self.token_embedding nn.Embedding(vocab_size, d_model) self.position_embedding nn.Embedding(max_seq_length, d_model) self.layers nn.ModuleList([ TransformerEncoderLayer(d_model, num_heads, dropoutdropout) for _ in range(num_layers) ]) self.dropout nn.Dropout(dropout) def forward(self, src, src_maskNone): batch_size, seq_length src.shape # 词嵌入 位置编码 token_embedded self.token_embedding(src) positions torch.arange(0, seq_length).unsqueeze(0).repeat(batch_size, 1).to(src.device) position_embedded self.position_embedding(positions) x self.dropout(token_embedded position_embedded) attention_weights [] for layer in self.layers: x, attn layer(x, src_mask) attention_weights.append(attn) return x, attention_weights # 测试Transformer编码器 if __name__ __main__: vocab_size 10000 d_model 512 num_heads 8 num_layers 6 max_seq_length 128 batch_size 32 encoder TransformerEncoder(vocab_size, d_model, num_heads, num_layers, max_seq_length) # 模拟输入批量大小32序列长度128 sample_input torch.randint(0, vocab_size, (batch_size, max_seq_length)) output, attention_weights encoder(sample_input) print(f输入形状: {sample_input.shape}) print(f输出形状: {output.shape}) print(f注意力权重数量: {len(attention_weights)}) print(f每个注意力权重形状: {attention_weights[0].shape})7. GAN生成对抗网络实战7.1 GAN的基本原理与训练动态GAN由生成器(Generator)和判别器(Discriminator)组成两者通过对抗训练共同进步。生成器试图生成逼真的假数据判别器则努力区分真实数据和生成数据。关键挑战GAN训练非常不稳定容易出现模式崩溃生成器只生成少数几种样本或训练发散。7.2 DCGAN实现示例# models/gan_dcgan.py import torch import torch.nn as nn class Generator(nn.Module): def __init__(self, latent_dim, img_channels, feature_map_size64): super(Generator, self).__init__() self.main nn.Sequential( # 输入: latent_dim维噪声 nn.ConvTranspose2d(latent_dim, feature_map_size * 8, 4, 1, 0, biasFalse), nn.BatchNorm2d(feature_map_size * 8), nn.ReLU(True), # 状态: (feature_map_size*8) x 4 x 4 nn.ConvTranspose2d(feature_map_size * 8, feature_map_size * 4, 4, 2, 1, biasFalse), nn.BatchNorm2d(feature_map_size * 4), nn.ReLU(True), # 状态: (feature_map_size*4) x 8 x 8 nn.ConvTranspose2d(feature_map_size * 4, feature_map_size * 2, 4, 2, 1, biasFalse), nn.BatchNorm2d(feature_map_size * 2), nn.ReLU(True), # 状态: (feature_map_size*2) x 16 x 16 nn.ConvTranspose2d(feature_map_size * 2, feature_map_size, 4, 2, 1, biasFalse), nn.BatchNorm2d(feature_map_size), nn.ReLU(True), # 状态: (feature_map_size) x 32 x 32 nn.ConvTranspose2d(feature_map_size, img_channels, 4, 2, 1, biasFalse), nn.Tanh() # 输出: (img_channels) x 64 x 64 ) def forward(self, input): return self.main(input) class Discriminator(nn.Module): def __init__(self, img_channels, feature_map_size64): super(Discriminator, self).__init__() self.main nn.Sequential( # 输入: (img_channels) x 64 x 64 nn.Conv2d(img_channels, feature_map_size, 4, 2, 1, biasFalse), nn.LeakyReLU(0.2, inplaceTrue), # 状态: (feature_map_size) x 32 x 32 nn.Conv2d(feature_map_size, feature_map_size * 2, 4, 2, 1, biasFalse), nn.BatchNorm2d(feature_map_size * 2), nn.LeakyReLU(0.2, inplaceTrue), # 状态: (feature_map_size*2) x 16 x 16 nn.Conv2d(feature_map_size * 2, feature_map_size * 4, 4, 2, 1, biasFalse), nn.BatchNorm2d(feature_map_size * 4), nn.LeakyReLU(0.2, inplaceTrue), # 状态: (feature_map_size*4) x 8 x 8 nn.Conv2d(feature_map_size * 4, feature_map_size * 8, 4, 2, 1, biasFalse), nn.BatchNorm2d(feature_map_size * 8), nn.LeakyReLU(0.2, inplaceTrue), # 状态: (feature_map_size*8) x 4 x 4 nn.Conv2d(feature_map_size * 8, 1, 4, 1, 0, biasFalse), nn.Sigmoid() ) def forward(self, input): return self.main(input).view(-1, 1).squeeze(1) # GAN训练函数 def train_gan(generator, discriminator, dataloader, num_epochs50): criterion nn.BCELoss() d_optimizer torch.optim.Adam(discriminator.parameters(), lr0.0002, betas(0.5, 0.999)) g_optimizer torch.optim.Adam(generator.parameters(), lr0.0002, betas(0.5, 0.999)) device torch.device(cuda if torch.cuda.is_available() else cpu) generator.to(device) discriminator.to(device) for epoch in range(num_epochs): for i, (real_imgs, _) in enumerate(dataloader): batch_size real_imgs.size(0) real_imgs real_imgs.to(device) # 真实标签和假标签 real_labels torch.ones(batch_size).to(device) fake_labels torch.zeros(batch_size).to(device) # 训练判别器 d_optimizer.zero_grad() # 真实图像的损失 real_output discriminator(real_imgs) d_loss_real criterion(real_output, real_labels) # 生成假图像 z torch.randn(batch_size, LATENT_DIM, 1, 1).to(device) fake_imgs generator(z) # 假图像的损失 fake_output discriminator(fake_imgs.detach()) d_loss_fake criterion(fake_output, fake_labels) # 判别器总损失 d_loss d_loss_real d_loss_fake d_loss.backward() d_optimizer.step() # 训练生成器 g_optimizer.zero_grad() fake_output discriminator(fake_imgs) g_loss criterion(fake_output, real_labels) # 希望判别器将假图判为真 g_loss.backward() g_optimizer.step() if i % 100 0: print(fEpoch [{epoch}/{num_epochs}], Step [{i}/{len(dataloader)}], fD_loss: {d_loss.item():.4f}, G_loss: {g_loss.item():.4f}) # 参数设置 LATENT_DIM 100 IMG_CHANNELS 3 if __name__ __main__: generator Generator(LATENT_DIM, IMG_CHANNELS) discriminator Discriminator(IMG_CHANNELS) print(f生成器参数量: {sum(p.numel() for p in generator.parameters()):,}) print(f判别器参数量: {sum(p.numel() for p in discriminator.parameters()):,}) # 测试生成器 z torch.randn(1, LATENT_DIM, 1, 1) fake_image generator(z) print(f生成图像形状: {fake_image.shape})8. 扩散模型原理与实现8.1 扩散过程的核心思想扩散模型通过两个过程工作前向过程逐步添加噪声破坏数据反向过程学习如何从噪声中重建原始数据。这种方法的优势在于训练稳定性远高于GAN。直观理解就像一张清晰的图片被不断加入噪点变得模糊前向过程然后模型学习如何一步步去除这些噪点恢复清晰图片反向过程。8.2 简单扩散模型实现# models/diffusion_basic.py import torch import torch.nn as nn import math class SinusoidalPositionEmbeddings(nn.Module): def __init__(self, dim): super().__init__() self.dim dim def forward(self, time): device time.device half_dim self.dim // 2 embeddings math.log(10000) / (half_dim - 1) embeddings torch.exp(torch.arange(half_dim, devicedevice) * -embeddings) embeddings time[:, None] * embeddings[None, :] embeddings torch.cat((embeddings.sin(), embeddings.cos()), dim-1) return embeddings class SimpleDiffusionModel(nn.Module): def __init__(self, image_size28, channels1, time_emb_dim32): super(SimpleDiffusionModel, self).__init__() self.time_embed nn.Sequential( SinusoidalPositionEmbeddings(time_emb_dim), nn.Linear(time_emb_dim, time_emb_dim), nn.ReLU() ) # 简单的UNet-like结构 self.conv1 nn.Conv2d(channels time_emb_dim, 64, 3, padding1) self.conv2 nn.Conv2d(64, 64, 3, padding1) self.conv3 nn.Conv2d(64, 64, 3, padding1) self.conv4 nn.Conv2d(64, channels, 3, padding1) self.activation nn.ReLU() self.norm1 nn.BatchNorm2d(64) self.norm2 nn.BatchNorm2d(64) self.norm3 nn.BatchNorm2d(64) def forward(self, x, t): # x: 噪声图像, t: 时间步 t_embed self.time_embed(t) # 将时间嵌入扩展到图像空间维度 t_embed t_embed.unsqueeze(-1).unsqueeze(-1) t_embed t_embed.repeat(1, 1, x.shape[2], x.shape[3]) # 拼接图像和时间信息 x torch.cat([x, t_embed], dim1) x self.activation(self.norm1(self.conv1(x))) x self.activation(self.norm2(self.conv2(x))) x self.activation(self.norm3(self.conv3(x))) x self.conv4(x) return x class DiffusionProcess: def __init__(self, T1000, beta_start1e-4, beta_end0.02): self.T T # 线性噪声调度 self.betas torch.linspace(beta_start, beta_end, T) self.alphas 1. - self.betas self.alpha_bars torch.cumprod(self.alphas, dim0) def forward_process(self, x0, t): 前向加噪过程 sqrt_alpha_bar torch.sqrt(self.alpha_bars[t]) sqrt_one_minus_alpha_bar torch.sqrt(1 - self.alpha_bars[t]) noise torch.randn_like(x0) xt sqrt_alpha_bar * x0 sqrt_one_minus_alpha_bar * noise return xt, noise def reverse_process(self, model, xt, t): 反向去噪过程 predicted_noise model(xt, t) return predicted_noise # 训练扩散模型 def train_diffusion(model, dataloader, diffusion, optimizer, epochs10): model.train() mse_loss nn.MSELoss() for epoch in range(epochs): total_loss 0 for batch_idx, (x0, _) in enumerate(dataloader): optimizer.zero_grad() # 随机选择时间步 t torch.randint(0, diffusion.T, (x0.size(0),)) # 前向加噪 xt, noise diffusion.forward_process(x0, t) # 预测噪声 predicted_noise model(xt, t) # 计算损失 loss mse_loss(predicted_noise, noise) loss.backward() optimizer.step() total_loss loss.item() if batch_idx