ART-ificial Intelligence in Gaming Part 2: GANs
By: Danielle Trinh
Tags: Deep Learning, Machine Learning, Artificial Intelligence, Gaming, AI

This is part two of a miniseries, so please read part one before continuing:

This post is quite content-heavy despite my efforts to keep it simple, so you do not have to expect yourself to understand everything at once.

Besides the two bonus examples, a common theme between the video games is how AI was trained to generate playable and non-repetitive levels, or stages. How was it done? Generative adversarial networks (GANs).

After a night out with his friends in 2014, Ian Goodfellow returned home and invented what is now called a GAN. This AI technique is widely regarded as a breakthrough in deep learning and unsupervised learning. A generative adversarial network can be described as two neural networks that contest with each other in a zero-sum game. One neural network acts as cop, while the other acts as counterfeiter. The model can be used in unsupervised learning to generate new examples that plausibly could have been drawn from the original dataset.

Hold up, that was a lot of buzzwords…

Relevant Vocabulary

Deep learning: a subset of machine learning that uses multi-layer neural networks

Supervised learning: a type of machine learning that maps an input to an output based on example input-output pairs that are labelled

a type of machine learning that maps an input to an output based on example input-output pairs that are labelled Unsupervised learning: a type of machine learning that looks for patterns within unlabelled data

Neural network: a mathematical model of connected neurons used to process nonlinear relationships between inputs and outputs

There are various types of neural networks that I will not go over, but you can find an article here.

Neuron: a group of weighted (w) inputs (X) that returns an output after a mathematical activation function (e.g. sigmoid) is applied

Weights are added to inputs to determine their significance.

GAN — let’s break it down

As you read the subsections, keep my general definition of a GAN in mind.

A generative adversarial network can be described as two neural networks that contest with each other in a zero-sum game. One neural network acts as cop, while the other acts as counterfeiter. The model can be used in unsupervised learning to generate new examples that plausibly could have been drawn from the original dataset.

Generative (vs discriminative)

A generative model is one that creates, or generates, new examples of plausible inputs. This is often used in unsupervised learning. The other statistical classification model is a discriminative model, where it classifies, or discriminates, examples of input variables into different groups (i.e. the model predicts which class each input belongs in). This is often used in supervised learning.

Adversarial

Adversarial describes two opposing sides.

Network

In this case, “network” is referring to a neural network, or a collection of connected neurons. (Reference the “Relevant Vocabulary” section above)

GAN — let’s put it together

These two diagrams are both depicting the basic architecture of a GAN. Remember the cop and counterfeiter metaphor from my definition? The generator model is the counterfeiter, and the discriminator model is the cop. This means the generator creates fake examples that are given to the discriminator, along with real examples from the original dataset. The discriminator then predicts whether each input is fake or not, which is where the zero-sum game comes in. For each example the discriminator predicts as a true negative (correct about the example being fake), only the generator model “loses” and is updated. For each example the discriminator predicts as a false positive (predicts the example is real, but it’s actually fake) or a false negative (predicts the example is fake, but it’s actually real), only the discriminator model “loses” and is updated. In short, only one model is updated at a time, depending on whether the discriminator predicts the generated example correctly or incorrectly. The process of updating is known as backpropagation, but I’ll talk about that a little later. By using this architecture, both models effectively improve with little human interference. The training process ends once the generated examples fool the discriminator model enough.

Here’s another example of the GAN architecture. The generator’s input is random noise (previously referred to as latent random variable and random input vector), in the form of a long vector, or array of numbers. Using a neural network, the generator translates the input into a three-dimensional image, which is used as input for the discriminator, alongside images from the real training set. Notice how the discriminator structure is pretty much the opposite of the generator model. This is because it is also a neural network, but it takes input in the form of three-dimensional images and translates it into an output by reshaping the data into a long vector. Don’t worry if that didn’t make sense to you — only recognize that the discriminator input is the generator output.

Another thing to keep in mind is that since the generator and discriminator are two separate neural networks, they have different training processes. When one is being trained, the other must remain constant. “Training” the models is essentially going through the backpropagation process explained in the next sections.

Generator Model

Let’s focus on the generator model. Latent random variable, random input vector, random noise, random input — what does it all mean? Well, I hope you picked up on the fact that it’s some sort of random input for the generator. All neural networks need some sort of input, and in this case, we’re trying to generate something completely new as an output. By using a random input, the generator can produce a wide variety of samples. Once it goes through the discriminator, generator loss is produced, which penalizes the generator for samples that were predicted as true negatives (i.e. the generator’s punishment for being caught counterfeiting). In this particular diagram, backpropagation is the mathematical process that adjusts the weights for the generator neural network and minimizes generator loss (i.e. makes generator better at creating samples that look like they’re from the actual dataset 😃).

Discriminator Model

Just like the generator model, the discriminator is penalized by discriminator loss through backpropagation — the mathematical process that adjusts the weights for the discriminator neural network and minimizes discriminator loss (i.e. makes discriminator better at recognizing samples that are not from the actual dataset 😃).

CONCLUSION

Yikes, that was a lot of big concepts to go through!

If you could not fully understand parts of this blog post, your biggest takeaway should be that a generative adversarial network (GAN) is essentially a game between two AI models, where the generator acts as counterfeiter to create fake content, while the discriminator acts as cop to try and catch the fakes among real samples. The generator wins when the discriminator classifies the fake sample as a real sample and loses when classified as a fake. The losses are what stimulate improvements in the algorithms of the models.

Tying it back to the video games, MarioGAN was the GAN trained to generate Super Mario levels. The generator created the levels and received feedback from the discriminator and generator loss based on certain characteristics, such as the amount of reachable ground tiles or the number of jumps required. The same concepts can be applied to the other game examples I gave in part one. And if you’re curious, this is the code for MarioGAN.

Yes, that was a lot of content stuffed into one blog post, but I hope you learned something! The next post in this miniseries will be about real-world implications and wrapping up concepts. I promise it will be a lot easier to understand.

See you soon :)