深度学习概率建模:生成模型理论
1. 技术分析
1.1 生成模型概述
生成模型学习数据的概率分布:
生成模型类型 显式密度模型: 直接建模P(X) 隐式密度模型: 采样生成 能量模型: 基于能量函数 应用: 图像生成 数据补全 异常检测
1.2 生成模型对比
| 模型 | 类型 | 优势 | 挑战 |
|---|
| GAN | 隐式 | 高质量生成 | 训练不稳定 |
| VAE | 显式 | 稳定训练 | 模糊输出 |
| Flow | 显式 | 精确密度估计 | 设计复杂 |
| Diffusion | 隐式 | 高质量生成 | 慢采样 |
1.3 生成模型评估
评估指标 对数似然: 密度模型 FID: 图像质量 IS: 多样性 Precision/Recall: 生成质量
2. 核心功能实现
2.1 VAE变分自编码器
import numpy as np class VAE: def __init__(self, input_dim, latent_dim=64): self.encoder = Encoder(input_dim, latent_dim) self.decoder = Decoder(latent_dim, input_dim) def encode(self, x): mean, log_var = self.encoder(x) return mean, log_var def reparameterize(self, mean, log_var): std = np.exp(0.5 * log_var) eps = np.random.normal(0, 1, std.shape) return mean + eps * std def decode(self, z): return self.decoder(z) def forward(self, x): mean, log_var = self.encode(x) z = self.reparameterize(mean, log_var) recon = self.decode(z) return recon, mean, log_var def compute_loss(self, x): recon, mean, log_var = self.forward(x) recon_loss = np.mean((recon - x) ** 2) kl_loss = -0.5 * np.mean(1 + log_var - mean ** 2 - np.exp(log_var)) return recon_loss + kl_loss class Encoder: def __init__(self, input_dim, latent_dim): self.fc1 = np.random.randn(input_dim, 256) self.fc_mean = np.random.randn(256, latent_dim) self.fc_var = np.random.randn(256, latent_dim) def forward(self, x): h = np.maximum(0, x @ self.fc1) mean = h @ self.fc_mean log_var = h @ self.fc_var return mean, log_var class Decoder: def __init__(self, latent_dim, output_dim): self.fc1 = np.random.randn(latent_dim, 256) self.fc2 = np.random.randn(256, output_dim) def forward(self, z): h = np.maximum(0, z @ self.fc1) recon = h @ self.fc2 return recon
2.2 GAN生成对抗网络
class GAN: def __init__(self, latent_dim=100, output_dim=784): self.generator = Generator(latent_dim, output_dim) self.discriminator = Discriminator(output_dim) def train(self, real_data, epochs=1000): for epoch in range(epochs): z = np.random.normal(0, 1, (len(real_data), 100)) fake_data = self.generator(z) d_loss = self._train_discriminator(real_data, fake_data) g_loss = self._train_generator(len(real_data)) def _train_discriminator(self, real, fake): real_pred = self.discriminator(real) fake_pred = self.discriminator(fake) real_loss = -np.log(real_pred).mean() fake_loss = -np.log(1 - fake_pred).mean() return real_loss + fake_loss def _train_generator(self, batch_size): z = np.random.normal(0, 1, (batch_size, 100)) fake = self.generator(z) pred = self.discriminator(fake) return -np.log(pred).mean() class Generator: def __init__(self, latent_dim, output_dim): self.fc1 = np.random.randn(latent_dim, 256) self.fc2 = np.random.randn(256, 512) self.fc3 = np.random.randn(512, output_dim) def forward(self, z): h1 = np.maximum(0, z @ self.fc1) h2 = np.maximum(0, h1 @ self.fc2) output = 1 / (1 + np.exp(-h2 @ self.fc3)) return output class Discriminator: def __init__(self, input_dim): self.fc1 = np.random.randn(input_dim, 512) self.fc2 = np.random.randn(512, 256) self.fc3 = np.random.randn(256, 1) def forward(self, x): h1 = np.maximum(0, x @ self.fc1) h2 = np.maximum(0, h1 @ self.fc2) output = 1 / (1 + np.exp(-h2 @ self.fc3)) return output
2.3 扩散模型
class DiffusionModel: def __init__(self, denoiser, timesteps=1000): self.denoiser = denoiser self.timesteps = timesteps self.beta = np.linspace(0.0001, 0.02, timesteps) self.alpha = 1 - self.beta self.alpha_bar = np.cumprod(self.alpha) def forward_diffusion(self, x0, t): noise = np.random.normal(0, 1, x0.shape) mean = np.sqrt(self.alpha_bar[t]) * x0 std = np.sqrt(1 - self.alpha_bar[t]) return mean + std * noise def reverse_diffusion(self, xt, t): noise_pred = self.denoiser(xt, t) alpha_t = self.alpha[t] alpha_bar_t = self.alpha_bar[t] mean = (xt - (1 - alpha_t) / np.sqrt(1 - alpha_bar_t) * noise_pred) / np.sqrt(alpha_t) if t > 0: std = np.sqrt(self.beta[t]) mean += std * np.random.normal(0, 1, xt.shape) return mean def sample(self, shape): x = np.random.normal(0, 1, shape) for t in reversed(range(self.timesteps)): x = self.reverse_diffusion(x, t) return x class DenoisingNetwork: def __init__(self, input_dim): self.layers = [ np.random.randn(input_dim, 512), np.random.randn(512, 512), np.random.randn(512, input_dim) ] def forward(self, x, t): h = x for i, layer in enumerate(self.layers[:-1]): h = np.maximum(0, h @ layer) h = h @ self.layers[-1] return h
3. 性能对比
3.1 生成模型对比
| 模型 | 生成质量 | 训练稳定性 | 采样速度 |
|---|
| GAN | 高 | 低 | 快 |
| VAE | 中 | 高 | 快 |
| Flow | 中 | 高 | 快 |
| Diffusion | 很高 | 高 | 慢 |
3.2 图像生成评估
| 模型 | FID | IS | Precision | Recall |
|---|
| GAN | 15 | 25 | 0.7 | 0.6 |
| VAE | 30 | 20 | 0.5 | 0.5 |
| Diffusion | 10 | 30 | 0.8 | 0.8 |
3.3 训练难度对比
| 模型 | 超参数敏感性 | 收敛速度 | 内存需求 |
|---|
| GAN | 高 | 中 | 中 |
| VAE | 低 | 快 | 低 |
| Diffusion | 中 | 慢 | 高 |
4. 最佳实践
4.1 生成模型选择
def choose_generative_model(task_type): models = { 'image_generation': 'diffusion', 'data_completion': 'vae', 'anomaly_detection': 'vae', 'style_transfer': 'gan' } return models.get(task_type, 'diffusion') class GenerativeModelSelector: @staticmethod def select(config): models = { 'gan': GAN, 'vae': VAE, 'diffusion': DiffusionModel } return models[config['type']](**config.get('params', {}))
4.2 生成模型训练流程
class GenerativeModelTrainingWorkflow: def __init__(self): pass def run(self, model_type, data, config): print("1. 初始化模型...") model = self._create_model(model_type, config) print("2. 训练模型...") model.train(data, epochs=config.get('epochs', 100)) print("3. 评估模型...") metrics = self._evaluate(model, data) print("4. 生成样本...") samples = model.generate(config.get('num_samples', 10)) return model, metrics, samples
5. 总结
生成模型是深度学习的重要方向:
- VAE:稳定训练,适合数据补全
- GAN:高质量生成,但训练不稳定
- Diffusion:当前最佳图像生成
- Flow:精确密度估计
对比数据如下:
- Diffusion模型生成质量最高
- VAE训练最稳定
- GAN需要仔细调参
- 推荐根据任务选择合适的模型