Generative Adversarial Networks (GANs) are an exciting innovation in artificial intelligence and machine learning. They were introduced by Ian Goodfellow et al. in 2014. GANs belong to the set of generative models, which are effective in generating new content.
At the heart of GANs, there are two components — a Generator and a Discriminator. The work of the Generator is to generate plausible data while the Discriminator's job is to distinguish between actual and generated data.
Image Source: Medium
The unique aspect of GANs is the way they train. They implement a two-player minimax game where the Generator tries to fool the Discriminator by creating better and better fakes, while the Discriminator learns to get better at distinguishing fake from real.
Now, let's walk through a code snippet of a simple Generative Adversarial Network using Python and Tensorflow/Keras.
import tensorflow as tf from tensorflow.keras.layers import Dense, LeakyReLU from tensorflow.keras.models import Sequential # Define the standalone generator model def define_generator(latent_dim, n_outputs=2): model = Sequential() model.add(Dense(15, activation='relu', kernel_initializer='he_uniform', input_dim=latent_dim)) model.add(Dense(n_outputs, activation='linear')) return model # Define the standalone discriminator model def define_discriminator(n_inputs=2): model = Sequential() model.add(Dense(25, kernel_initializer='he_uniform', input_dim=n_inputs)) model.add(LeakyReLU(alpha=0.01)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') return model # Size of latent space latent_dim = 5 # Define the discriminator d_model = define_discriminator() # Define the generator g_model = define_generator(latent_dim) # Summarize the model d_model.summary() g_model.summary()
In the code above, we have first defined the generator and discriminator as two separate models. We are using the ReLU and LeakyReLU activation functions. The generator uses a linear activation function to output generated samples, while the discriminator uses sigmoid activation to output probability scores.
This is a very simple representation of a GAN using Keras and this example can be expanded upon for more complex and practical use cases. GANs are a vibrant and fast-evolving field in machine learning that continue to push the boundaries of what's possible with AI.