Gen-ai 简明教程
How do Generative Adversarial Networks (GAN) Work?
生成对抗网络 (GAN) 是生成建模的一种有力方法。GAN 基于生成与原始训练数据相似的复杂新输出的深度神经网络架构。
Generative Adversarial Networks (GAN) is a powerful approach to generative modeling. GANs are based on deep neural network architecture that generates a new complex output that looks like the original training data.
GAN 通常利用卷积神经网络 (CNN) 等架构。事实上,ChatGPT 与基于深度学习的其他 LLM(大型语言模型)一样,是 GAN 的一个非凡应用。本章涵盖了您需要了解的有关 GAN 及其工作原理的所有知识。
GANs typically utilize architectures such as convolutional neural networks (CNN). In fact, ChatGPT, like other LLMs (Large Language Models) based on deep learning, is a remarkable application of GAN. This chapter covers all you need to know about GAN and its working.
What is a Generative Adversarial Network?
生成对抗网络 (GAN) 是一种用于无监督学习的人工智能框架。GAN 由两个神经网络组成:一个 Generator 和一个 Discriminator 。GAN 使用对抗训练来生成类似于实际数据的虚拟数据。
A Generative Adversarial Network (GAN) is a type of artificial intelligence framework that is used for unsupervised learning. GANs are made up of two neural networks: a Generator and a Discriminator. GANs use adversarial training to produce artificial data that resembles the actual data.
可以将 GAN 分为 three components −
GANs can be divided to have three components −
-
Generative − This component focuses on learning how to generate new data by understanding the underlying patterns in the dataset.
-
Adversarial − In simple terms, "adversarial" means setting two things in opposition. In GANs, the generated data is compared to real data from the dataset. This is done using a model trained to distinguish between real and fake data. This model is known as discriminator.
-
Networks − To enable the learning process, GANs uses deep neural networks.
在了解 GAN 的工作原理之前,让我们首先讨论它的两个主要部分:生成器模型和鉴别器模型。
Before getting into how GANs work, let’s first discuss its two primary parts: the Generator model and the Discriminator model.
The Generator Model
生成器模型的目标是生成新的数据样本,这些数据样本旨在模拟来自数据集的真实数据。
The goal of the generator model is to generate new data samples that are intended to resemble real data from the dataset.
-
It takes random input data as input and transforms it into synthetic data samples.
-
Once transformed, the other goal of the generator is to produce data that is identical to real data when presented to the discriminator.
-
The generator is implemented as a neural network model. Depending on the type of data being generated, it uses fully connected layers like Dense or Convolutional layers.
The Discriminator Model
判别器模型的目标是评估输入数据并尝试区分来自数据集的真实数据样本和生成器模型生成的虚假数据样本。
The goal of the discriminator model is to evaluate the input data and tries to distinguish between real data samples from the dataset and fake data samples generated by the generator model.
-
It takes input data and predicts whether it is real or fake.
-
Another goal of the discriminator model is to correctly classify the source of the input data as real or fake.
-
Like the generator model, the discriminator model is also implemented as a neural network model. It also uses Dense or Convolutional layers.
在对抗生成网络 (GAN) 的训练期间,生成器和判别器同时接受训练,但以相反的方式进行训练,即彼此竞争。
During the training of a GAN, both the generator and the discriminator are trained simultaneously but in adverse ways, i.e., in competition with each other.
How does a GAN Work?
要了解 GAN 的工作原理,首先查看此图表,该图表展示了 GAN 的不同组件如何生成与真实数据非常相似的新的数据样本 −
To understand how a GAN works, first take a look at this diagram that shows how the different components of a GAN function to generate new data samples that closely resemble with real data −
GAN 有两个主要组件: generator network 和 discriminative network 。以下是 GAN 工作涉及的步骤 −
GANs have two main components: a generator network and a discriminative network. Given below are the steps involved in the working of a GAN −
Initialization
GAN 包含两个神经网络:生成器(例如 G )和判别器(例如 D )。
The GAN consists of two neural networks: the Generator (say G) and the Discriminator (say D).
-
The goal of the generator is to generate new data samples like images or text that closely resemble the real data from the dataset.
-
The discriminator, playing the role of a critic, has the goal to distinguish between the real data and the data generated by the generator.
Training Loop
训练循环涉及在训练生成器和判别器之间交替。
The training loop involves alternating between training the generator and the discriminator.
Training the Discriminator
在训练判别器时,对于每次迭代 −
While training the discriminator, for each iteration −
-
First, select a batch of real data samples from the dataset.
-
Next, enerate a batch of fake data samples using the current generator.
-
Once generated, train the discriminator on both the real and fake data samples.
-
Finally, the discriminator learns to distinguish between real and fake data by adjusting its weights to minimize its classification error.
Training the Generator
在训练生成器的过程中,对于每一次迭代 -
While training the generator, for each iteration −
-
First, generate a batch of fake data samples using the generator.
-
Next, train the generator to produce fake data that the discriminator classifies as the real data. To do this, we need to pass the fake data through the discriminator and update the generator’s weights based on the discriminator’s classification error.
-
Finally, the generator will learn to produce more realistic fake data by adjusting its weights to maximize the discriminator’s error when classifying its generated samples.
Adversarial Training
随着训练的进行,生成器和判别器都以对抗的方式提高了它们的性能,即对立。
As the training progresses, both the generator and discriminator improve their performance in an adversarial manner, i.e., in opposition.
生成器在创建类似于真实数据的虚假数据方面变得更好,而判别器在区分真实数据和虚假数据方面变得更好。
The generator gets better at creating fake data that resembles real data, while the discriminator gets better at distinguishing between real and fake data.
在生成器与判别器之间的这种对抗关系的帮助下,两个网络都尝试不断改进,直到生成器生成与真实数据相同的数据。
With the help of this adversarial relationship between the generator and discriminator, both the networks try to improve continuously until the generator generates data that is identical to the real data.
Evaluation
一旦训练结束,就可以使用生成器根据数据集生成新的数据样本,这些样本类似于真实数据。
Once the training is over, the generator can be used to generate new data samples that resemble the real data from the dataset.
我们可以通过目测样本或使用诸如相似度得分或分类器准确度之类的定量衡量标准来评估生成数据的质量。
We can evaluate the quality of the generated data either by inspecting samples visually or using quantitative measures like similarity scores or classifier accuracy.
Conclusion
生成对抗网络(GAN)是最突出、最广泛使用的生成模型之一。在本节中,我们解释了 GAN 的基础知识以及它如何使用神经网络来生成类似于实际数据的人工数据的工作原理。
Generative Adversarial Networks (GANs) is one of the most prominent and widely used generative models. In this chapter, we explained the basics of a GAN and how it works using neural networks to produce artificial data that resembles actual data.
GAN 工作中涉及的步骤包括:初始化、训练循环、训练判别器、训练生成器、对抗性训练、评估以及微调和优化。
The steps that are involved in the working of a GAN include: Initialization, Training Loop, Training the Discriminator, Training the Generator, Adversarial Training, Evaluation, and Fine tuning and Optimization.