机器学习实操 第二部分 神经网路和深度学习 第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)和扩散模型的基本原理和实现方法。内容涵盖了这些模型的架构、训练方法、面临的挑战以及实际应用。这些知识将帮助读者在无监督学习和生成模型领域构建高效、创新的解决方案。

相关推荐
shaibdoio22 分钟前
意图识别实战:从规则匹配到传统机器学习,再到混合大模型方案
人工智能·机器学习
Ivanqhz29 分钟前
BURG(自底向上重写生成器)
服务器·数据库·人工智能·深度学习·算法
言乐636 分钟前
逻辑回归与利弊
人工智能·算法·机器学习·数据挖掘·逻辑回归
袖清暮雨44 分钟前
机器学习之线性回归
人工智能·机器学习·线性回归
Ivanqhz11 小时前
激活函数在 Transformer 中的作用及各种变体简述
java·linux·数据库·人工智能·深度学习
workflower11 小时前
AI system product quality model
大数据·人工智能·机器学习·云计算·无人机
可乐ea12 小时前
Agent 模型分级升级路由:用置信度阈值把贵模型省下来
人工智能·算法·机器学习·结构化输出·置信度路由·模型升级路由·大模型成本优化
晚安code12 小时前
大模型是什么?讲透 Token、上下文窗口、Temperature 和 MoE
人工智能·深度学习·机器学习
for_ever_love__12 小时前
机器学习入门——手写线性回归与梯度下降
人工智能·python·学习·机器学习·线性回归
CHEEVEN_QY13 小时前
液冷板热阻计算与流阻优化的实战方法
人工智能·算法·机器学习