<jupyter_start><jupyter_text>Exercise Sheet 9<jupyter_code>from __future__ import print_function, division

import numpy as np
import matplotlib.pyplot as plt
import sys
import tensorflow as tf

from tensorflow.keras.layers import Input, Dense, Reshape, Flatten, Dropout
from tensorflow.keras.layers import BatchNormalization, Activation, ZeroPadding2D
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import UpSampling2D, Conv2D
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam, RMSprop<jupyter_output><empty_output><jupyter_text>GAN Optimisation For the example $V(x, y) = x y$ perform alternating gradient descent updates with respect to $x$ and $y$ as we would perform when training a GAN, i.e. one player is performing updates to optimise $V$ and one player to optimise $−V$. Identify the trajectory of this update. Which ‘late-time’ behaviour do you find?  Solution First we see what happens when both players update with respect to the full gradient<jupyter_code># X = [x, y]
V = lambda X: X[0] * X[1]
Vneg = lambda X: -V(X)
dV = lambda X: np.array([X[1], X[0]])
dVneg = lambda X: -dV(X)
def alt_grad_descent1(epochs, lrate):
    X = np.random.normal(0,10,2)
    hist = np.zeros([epochs ,2])
    for j in range(epochs):
        # minimize V
        X = X - lrate*dV(X)
        # minimize -V
        X = X - lrate*dVneg(X)
        hist[j] = X
    return hist

hist = alt_grad_descent1(50000, 10**-2)
Vhist = [V(hist[i]) for i in range(len(hist))]
plt.plot(Vhist)
plt.plot(hist[:,0], hist[:,1])<jupyter_output><empty_output><jupyter_text>Now we check what happens when player 1 updates only the x-direction, while player 2 updates only the y-direction. See [here](https://arxiv.org/pdf/1801.04406.pdf) for the relation to GAN training<jupyter_code>def alt_grad_descent2(epochs, lrate):
    X = np.random.normal(0,10,2)
    hist = np.zeros([epochs ,2])
    for j in range(epochs):
        # minimize V
        X[0] = X[0] - lrate*dV(X)[0]
        # minimize -V
        X[1] = X[1] - lrate*dVneg(X)[1]
        hist[j] = X
    return hist

hist = alt_grad_descent2(50000, 10**-3)
Vhist = [V(hist[i]) for i in range(len(hist))]
plt.plot(Vhist)
plt.plot(hist[:,0], hist[:,1])<jupyter_output><empty_output><jupyter_text>GAN The aim of this exercise is to implement a GAN architecture and to test it on the polynomial dataset from the previous exercise sheets. You can adapt the architecture from the MNIST example in the lectures.  $\star$ Update your loss function to implement a Wasserstein GAN.  Note that in case you do not have a local environment where you can calculate on a GPU, you can calculate online on collab on a GPU. The references are: [GAN](https://arxiv.org/abs/1406.2661), [WGAN](https://arxiv.org/abs/1701.07875)  Solution<jupyter_code>!mkdir images
# Polynomial data

size = 40

# evaluate polynomial over grid of size 40x40
def polynomial(degree):
    coeff = np.random.normal(0,1,(degree+1, degree+1))
    #coeff = np.random.uniform(-1,1,(degree+1, degree+1))
    return [[sum([coeff[i,j]*((x/size)**i)*((y/size)**j)
            for i in range(degree+1) for j in range(degree+1) if (i+j)<=degree]) 
            for x in range(size)] for y in range(size)]

# training set of polynomial images of degree <=5
maxdegree = 5
size = 40
num_polys = 20000
polydata = np.array([polynomial(np.random.randint(0,maxdegree)) for i in range(num_polys)])
polydata = tf.keras.utils.normalize(polydata)
np.save('polydata.npy', polydata)
polydata = np.load('polydata.npy')
plt.imshow(polydata[1])
class DCGAN():
    def __init__(self):
        # Input shape
        self.img_rows = 40
        self.img_cols = 40
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.latent_dim = 2

        optimizer = Adam(0.0002, 0.5)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss='binary_crossentropy',
            optimizer=optimizer,
            metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        # The generator takes noise as input and generates imgs
        z = Input(shape=(self.latent_dim,))
        img = self.generator(z)

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator takes generated images as input and determines validity
        valid = self.discriminator(img)

        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator
        self.combined = Model(z, valid)
        self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)

    def build_generator(self):

        model = Sequential()

        model.add(Dense(128 * 10 * 10, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((10, 10, 128)))
        model.add(UpSampling2D())
        model.add(Conv2D(128, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(UpSampling2D())
        model.add(Conv2D(64, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(Conv2D(self.channels, kernel_size=3, padding="same"))
        model.add(Activation("tanh"))

        model.summary()

        noise = Input(shape=(self.latent_dim,))
        img = model(noise)

        return Model(noise, img)

    def build_discriminator(self):

        model = Sequential()

        model.add(Conv2D(16, kernel_size=3, strides=2, input_shape=self.img_shape, padding="same"))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Conv2D(32, kernel_size=3, strides=2, padding="same"))
        model.add(ZeroPadding2D(padding=((0,1),(0,1))))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Conv2D(64, kernel_size=3, strides=2, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Conv2D(128, kernel_size=3, strides=1, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Flatten())
        model.add(Dense(1, activation='sigmoid'))
        

        model.summary()

        img = Input(shape=self.img_shape)
        validity = model(img)

        return Model(img, validity)

    def train(self, epochs, batch_size=128, save_interval=50):

        # Load the dataset
        # (X_train, _), (_, _) = mnist.load_data()
        X_train = polydata

        # Rescale -1 to 1
        # X_train = X_train / 127.5 - 1.
        X_train = np.expand_dims(X_train, axis=3)

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs):

            # ---------------------
            #  Train Discriminator
            # ---------------------

            # Select a random half of images
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            # Sample noise and generate a batch of new images
            noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
            gen_imgs = self.generator.predict(noise)

            # Train the discriminator (real classified as ones and generated as zeros)
            d_loss_real = self.discriminator.train_on_batch(imgs, valid)
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # ---------------------
            #  Train Generator
            # ---------------------

            # Train the generator (wants discriminator to mistake images as real)
            g_loss = self.combined.train_on_batch(noise, valid)

            # Plot the progress
            print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))

            # If at save interval => save generated image samples
            if epoch % save_interval == 0:
                self.save_imgs(epoch)

    def save_imgs(self, epoch):
        r, c = 5, 5
        noise = np.random.normal(0, 1, (r * c, self.latent_dim))
        gen_imgs = self.generator.predict(noise)

        # Rescale images 0 - 1
        gen_imgs = 0.5 * gen_imgs + 0.5

        fig, axs = plt.subplots(r, c)
        cnt = 0
        for i in range(r):
            for j in range(c):
                axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
                axs[i,j].axis('off')
                cnt += 1
        fig.savefig("images/poly_%d.png" % epoch)
        plt.close()


if __name__ == '__main__':
    dcgan = DCGAN()
dcgan.train(epochs=4000, batch_size=32, save_interval=50)
fig, axes = plt.subplots(2, 8, figsize=(19,5))
for i in range(2):
  for j in range(8):
    out = dcgan.generator.predict(np.reshape(np.random.normal(0,1,2),(1,2)))
    axes[i,j].imshow(out[0,:,:,0])
    
fig, axes = plt.subplots(2, 8, figsize=(19,5))
for i in range(2):
  for j in range(8):
    axes[i,j].imshow(polydata[i*j+j])<jupyter_output><empty_output><jupyter_text>Wasserstein GAN  [Paper](https://arxiv.org/pdf/1701.07875.pdf)<jupyter_code>from tensorflow.keras import backend as K


class WGAN():
    def __init__(self):
        self.img_rows = 40
        self.img_cols = 40
        self.channels = 1
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.latent_dim = 10

        # Following parameter and optimizer set as recommended in paper
        self.n_critic = 10
        self.clip_value = 0.03
        optimizer = RMSprop(lr=0.00003)

        # Build and compile the critic
        self.critic = self.build_critic()
        self.critic.compile(loss=self.wasserstein_loss,
            optimizer=optimizer,
            metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator()

        # The generator takes noise as input and generated imgs
        z = Input(shape=(self.latent_dim,))
        img = self.generator(z)

        # For the combined model we will only train the generator
        self.critic.trainable = False

        # The critic takes generated images as input and determines validity
        valid = self.critic(img)

        # The combined model  (stacked generator and critic)
        self.combined = Model(z, valid)
        self.combined.compile(loss=self.wasserstein_loss,
            optimizer=optimizer)

    def wasserstein_loss(self, y_true, y_pred):
        return K.mean(y_true * y_pred)

    def build_generator(self):

        model = Sequential()

        model.add(Dense(128 * 10 * 10, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((10, 10, 128)))
        model.add(UpSampling2D())
        model.add(Conv2D(128, kernel_size=4, padding="same"))
#         model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(UpSampling2D())
        model.add(Conv2D(64, kernel_size=4, padding="same"))
#         model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(Conv2D(self.channels, kernel_size=4, padding="same"))
        model.add(Activation("tanh"))

        model.summary()

        noise = Input(shape=(self.latent_dim,))
        img = model(noise)

        return Model(noise, img)

    def build_critic(self):

        model = Sequential()

        x=8
        model.add(Conv2D(x*16, kernel_size=3, strides=2, input_shape=self.img_shape, padding="same"))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Conv2D(x*32, kernel_size=3, strides=2, padding="same"))
        model.add(ZeroPadding2D(padding=((0,1),(0,1))))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Conv2D(x*64, kernel_size=3, strides=2, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Conv2D(x*128, kernel_size=3, strides=1, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(Flatten())
        model.add(Dense(1))

        model.summary()

        img = Input(shape=self.img_shape)
        validity = model(img)

        return Model(img, validity)

    def train(self, epochs, batch_size=128, sample_interval=50):

        # Load the dataset
        # (X_train, _), (_, _) = mnist.load_data()
        X_train = polydata

        # Rescale -1 to 1
        # X_train = (X_train.astype(np.float32) - 127.5) / 127.5
        X_train = np.expand_dims(X_train, axis=3)

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = -np.ones((batch_size, 1))

        for epoch in range(epochs):

            self.critic.trainable = True
            for _ in range(self.n_critic):

                # ---------------------
                #  Train Discriminator
                # ---------------------

                # Select a random batch of images
                idx = np.random.randint(0, X_train.shape[0], batch_size)
                imgs = X_train[idx]
                
                # Sample noise as generator input
                noise = np.random.normal(0, 1, (batch_size, self.latent_dim))

                # Generate a batch of new images
                gen_imgs = self.generator.predict(noise)

                # Train the critic
                d_loss_real = self.critic.train_on_batch(imgs, valid)
                d_loss_fake = self.critic.train_on_batch(gen_imgs, fake)
                d_loss = 0.5 * np.add(d_loss_fake, d_loss_real)

                # Clip critic weights
                for l in self.critic.layers:
                    weights = l.get_weights()
                    weights = [np.clip(w, -self.clip_value, self.clip_value) for w in weights]
                    l.set_weights(weights)


            # ---------------------
            #  Train Generator
            # ---------------------

            #self.critic.trainable = False
            g_loss = self.combined.train_on_batch(noise, valid)

            # Plot the progress
            #print(epoch)
            print ("%d [D loss: %f] [G loss: %f]" % (epoch, 1 - d_loss[0], 1 - g_loss))

            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                self.sample_images(epoch)

    def sample_images(self, epoch):
        r, c = 5, 5
        noise = np.random.normal(0, 1, (r * c, self.latent_dim))
        gen_imgs = self.generator.predict(noise)

        # Rescale images 0 - 1
        gen_imgs = 0.5 * gen_imgs + 0.5

        fig, axs = plt.subplots(r, c)
        cnt = 0
        for i in range(r):
            for j in range(c):
                axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
                axs[i,j].axis('off')
                cnt += 1
        fig.savefig("images/wgan_poly_%d.png" % epoch)
        plt.close()


if __name__ == '__main__':
    wgan = WGAN()
wgan.train(epochs=4000, batch_size=32, sample_interval=50)
fig, axes = plt.subplots(2, 8, figsize=(19,5))
for i in range(2):
    for j in range(8):
        out = wgan.generator.predict(np.reshape(np.random.normal(0,1,10),(1,10)))
        axes[i,j].imshow(out[0,:,:,0])
    
fig, axes = plt.subplots(2, 8, figsize=(19,5))
for i in range(2):
    for j in range(8):
        axes[i,j].imshow(polydata[i*j+j])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13426100037608124, 'fraction_numerical': 0.0285821737495299, 'mean_word_length': 2.4408022428294154, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '489617', 'license': 'MIT', 'n_tokens_mistral': 5591, 'n_tokens_neox': 5221, 'n_words': 1194, 'path': '09/.ipynb_checkpoints/SolutionExercise9-checkpoint.ipynb', 'repo_name': 'mu-gaimann/machine-learning-in-fundamental-physics-2019', 'size': '15954'}