Skip to content

Commit

Permalink
ch17 so far
Browse files Browse the repository at this point in the history
rasbt committed Nov 4, 2019
1 parent 9e2550b commit 0e96ebf
Showing 5 changed files with 1,600 additions and 1,513 deletions.
1 change: 0 additions & 1 deletion ch17/ch17-optional-DCGAN.ipynb

This file was deleted.

1,289 changes: 1,289 additions & 0 deletions ch17/ch17_optional_DCGAN.ipynb

Large diffs are not rendered by default.

931 changes: 191 additions & 740 deletions ch17/ch17_part1.ipynb

Large diffs are not rendered by default.

36 changes: 25 additions & 11 deletions ch17/ch17_part1.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@


import tensorflow as tf
from google.colab import drive
#from google.colab import drive
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
@@ -15,8 +15,7 @@
#
# Code License: [MIT License](https://github.com/rasbt/python-machine-learning-book-3rd-edition/blob/master/LICENSE.txt)

# Chapter 17: Generative Adversarial Networks (part 1/2)
# =====
# # Chapter 17: Generative Adversarial Networks (Part 1/2)

# Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).

@@ -83,13 +82,18 @@

print("GPU Available:", tf.test.is_gpu_available())

device_name = tf.test.gpu_device_name()
if tf.test.is_gpu_available():
device_name = tf.test.gpu_device_name()

else:
device_name = 'cpu:0'

print(device_name)




drive.mount('/content/drive/')
#drive.mount('/content/drive/')


# ## Implementing the generator and the discriminator networks
@@ -145,7 +149,7 @@ def make_discriminator_network(

image_size = (28, 28)
z_size = 20
mode_z = 'uniform' # 'uniform' vs. 'normal'
mode_z = 'uniform' # 'uniform' vs. 'normal'
gen_hidden_layers = 1
gen_hidden_size = 100
disc_hidden_layers = 1
@@ -213,7 +217,7 @@ def preprocess(ex, mode='uniform'):

mnist_trainset = mnist_trainset.batch(32, drop_remainder=True)
input_z, input_real = next(iter(mnist_trainset))
print('input-z -- shape: ', input_z.shape)
print('input-z -- shape:', input_z.shape)
print('input-real -- shape:', input_real.shape)

g_output = gen_model(input_z)
@@ -250,6 +254,8 @@ def preprocess(ex, mode='uniform'):





num_epochs = 100
batch_size = 64
image_size = (28, 28)
@@ -272,6 +278,7 @@ def preprocess(ex, mode='uniform'):
fixed_z = tf.random.normal(
shape=(batch_size, z_size))


def create_samples(g_model, input_z):
g_output = g_model(input_z, training=False)
images = tf.reshape(g_output, (batch_size, *image_size))
@@ -318,7 +325,7 @@ def create_samples(g_model, input_z):
g_output = gen_model(input_z)
d_logits_fake = disc_model(g_output, training=True)
labels_real = tf.ones_like(d_logits_fake)
g_loss = loss_fn(y_true=labels_real,y_pred=d_logits_fake)
g_loss = loss_fn(y_true=labels_real, y_pred=d_logits_fake)

g_grads = g_tape.gradient(g_loss, gen_model.trainable_variables)
g_optimizer.apply_gradients(
@@ -358,7 +365,7 @@ def create_samples(g_model, input_z):
all_losses.append(epoch_losses)
all_d_vals.append(epoch_d_vals)
print(
'Epoch {:-3d} | ET {:.2f} min | Avg Losses >>'
'Epoch {:03d} | ET {:.2f} min | Avg Losses >>'
' G/D {:.4f}/{:.4f} [D-Real: {:.4f} D-Fake: {:.4f}]'
.format(
epoch, (time.time() - start_time)/60,
@@ -432,7 +439,7 @@ def create_samples(g_model, input_z):
ax2.tick_params(axis='both', which='major', labelsize=15)


plt.savefig('/content/drive/My Drive/Colab Notebooks/PyML-3rd-edition/ch17-gan-learning-curve.pdf')
# plt.savefig('images/ch17-gan-learning-curve.pdf')
plt.show()


@@ -456,10 +463,17 @@ def create_samples(g_model, input_z):
image = epoch_samples[e-1][j]
ax.imshow(image, cmap='gray_r')

plt.savefig('/content/drive/My Drive/Colab Notebooks/PyML-3rd-edition/ch17-vanila-gan-samples.pdf')
#plt.savefig('images/ch17-vanila-gan-samples.pdf')
plt.show()


#
# ----

#
#
# Readers may ignore the next cell.
#



856 changes: 95 additions & 761 deletions ch17/ch17_part2.ipynb

Large diffs are not rendered by default.

0 comments on commit 0e96ebf

Please sign in to comment.