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

发布于:2025-05-09 ⋅ 阅读:(19) ⋅ 点赞:(0)

机器学习实操 第二部分 神经网路和深度学习 第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)

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)

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


网站公告

今日签到

点亮在社区的每一天
去签到