For your handwritten solutions, please scan or take a picture of them. Alternatively, you can write them in markdown if you prefer.
Only .ipynb files will be graded for your code.
Compress all the files into a single .zip file.
Do not submit a printed version of your code, as it will not be graded.
The Turing test, developed by Alan Turing in 1950, is a test of a machine's ability to exhibit intelligent behaviour equivalent to, or indistinguishable from, that of a human. Turing proposed that a human evaluator would judge natural language conversations between a human and a machine designed to generate human-like responses. The evaluator would be aware that one of the two partners in conversation is a machine, and all participants would be separated from one another. The conversation would be limited to a text-only channel such as a computer keyboard and screen so the result would not depend on the machine's ability to render words as speech. If the evaluator cannot reliably tell the machine from the human, the machine is said to have passed the test. The test results do not depend on the machine's ability to give correct answers to questions, only how closely its answers resemble those a human would give.
(1) Explain the GAN algorithm using the figure provided below.
(2) You are also asked to write loss functions and outline optimizaton procedure as well using the figure provided below..
(3) While training a GAN to generate high-quality human face images, you notice that the generator's cost, $J^{(G)}$, is extremely low. However, the generated images lack meaningful output images. Can you explain this phenomenon in the context of the GAN's loss functions?
In the GAN example, we employed the following fully connected neural networks. However, we discovered that DCGAN is a direct extension of GAN, differing primarily in its utilization of convolutional and convolutional-transpose layers in the discriminator and generator. Now, let's construct a custom generative model for generating MNIST digit 2 using these convolutional and convolutional-transpose layers.
## your code here
#
By the CycleGAN algorithm, we can even change the style of images. In this problem, we will change the white-colored digit in a black background image to the black-colored digit in a white background image.We will use the following CycleGAN structure.
Description
Generate Xab image by generator Gab
Generate Xaba image by generator Gba
Note: I strongly recommend you to use either GPU or Colab. Don't use CPU to train your model! It will take a long time to complete training.
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
(1) Load images corresponding to the number 1 from the MNIST dataset. Then use half as they are and for the other half, invert the colors so that the background is white and the digits are black. Notice that they are not paired datasets.
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_x = train_images[np.where(train_labels == 1)]/255.0
half_index = train_x.shape[0] // 2
# unpaired dataset
wnum = train_x[:half_index].reshape(-1,784)
bnum = train_x[half_index:].reshape(-1,784)
bnum = np.abs(1 - bnum)
# plot a randomly selected image from wnum
## your code here
#
# plot a randomly selected image from bnum
## your code here
#
(2) Define your model.
n_G_input
: 28 $\times$ 28n_G_encoder1
: 256n_G_latent1
: 50n_G_decoder1
: 256n_G_encoder2
: 256n_G_latent2
: 50n_G_decoder2
: 256n_D_input
: 28 $\times$ 28n_D_hidden
: 256n_D_output
: 2Gab = # your code here
Gba = # your code here
discriminator = # your code here
# Discriminator compile
## your code here
#
(3) Refer to the loss function below and train the model. And plot Xab image and Xaba image for each print. Note that we are not asking you to make a perfect inverted image.
1: Fix $G$ and perform a gradient step to
2: Fix $D$ and perform a gradient step to
3: Minimize reconstruct error
# Genreator AB
## your code here
#
# Generator BA
## your code here
#
def plot_generated_images(imgs, Gab, Gba):
idx = np.random.randint(0, imgs.shape[0], 1 )
Xab = Gab.predict(imgs[idx].reshape(-1, 28*28), verbose = 0)
Xaba = Gba.predict(Xab.reshape(-1, 28*28), verbose = 0)
plt.figure(figsize = (8, 3))
plt.subplot(1, 3, 1)
plt.imshow(imgs[idx].reshape(28, 28), 'gray', interpolation = 'nearest')
plt.title('Xa')
plt.axis('off')
plt.tight_layout()
plt.subplot(1, 3, 2)
plt.imshow(Xab.reshape(28, 28), 'gray', interpolation = 'nearest')
plt.title('Xab')
plt.axis('off')
plt.tight_layout()
plt.subplot(1, 3, 3)
plt.imshow(Xaba.reshape(28, 28), 'gray', interpolation = 'nearest')
plt.title('Xaba')
plt.axis('off')
plt.tight_layout()
plt.show()
## your code here
#