This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_GAN(train_dataset, 500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def train_GAN(dataset, epochs): | |
for epoch in range(epochs): | |
start = time.time() | |
for image_batch in dataset: | |
train_step(image_batch) | |
print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generator = create_generator() | |
discriminator = create_discriminator() | |
@tf.function | |
def train_step(images): | |
noise = tf.random.normal([BATCH_SIZE, noise_dim]) | |
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape: | |
generated_images = generator(noise, training=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
noise_dim = 100 | |
num_of_generated_examples = 16 | |
seed = tf.random.normal([num_of_generated_examples, noise_dim]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generator_optimizer = tf.keras.optimizers.Adam(1e-4) | |
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True) | |
def D_loss(real_output, fake_output): | |
real_loss = cross_entropy(tf.ones_like(real_output), real_output) | |
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output) | |
total_loss = real_loss + fake_loss | |
return total_loss | |
def G_loss(fake_output): | |
return cross_entropy(tf.ones_like(fake_output), fake_output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_discriminator(): | |
model = tf.keras.Sequential() | |
model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=[28, 28, 1])) | |
model.add(layers.LeakyReLU()) | |
model.add(layers.Dropout(0.3)) | |
model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same')) | |
model.add(layers.LeakyReLU()) | |
model.add(layers.Dropout(0.3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_generator(): | |
model = tf.keras.Sequential() | |
# creating Dense layer with units 7*7*256(batch_size) and input_shape of (100,) | |
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,))) | |
model.add(layers.BatchNormalization()) | |
model.add(layers.LeakyReLU()) | |
model.add(layers.Reshape((7, 7, 256))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data() | |
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32') | |
train_images = (train_images - 127.5) / 127.5 | |
train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(60000).batch(256) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import os | |
from tensorflow.keras import layers | |
import time | |
from IPython import display |