机器学习实操 第二部分 神经网路和深度学习 第17章 编码器、生成对抗网络和扩散模型

机器学习实操 第二部分 神经网路和深度学习 第17章 编码器、生成对抗网络和扩散模型

内容概要

第17章深入探讨了自编码器(Autoencoders)、生成对抗网络(GANs)和扩散模型(Diffusion Models)。这些模型能够学习输入数据的密集表示(称为潜在表示或编码),并在无监督的情况下生成新的数据。章节详细介绍了这些模型的原理、架构和应用,包括降维、特征提取、无监督预训练和生成新数据。

主要内容

  1. 自编码器

    • 自编码器是一种人工神经网络,能够学习输入数据的密集表示。这些表示通常具有比输入数据更低的维度,使得自编码器在降维和特征提取方面非常有用。
    • 自编码器由编码器(将输入转换为潜在表示)和解码器(将潜在表示转换回输出)组成。常见的类型包括欠完备自编码器、堆叠自编码器和卷积自编码器。
    • 降维和可视化:自编码器可以用于降维和数据可视化。例如,通过将高维数据投影到低维空间,可以使用t-SNE等算法进一步处理以进行可视化。
    • 无监督预训练:自编码器可以用于无监督预训练,通过在大量未标记数据上训练自编码器,然后重用其编码器层来构建新的神经网络。
  2. 去噪自编码器和稀疏自编码器

    • 去噪自编码器:通过在输入中添加噪声并训练模型恢复原始输入,迫使模型学习数据的有用特征。
    • 稀疏自编码器:通过在损失函数中添加稀疏性约束,迫使模型减少编码层中激活的神经元数量,从而学习更有意义的特征。
  3. 变分自编码器(VAEs)

    • VAEs是一种概率自编码器,能够生成新的数据实例。它们通过学习数据的潜在分布来实现这一点。
    • VAEs的编码器输出均值和标准差,解码器从高斯分布中采样并解码生成新的数据。
  4. 生成对抗网络(GANs)

    • GANs由生成器和判别器组成。生成器试图生成逼真的数据,而判别器试图区分真实数据和生成数据。两者在训练过程中相互竞争。
    • 训练困难:GANs的训练面临模式崩溃和不稳定性等问题。为了解决这些问题,提出了多种技术,如经验回放和迷你批量判别。
  5. 扩散模型

    • 扩散模型通过逐步添加噪声来训练模型学习逆向扩散过程,从而生成新的数据。它们在生成高质量图像方面表现出色,但生成速度较慢。
    • 潜在扩散模型:通过在潜在空间中进行扩散过程,显著提高了生成速度和图像质量。

精彩语录

  1. 中文 :自编码器能够学习输入数据的密集表示,这些表示通常具有比输入数据更低的维度,使得自编码器在降维和特征提取方面非常有用。
    英文原文 :Autoencoders are artificial neural networks capable of learning dense representations of the input data, called latent representations or codings, without any supervision. These codings typically have a much lower dimensionality than the input data, making autoencoders useful for dimensionality reduction.
    解释:强调了自编码器在降维和特征提取中的应用。

  2. 中文 :GANs由生成器和判别器组成,生成器试图生成逼真的数据,而判别器试图区分真实数据和生成数据。
    英文原文 :GANs are composed of two neural networks: a generator that tries to generate data that looks similar to the training data, and a discriminator that tries to tell real data from fake data.
    解释:介绍了GANs的基本架构和工作原理。

  3. 中文 :扩散模型通过逐步添加噪声来训练模型学习逆向扩散过程,从而生成新的数据。
    英文原文 :The core idea behind diffusion models is to train a model to learn the reverse process of a diffusion process, similar to a drop of milk diffusing in a cup of tea.
    解释:解释了扩散模型的基本原理。

关键代码

使用Keras实现变分自编码器(VAE)

python 复制代码
class Sampling(tf.keras.layers.Layer):
    def call(self, inputs):
        mean, log_var = inputs
        return tf.random.normal(tf.shape(log_var)) * tf.exp(log_var / 2) + mean

codings_size = 10
inputs = tf.keras.layers.Input(shape=[28, 28])
Z = tf.keras.layers.Flatten()(inputs)
Z = tf.keras.layers.Dense(150, activation="relu")(Z)
Z = tf.keras.layers.Dense(100, activation="relu")(Z)
codings_mean = tf.keras.layers.Dense(codings_size)(Z)
codings_log_var = tf.keras.layers.Dense(codings_size)(Z)
codings = Sampling()([codings_mean, codings_log_var])
variational_encoder = tf.keras.Model(inputs=[inputs], outputs=[codings_mean, codings_log_var, codings])

decoder_inputs = tf.keras.layers.Input(shape=[codings_size])
x = tf.keras.layers.Dense(100, activation="relu")(decoder_inputs)
x = tf.keras.layers.Dense(150, activation="relu")(x)
x = tf.keras.layers.Dense(28 * 28)(x)
outputs = tf.keras.layers.Reshape([28, 28])(x)
variational_decoder = tf.keras.Model(inputs=[decoder_inputs], outputs=[outputs])

_, _, codings = variational_encoder(inputs)
reconstructions = variational_decoder(codings)
variational_ae = tf.keras.Model(inputs=[inputs], outputs=[reconstructions])

latent_loss = -0.5 * tf.reduce_sum(1 + codings_log_var - tf.exp(codings_log_var) - tf.square(codings_mean), axis=-1)
variational_ae.add_loss(tf.reduce_mean(latent_loss) / 784.)
variational_ae.compile(loss="mse", optimizer="nadam")
history = variational_ae.fit(X_train, X_train, epochs=25, batch_size=128, validation_data=(X_valid, X_valid))

使用Keras实现生成对抗网络(GAN)

python 复制代码
codings_size = 30
generator = tf.keras.Sequential([
    tf.keras.layers.Dense(100, activation="relu", kernel_initializer="he_normal"),
    tf.keras.layers.Dense(150, activation="relu", kernel_initializer="he_normal"),
    tf.keras.layers.Dense(28 * 28, activation="sigmoid"),
    tf.keras.layers.Reshape([28, 28])
])

discriminator = tf.keras.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(150, activation="relu", kernel_initializer="he_normal"),
    tf.keras.layers.Dense(100, activation="relu", kernel_initializer="he_normal"),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

gan = tf.keras.Sequential([generator, discriminator])

discriminator.compile(loss="binary_crossentropy", optimizer="rmsprop")
discriminator.trainable = False
gan.compile(loss="binary_crossentropy", optimizer="rmsprop")

def train_gan(gan, dataset, batch_size, codings_size, n_epochs):
    generator, discriminator = gan.layers
    for epoch in range(n_epochs):
        for X_batch in dataset:
            noise = tf.random.normal(shape=[batch_size, codings_size])
            generated_images = generator(noise)
            X_fake_and_real = tf.concat([generated_images, X_batch], axis=0)
            y1 = tf.constant([[0.]] * batch_size + [[1.]] * batch_size)
            discriminator.train_on_batch(X_fake_and_real, y1)
            noise = tf.random.normal(shape=[batch_size, codings_size])
            y2 = tf.constant([[1.]] * batch_size)
            gan.train_on_batch(noise, y2)

train_gan(gan, dataset, batch_size=32, codings_size=30, n_epochs=50)

总结

通过本章的学习,读者将掌握自编码器、生成对抗网络(GANs)和扩散模型的基本原理和实现方法。内容涵盖了这些模型的架构、训练方法、面临的挑战以及实际应用。这些知识将帮助读者在无监督学习和生成模型领域构建高效、创新的解决方案。

相关推荐
胡耀超6 分钟前
标签体系设计与管理:从理论基础到智能化实践的综合指南
人工智能·python·深度学习·数据挖掘·大模型·用户画像·语义分析
fzyz1232 小时前
Windows系统下WSL从C盘迁移方案
人工智能·windows·深度学习·wsl
martian6653 小时前
支持向量机(SVM)深度解析:从数学根基到工程实践
算法·机器学习·支持向量机
FF-Studio4 小时前
【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》
人工智能·pytorch·深度学习·线性代数·机器学习·数学建模·transformer
云渚钓月梦未杳4 小时前
深度学习03 人工神经网络ANN
人工智能·深度学习
贾全5 小时前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天5 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
我是小哪吒2.05 小时前
书籍推荐-《对抗机器学习:攻击面、防御机制与人工智能中的学习理论》
人工智能·深度学习·学习·机器学习·ai·语言模型·大模型
慕婉03075 小时前
深度学习前置知识全面解析:从机器学习到深度学习的进阶之路
人工智能·深度学习·机器学习
蓝婷儿6 小时前
Python 机器学习核心入门与实战进阶 Day 2 - KNN(K-近邻算法)分类实战与调参
python·机器学习·近邻算法