<jupyter_start><jupyter_text>WGAN<jupyter_code>from tensorflow.keras.datasets import mnist
(train_X, _), (test_X, _) = mnist.load_data()

print(train_X.shape, test_X.shape)
import numpy as np

train_X = (train_X.astype(np.float32) - 127.5) / 127.5
test_X = (test_X.astype(np.float32) - 127.5) / 127.5

train_X = np.expand_dims(train_X, axis=3)
test_X = np.expand_dims(test_X, axis=3)

print(train_X.shape, test_X.shape)
# Wasserstein Loss
from tensorflow.keras import backend as K

def Wasserstein_loss(y_true, y_pred):

    return K.mean(y_true * y_pred)
    
#######################################################################################

from tensorflow.config.experimental import list_physical_devices, set_memory_growth
gpus = list_physical_devices('GPU')
display(gpus)
if gpus:
  try:
    set_memory_growth(gpus[0], True)
  except RuntimeError as e:
    print(e)
    
#######################################################################################
#critic
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, LeakyReLU, BatchNormalization, Flatten, Input, Dropout
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.metrics import AUC
from tensorflow.keras import regularizers
from tensorflow.keras.backend import clear_session

clear_session()

# YOUR PARAMTERS
#######################################################################################

lr = 0.00005  # learning rate
rr = 1e-5  #regularizer rate

epoch = 30
opt = RMSprop(learning_rate=lr)
metrics = [AUC()]

#######################################################################################

def create_critic(input_dim):
    model = Sequential(name="critic")
    
    # MODEL
    #######################################################################################
    
    model.add(Input(input_dim))
    model.add(Conv2D(kernel_size=3, filters=32, strides=2, padding="same"))
    model.add(BatchNormalization())
    model.add(LeakyReLU())
    model.add(Dropout(.3))
    model.add(Conv2D(kernel_size=3, filters=64, strides=2, padding="same"))
    model.add(BatchNormalization())
    model.add(LeakyReLU())
    model.add(Dropout(.3))
    model.add(Conv2D(kernel_size=3, filters=128, strides=2, padding="same"))
    model.add(BatchNormalization())    
    model.add(LeakyReLU())
    model.add(Dropout(.3))
    model.add(Flatten())
    model.add(Dense(1))

    #######################################################################################
    
    model.summary()
    
    return model

critic = create_critic(train_X.shape[1:])

critic.compile(loss=Wasserstein_loss, optimizer=opt)
critic.trainable = False
#generator
from tensorflow.keras.layers import Conv2DTranspose, Reshape, Activation, UpSampling2D

z_dim = 100

# YOUR PARAMTERS
#######################################################################################


#######################################################################################

def create_generator(z_dim):
    model = Sequential(name="generator")
    # MODEL
    #######################################################################################

    model.add(Dense(128 * 7 * 7, activation="relu", input_dim=100))
    model.add(Reshape((7, 7, 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(1, kernel_size=4, padding="same"))
    model.add(Activation("tanh"))

    #######################################################################################
    
    model.summary()

    return model

generator = create_generator(z_dim)
#assemble

model = Sequential(name="WGAN")
model.add(generator)
model.add(critic)
model.compile(loss=Wasserstein_loss, optimizer=opt)
model.summary()
#sample img
import matplotlib.pyplot as plt

def sample_images(generator, image_grid_rows=4, image_grid_columns=4):
    z = np.random.normal(0, 1, (image_grid_rows * image_grid_columns, z_dim))
    gen_imgs = generator.predict(z)
    gen_imgs = 0.5 * gen_imgs + 0.5
    
    fig, axs = plt.subplots(image_grid_rows, image_grid_columns, figsize=(4, 4), sharey=True, sharex=True)
    
    cnt = 0
    for i in range(image_grid_rows):
        for j in range(image_grid_columns):
            axs[i, j].imshow(gen_imgs[cnt, :, : ,0], cmap='gray')
            axs[i, j].axis('off')
            cnt += 1
    plt.show()
    plt.close()
#train
import numpy as np
d_loss_history = []
g_loss_history = []
def train(iterations, batch_size, sample_interval):
    print("start!!")
    
    real = np.ones((batch_size, 1), dtype=np.float32)
    fake = - real

    for iteration in range(iterations):
        idx = np.random.randint(0, train_X.shape[0], batch_size)
        imgs = train_X[idx]
        for _ in range(5):
            z = np.random.normal(0, 1, (batch_size, 100))
            
            gen_imgs = generator.predict(z)
            
            d_loss_real = critic.train_on_batch(imgs, real)
            d_loss_fake = critic.train_on_batch(gen_imgs, fake)
            d_loss = .5 * np.add(d_loss_real, d_loss_fake)
            for l in critic.layers:
                weights = l.get_weights()
                weights = [np.clip(w, -0.01, 0.01) for w in weights]
                l.set_weights(weights)
        
        z = np.random.normal(0, 1, (batch_size, 100))
        g_loss = model.train_on_batch(z, real) # train generator GAN.train_on_batch()
        
        if (iteration + 1) % sample_interval == 0:
            print("["+str(iteration+1)+":", "d_loss: ", d_loss, ",", "g_loss: ", str(g_loss)+"]")
            sample_images(generator)
iterations = 100000
batch_size = 128
sample_iterval = 2500

train(iterations, batch_size, sample_iterval)<jupyter_output>start!!
[2500: d_loss:  2.3771066480549052e-05 , g_loss:  -8.198957948479801e-05]
{'fraction_non_alphanumeric': 0.25452451464297465, 'fraction_numerical': 0.03043764396182955, 'mean_word_length': 4.313811188811189, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '425892', 'license': 'MIT', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1951, 'n_words': 351, 'path': 'Machine_Learning/tensorflow&keras/WGAN.ipynb', 'repo_name': 'godic97/ILoveVanilaIcecream', 'size': '6078'}