Chapter 24
Chapter 17 - Generative Adversarial Networks for Synthesizing New Data (Part 1/2)
NotebookPython 342 cells
Python Machine Learning 3rd Edition by Sebastian Raschka & Vahid Mirjalili, Packt Publishing Ltd. 2019
Code Repository: https://github.com/rasbt/python-machine-learning-book-3rd-edition
Code License: MIT License
Chapter 17 - Generative Adversarial Networks for Synthesizing New Data (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).
In [1]python · cell 4
python
%load_ext watermark
%watermark -a "Sebastian Raschka & Vahid Mirjalili" -u -d -p numpy,scipy,matplotlib,tensorflow,tensorflow_datasetsOutput
Sebastian Raschka & Vahid Mirjalili last updated: 2019-11-25 numpy 1.17.2 scipy 1.2.1 matplotlib 3.1.0 tensorflow 2.0.0 tensorflow_datasets 1.3.0
In [2]python · cell 5
python
from IPython.display import Image
%matplotlib inlineIntroducing generative adversarial networks
Starting with autoencoders
In [3]python · cell 7
python
Image(filename='images/17_01.png', width=500)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
Generative models for synthesizing new data
In [4]python · cell 9
python
Image(filename='images/17_02.png', width=700)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
Generating new samples with GANs
In [5]python · cell 11
python
Image(filename='images/17_03.png', width=700)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
Understanding the loss functions for the generator and discriminator networks in a GAN model
In [6]python · cell 13
python
Image(filename='images/17_04.png', width=800)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
Implementing a GAN from scratch
Training GAN models on Google Colab
In [7]python · cell 16
python
Image(filename='images/17_05.png', width=700)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
In [8]python · cell 17
python
Image(filename='images/17_06.png', width=600)Output
<IPython.core.display.Image object>
In [9]python · cell 18
python
Image(filename='images/17_07.png', width=600)Output
<IPython.core.display.Image object>
In [10]python · cell 19
python
# Uncomment the following line if running this notebook on Google Colab
#! pip install -q tensorflow-gpu==2.0.0In [11]python · cell 20
python
import tensorflow as tf
print(tf.__version__)
print("GPU Available:", tf.test.is_gpu_available())
if tf.test.is_gpu_available():
device_name = tf.test.gpu_device_name()
else:
device_name = 'cpu:0'
print(device_name)Output
2.0.0 GPU Available: True /device:GPU:0
In [12]python · cell 21
python
#from google.colab import drive
#drive.mount('/content/drive/')Implementing the generator and the discriminator networks
In [13]python · cell 23
python
Image(filename='images/17_08.png', width=600)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
In [13]python · cell 24
python
Image(filename='images/17_17.png', width=600)Output
<IPython.core.display.Image object>
[省略较大 image/png 输出]
In [14]python · cell 25
python
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inlineIn [15]python · cell 26
python
## define a function for the generator:
def make_generator_network(
num_hidden_layers=1,
num_hidden_units=100,
num_output_units=784):
model = tf.keras.Sequential()
for i in range(num_hidden_layers):
model.add(
tf.keras.layers.Dense(
units=num_hidden_units,
use_bias=False)
)
model.add(tf.keras.layers.LeakyReLU())
model.add(tf.keras.layers.Dense(
units=num_output_units, activation='tanh'))
return model
## define a function for the discriminator:
def make_discriminator_network(
num_hidden_layers=1,
num_hidden_units=100,
num_output_units=1):
model = tf.keras.Sequential()
for i in range(num_hidden_layers):
model.add(tf.keras.layers.Dense(units=num_hidden_units))
model.add(tf.keras.layers.LeakyReLU())
model.add(tf.keras.layers.Dropout(rate=0.5))
model.add(
tf.keras.layers.Dense(
units=num_output_units,
activation=None)
)
return modelIn [16]python · cell 27
python
image_size = (28, 28)
z_size = 20
mode_z = 'uniform' # 'uniform' vs. 'normal'
gen_hidden_layers = 1
gen_hidden_size = 100
disc_hidden_layers = 1
disc_hidden_size = 100
tf.random.set_seed(1)
gen_model = make_generator_network(
num_hidden_layers=gen_hidden_layers,
num_hidden_units=gen_hidden_size,
num_output_units=np.prod(image_size))
gen_model.build(input_shape=(None, z_size))
gen_model.summary()Output
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) multiple 2000 _________________________________________________________________ leaky_re_lu (LeakyReLU) multiple 0 _________________________________________________________________ dense_1 (Dense) multiple 79184 ================================================================= Total params: 81,184 Trainable params: 81,184 Non-trainable params: 0 _________________________________________________________________
In [17]python · cell 28
python
disc_model = make_discriminator_network(
num_hidden_layers=disc_hidden_layers,
num_hidden_units=disc_hidden_size)
disc_model.build(input_shape=(None, np.prod(image_size)))
disc_model.summary()Output
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_2 (Dense) multiple 78500 _________________________________________________________________ leaky_re_lu_1 (LeakyReLU) multiple 0 _________________________________________________________________ dropout (Dropout) multiple 0 _________________________________________________________________ dense_3 (Dense) multiple 101 ================================================================= Total params: 78,601 Trainable params: 78,601 Non-trainable params: 0 _________________________________________________________________
Defining the training dataset
In [18]python · cell 30
python
mnist_bldr = tfds.builder('mnist')
mnist_bldr.download_and_prepare()
mnist = mnist_bldr.as_dataset(shuffle_files=False)
def preprocess(ex, mode='uniform'):
image = ex['image']
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.reshape(image, [-1])
image = image*2 - 1.0
if mode == 'uniform':
input_z = tf.random.uniform(
shape=(z_size,), minval=-1.0, maxval=1.0)
elif mode == 'normal':
input_z = tf.random.normal(shape=(z_size,))
return input_z, image
mnist_trainset = mnist['train']
print('Before preprocessing: ')
example = next(iter(mnist_trainset))['image']
print('dtype: ', example.dtype, ' Min: {} Max: {}'.format(np.min(example), np.max(example)))
mnist_trainset = mnist_trainset.map(preprocess)
print('After preprocessing: ')
example = next(iter(mnist_trainset))[0]
print('dtype: ', example.dtype, ' Min: {} Max: {}'.format(np.min(example), np.max(example)))Output
Before preprocessing: dtype: <dtype: 'uint8'> Min: 0 Max: 255 After preprocessing: dtype: <dtype: 'float32'> Min: -0.6264450550079346 Max: 0.9958574771881104
- Step-by-step walk through the data-flow
In [19]python · cell 32
python
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-real -- shape:', input_real.shape)
g_output = gen_model(input_z)
print('Output of G -- shape:', g_output.shape)
d_logits_real = disc_model(input_real)
d_logits_fake = disc_model(g_output)
print('Disc. (real) -- shape:', d_logits_real.shape)
print('Disc. (fake) -- shape:', d_logits_fake.shape)Output
input-z -- shape: (32, 20) input-real -- shape: (32, 784) Output of G -- shape: (32, 784) Disc. (real) -- shape: (32, 1) Disc. (fake) -- shape: (32, 1)
Training the GAN model
In [20]python · cell 34
python
loss_fn = tf.keras.losses.BinaryCrossentropy(from_logits=True)
## Loss for the Generator
g_labels_real = tf.ones_like(d_logits_fake)
g_loss = loss_fn(y_true=g_labels_real, y_pred=d_logits_fake)
print('Generator Loss: {:.4f}'.format(g_loss))
## Loss for the Discriminator
d_labels_real = tf.ones_like(d_logits_real)
d_labels_fake = tf.zeros_like(d_logits_fake)
d_loss_real = loss_fn(y_true=d_labels_real, y_pred=d_logits_real)
d_loss_fake = loss_fn(y_true=d_labels_fake, y_pred=d_logits_fake)
print('Discriminator Losses: Real {:.4f} Fake {:.4f}'
.format(d_loss_real.numpy(), d_loss_fake.numpy()))Output
Generator Loss: 0.7505 Discriminator Losses: Real 1.3683 Fake 0.6434
- Final training
In [21]python · cell 36
python
import time
num_epochs = 100
batch_size = 64
image_size = (28, 28)
z_size = 20
mode_z = 'uniform'
gen_hidden_layers = 1
gen_hidden_size = 100
disc_hidden_layers = 1
disc_hidden_size = 100
tf.random.set_seed(1)
np.random.seed(1)
if mode_z == 'uniform':
fixed_z = tf.random.uniform(
shape=(batch_size, z_size),
minval=-1, maxval=1)
elif mode_z == 'normal':
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))
return (images+1)/2.0
## Set-up the dataset
mnist_trainset = mnist['train']
mnist_trainset = mnist_trainset.map(
lambda ex: preprocess(ex, mode=mode_z))
mnist_trainset = mnist_trainset.shuffle(10000)
mnist_trainset = mnist_trainset.batch(
batch_size, drop_remainder=True)
## Set-up the model
with tf.device(device_name):
gen_model = make_generator_network(
num_hidden_layers=gen_hidden_layers,
num_hidden_units=gen_hidden_size,
num_output_units=np.prod(image_size))
gen_model.build(input_shape=(None, z_size))
disc_model = make_discriminator_network(
num_hidden_layers=disc_hidden_layers,
num_hidden_units=disc_hidden_size)
disc_model.build(input_shape=(None, np.prod(image_size)))
## Loss function and optimizers:
loss_fn = tf.keras.losses.BinaryCrossentropy(from_logits=True)
g_optimizer = tf.keras.optimizers.Adam()
d_optimizer = tf.keras.optimizers.Adam()
all_losses = []
all_d_vals = []
epoch_samples = []
start_time = time.time()
for epoch in range(1, num_epochs+1):
epoch_losses, epoch_d_vals = [], []
for i,(input_z,input_real) in enumerate(mnist_trainset):
## Compute generator's loss
with tf.GradientTape() as g_tape:
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_grads = g_tape.gradient(g_loss, gen_model.trainable_variables)
g_optimizer.apply_gradients(
grads_and_vars=zip(g_grads, gen_model.trainable_variables))
## Compute discriminator's loss
with tf.GradientTape() as d_tape:
d_logits_real = disc_model(input_real, training=True)
d_labels_real = tf.ones_like(d_logits_real)
d_loss_real = loss_fn(
y_true=d_labels_real, y_pred=d_logits_real)
d_logits_fake = disc_model(g_output, training=True)
d_labels_fake = tf.zeros_like(d_logits_fake)
d_loss_fake = loss_fn(
y_true=d_labels_fake, y_pred=d_logits_fake)
d_loss = d_loss_real + d_loss_fake
## Compute the gradients of d_loss
d_grads = d_tape.gradient(d_loss, disc_model.trainable_variables)
## Optimization: Apply the gradients
d_optimizer.apply_gradients(
grads_and_vars=zip(d_grads, disc_model.trainable_variables))
epoch_losses.append(
(g_loss.numpy(), d_loss.numpy(),
d_loss_real.numpy(), d_loss_fake.numpy()))
d_probs_real = tf.reduce_mean(tf.sigmoid(d_logits_real))
d_probs_fake = tf.reduce_mean(tf.sigmoid(d_logits_fake))
epoch_d_vals.append((d_probs_real.numpy(), d_probs_fake.numpy()))
all_losses.append(epoch_losses)
all_d_vals.append(epoch_d_vals)
print(
'Epoch {:03d} | ET {:.2f} min | Avg Losses >>'
' G/D {:.4f}/{:.4f} [D-Real: {:.4f} D-Fake: {:.4f}]'
.format(
epoch, (time.time() - start_time)/60,
*list(np.mean(all_losses[-1], axis=0))))
epoch_samples.append(
create_samples(gen_model, fixed_z).numpy())Output
Epoch 001 | ET 0.77 min | Avg Losses >> G/D 2.9625/0.2842 [D-Real: 0.0306 D-Fake: 0.2535] Epoch 002 | ET 1.42 min | Avg Losses >> G/D 5.2962/0.3157 [D-Real: 0.0986 D-Fake: 0.2172] Epoch 003 | ET 2.03 min | Avg Losses >> G/D 3.4596/0.6666 [D-Real: 0.2970 D-Fake: 0.3696] Epoch 004 | ET 2.64 min | Avg Losses >> G/D 1.9825/0.9088 [D-Real: 0.4667 D-Fake: 0.4421] Epoch 005 | ET 3.25 min | Avg Losses >> G/D 1.9429/0.8474 [D-Real: 0.4584 D-Fake: 0.3891] Epoch 006 | ET 3.96 min | Avg Losses >> G/D 1.8539/0.8880 [D-Real: 0.4798 D-Fake: 0.4082] Epoch 007 | ET 4.73 min | Avg Losses >> G/D 1.5408/0.9731 [D-Real: 0.5391 D-Fake: 0.4340] Epoch 008 | ET 5.49 min | Avg Losses >> G/D 1.6199/0.9666 [D-Real: 0.5217 D-Fake: 0.4449] Epoch 009 | ET 6.26 min | Avg Losses >> G/D 1.3690/1.0835 [D-Real: 0.5827 D-Fake: 0.5009] Epoch 010 | ET 7.02 min | Avg Losses >> G/D 1.3235/1.0702 [D-Real: 0.5786 D-Fake: 0.4915] Epoch 011 | ET 7.78 min | Avg Losses >> G/D 1.4463/1.0575 [D-Real: 0.5648 D-Fake: 0.4928] Epoch 012 | ET 8.53 min | Avg Losses >> G/D 1.2226/1.1532 [D-Real: 0.6028 D-Fake: 0.5504] Epoch 013 | ET 9.30 min | Avg Losses >> G/D 1.2307/1.1206 [D-Real: 0.5924 D-Fake: 0.5282] Epoch 014 | ET 10.06 min | Avg Losses >> G/D 1.3149/1.1634 [D-Real: 0.5974 D-Fake: 0.5660] Epoch 015 | ET 10.82 min | Avg Losses >> G/D 1.1281/1.1878 [D-Real: 0.6190 D-Fake: 0.5688] Epoch 016 | ET 11.59 min | Avg Losses >> G/D 1.1482/1.1823 [D-Real: 0.6118 D-Fake: 0.5705] Epoch 017 | ET 12.35 min | Avg Losses >> G/D 1.2054/1.1597 [D-Real: 0.6010 D-Fake: 0.5588] Epoch 018 | ET 13.11 min | Avg Losses >> G/D 1.2152/1.1362 [D-Real: 0.5912 D-Fake: 0.5449] Epoch 019 | ET 13.86 min | Avg Losses >> G/D 1.1440/1.1703 [D-Real: 0.6047 D-Fake: 0.5656] Epoch 020 | ET 14.61 min | Avg Losses >> G/D 1.1297/1.2049 [D-Real: 0.6177 D-Fake: 0.5872] Epoch 021 | ET 15.28 min | Avg Losses >> G/D 1.2108/1.1807 [D-Real: 0.6016 D-Fake: 0.5791] Epoch 022 | ET 15.89 min | Avg Losses >> G/D 1.1213/1.1990 [D-Real: 0.6170 D-Fake: 0.5820] Epoch 023 | ET 16.49 min | Avg Losses >> G/D 1.0495/1.2237 [D-Real: 0.6253 D-Fake: 0.5984] Epoch 024 | ET 17.10 min | Avg Losses >> G/D 1.0738/1.2265 [D-Real: 0.6219 D-Fake: 0.6046] Epoch 025 | ET 17.82 min | Avg Losses >> G/D 1.1320/1.2220 [D-Real: 0.6197 D-Fake: 0.6023] Epoch 026 | ET 18.57 min | Avg Losses >> G/D 1.0177/1.2491 [D-Real: 0.6344 D-Fake: 0.6147] Epoch 027 | ET 19.33 min | Avg Losses >> G/D 0.9928/1.2344 [D-Real: 0.6304 D-Fake: 0.6039] Epoch 028 | ET 20.09 min | Avg Losses >> G/D 1.0970/1.2159 [D-Real: 0.6178 D-Fake: 0.5981] Epoch 029 | ET 20.86 min | Avg Losses >> G/D 1.0908/1.2253 [D-Real: 0.6201 D-Fake: 0.6052] Epoch 030 | ET 21.62 min | Avg Losses >> G/D 0.9918/1.2525 [D-Real: 0.6342 D-Fake: 0.6183] Epoch 031 | ET 22.38 min | Avg Losses >> G/D 0.9820/1.2639 [D-Real: 0.6388 D-Fake: 0.6251] Epoch 032 | ET 23.13 min | Avg Losses >> G/D 1.0501/1.2503 [D-Real: 0.6306 D-Fake: 0.6197] Epoch 033 | ET 23.89 min | Avg Losses >> G/D 1.0299/1.2498 [D-Real: 0.6329 D-Fake: 0.6169] Epoch 034 | ET 24.65 min | Avg Losses >> G/D 1.0312/1.2610 [D-Real: 0.6353 D-Fake: 0.6257] Epoch 035 | ET 25.42 min | Avg Losses >> G/D 0.9960/1.2615 [D-Real: 0.6358 D-Fake: 0.6257] Epoch 036 | ET 26.17 min | Avg Losses >> G/D 1.0010/1.2712 [D-Real: 0.6406 D-Fake: 0.6306] Epoch 037 | ET 26.93 min | Avg Losses >> G/D 1.0250/1.2698 [D-Real: 0.6393 D-Fake: 0.6305] Epoch 038 | ET 27.69 min | Avg Losses >> G/D 0.9882/1.2692 [D-Real: 0.6398 D-Fake: 0.6294] Epoch 039 | ET 28.44 min | Avg Losses >> G/D 0.9706/1.2833 [D-Real: 0.6463 D-Fake: 0.6370] Epoch 040 | ET 29.15 min | Avg Losses >> G/D 0.9865/1.2805 [D-Real: 0.6427 D-Fake: 0.6378] Epoch 041 | ET 29.77 min | Avg Losses >> G/D 0.9909/1.2746 [D-Real: 0.6429 D-Fake: 0.6317] Epoch 042 | ET 30.38 min | Avg Losses >> G/D 0.9743/1.2907 [D-Real: 0.6491 D-Fake: 0.6416] Epoch 043 | ET 31.00 min | Avg Losses >> G/D 0.9501/1.2898 [D-Real: 0.6486 D-Fake: 0.6412] Epoch 044 | ET 31.67 min | Avg Losses >> G/D 0.9806/1.2939 [D-Real: 0.6505 D-Fake: 0.6434] Epoch 045 | ET 32.43 min | Avg Losses >> G/D 0.9758/1.2872 [D-Real: 0.6464 D-Fake: 0.6407] Epoch 046 | ET 33.19 min | Avg Losses >> G/D 0.9255/1.3094 [D-Real: 0.6588 D-Fake: 0.6506] Epoch 047 | ET 33.95 min | Avg Losses >> G/D 0.9285/1.2908 [D-Real: 0.6519 D-Fake: 0.6389] Epoch 048 | ET 34.72 min | Avg Losses >> G/D 0.9759/1.2930 [D-Real: 0.6494 D-Fake: 0.6436] Epoch 049 | ET 35.47 min | Avg Losses >> G/D 0.9861/1.2875 [D-Real: 0.6450 D-Fake: 0.6426] Epoch 050 | ET 36.24 min | Avg Losses >> G/D 0.9313/1.3039 [D-Real: 0.6543 D-Fake: 0.6495] Epoch 051 | ET 36.99 min | Avg Losses >> G/D 0.9314/1.3115 [D-Real: 0.6574 D-Fake: 0.6541] Epoch 052 | ET 37.75 min | Avg Losses >> G/D 0.9586/1.2949 [D-Real: 0.6503 D-Fake: 0.6446] Epoch 053 | ET 38.51 min | Avg Losses >> G/D 0.9498/1.3026 [D-Real: 0.6549 D-Fake: 0.6477] Epoch 054 | ET 39.27 min | Avg Losses >> G/D 0.9339/1.3172 [D-Real: 0.6642 D-Fake: 0.6530] Epoch 055 | ET 40.04 min | Avg Losses >> G/D 0.9535/1.3029 [D-Real: 0.6517 D-Fake: 0.6513] Epoch 056 | ET 40.81 min | Avg Losses >> G/D 0.9217/1.3032 [D-Real: 0.6570 D-Fake: 0.6462] Epoch 057 | ET 41.57 min | Avg Losses >> G/D 0.9207/1.3090 [D-Real: 0.6568 D-Fake: 0.6521] Epoch 058 | ET 42.33 min | Avg Losses >> G/D 0.9190/1.3031 [D-Real: 0.6559 D-Fake: 0.6472] Epoch 059 | ET 43.05 min | Avg Losses >> G/D 0.9499/1.3037 [D-Real: 0.6500 D-Fake: 0.6537] Epoch 060 | ET 43.66 min | Avg Losses >> G/D 0.9326/1.3047 [D-Real: 0.6537 D-Fake: 0.6511] Epoch 061 | ET 44.27 min | Avg Losses >> G/D 0.9302/1.3034 [D-Real: 0.6557 D-Fake: 0.6477] Epoch 062 | ET 44.87 min | Avg Losses >> G/D 0.9105/1.3099 [D-Real: 0.6557 D-Fake: 0.6542] Epoch 063 | ET 45.53 min | Avg Losses >> G/D 0.9502/1.2975 [D-Real: 0.6500 D-Fake: 0.6474] Epoch 064 | ET 46.29 min | Avg Losses >> G/D 0.8725/1.3231 [D-Real: 0.6665 D-Fake: 0.6565] Epoch 065 | ET 47.06 min | Avg Losses >> G/D 0.9950/1.2893 [D-Real: 0.6447 D-Fake: 0.6446] Epoch 066 | ET 47.81 min | Avg Losses >> G/D 0.9703/1.2966 [D-Real: 0.6504 D-Fake: 0.6462] Epoch 067 | ET 48.57 min | Avg Losses >> G/D 0.9295/1.2927 [D-Real: 0.6520 D-Fake: 0.6407] Epoch 068 | ET 49.34 min | Avg Losses >> G/D 0.9167/1.3033 [D-Real: 0.6564 D-Fake: 0.6469] Epoch 069 | ET 50.10 min | Avg Losses >> G/D 0.9390/1.3113 [D-Real: 0.6555 D-Fake: 0.6558] Epoch 070 | ET 50.86 min | Avg Losses >> G/D 0.9463/1.3003 [D-Real: 0.6522 D-Fake: 0.6481] Epoch 071 | ET 51.62 min | Avg Losses >> G/D 0.9294/1.3045 [D-Real: 0.6555 D-Fake: 0.6490] Epoch 072 | ET 52.38 min | Avg Losses >> G/D 0.9115/1.3154 [D-Real: 0.6591 D-Fake: 0.6563] Epoch 073 | ET 53.15 min | Avg Losses >> G/D 0.9675/1.2961 [D-Real: 0.6498 D-Fake: 0.6463] Epoch 074 | ET 53.90 min | Avg Losses >> G/D 0.9044/1.3074 [D-Real: 0.6557 D-Fake: 0.6518] Epoch 075 | ET 54.66 min | Avg Losses >> G/D 0.9254/1.3098 [D-Real: 0.6575 D-Fake: 0.6524] Epoch 076 | ET 55.42 min | Avg Losses >> G/D 0.9347/1.3059 [D-Real: 0.6555 D-Fake: 0.6504] Epoch 077 | ET 56.17 min | Avg Losses >> G/D 0.9129/1.3121 [D-Real: 0.6588 D-Fake: 0.6533] Epoch 078 | ET 56.92 min | Avg Losses >> G/D 0.9344/1.3089 [D-Real: 0.6570 D-Fake: 0.6519] Epoch 079 | ET 57.55 min | Avg Losses >> G/D 0.9319/1.3035 [D-Real: 0.6547 D-Fake: 0.6488] Epoch 080 | ET 58.16 min | Avg Losses >> G/D 0.8852/1.3210 [D-Real: 0.6649 D-Fake: 0.6561] Epoch 081 | ET 58.78 min | Avg Losses >> G/D 0.9776/1.3050 [D-Real: 0.6527 D-Fake: 0.6523] Epoch 082 | ET 59.43 min | Avg Losses >> G/D 0.9420/1.2934 [D-Real: 0.6499 D-Fake: 0.6435] Epoch 083 | ET 60.19 min | Avg Losses >> G/D 0.8800/1.3181 [D-Real: 0.6615 D-Fake: 0.6566] Epoch 084 | ET 60.96 min | Avg Losses >> G/D 0.9154/1.3188 [D-Real: 0.6617 D-Fake: 0.6570] Epoch 085 | ET 61.71 min | Avg Losses >> G/D 0.9219/1.3081 [D-Real: 0.6563 D-Fake: 0.6518] Epoch 086 | ET 62.47 min | Avg Losses >> G/D 0.9325/1.3093 [D-Real: 0.6570 D-Fake: 0.6523] Epoch 087 | ET 63.23 min | Avg Losses >> G/D 0.9212/1.3078 [D-Real: 0.6557 D-Fake: 0.6522] Epoch 088 | ET 63.99 min | Avg Losses >> G/D 0.9335/1.3156 [D-Real: 0.6586 D-Fake: 0.6570] Epoch 089 | ET 64.75 min | Avg Losses >> G/D 0.9054/1.3182 [D-Real: 0.6627 D-Fake: 0.6555] Epoch 090 | ET 65.51 min | Avg Losses >> G/D 0.8793/1.3228 [D-Real: 0.6638 D-Fake: 0.6589] Epoch 091 | ET 66.25 min | Avg Losses >> G/D 0.9549/1.3111 [D-Real: 0.6544 D-Fake: 0.6567] Epoch 092 | ET 67.00 min | Avg Losses >> G/D 0.8979/1.3190 [D-Real: 0.6639 D-Fake: 0.6552] Epoch 093 | ET 67.75 min | Avg Losses >> G/D 0.9285/1.3128 [D-Real: 0.6602 D-Fake: 0.6526] Epoch 094 | ET 68.52 min | Avg Losses >> G/D 0.9199/1.3065 [D-Real: 0.6563 D-Fake: 0.6502] Epoch 095 | ET 69.27 min | Avg Losses >> G/D 0.8917/1.3259 [D-Real: 0.6661 D-Fake: 0.6598] Epoch 096 | ET 70.03 min | Avg Losses >> G/D 0.9548/1.3101 [D-Real: 0.6551 D-Fake: 0.6549] Epoch 097 | ET 70.79 min | Avg Losses >> G/D 0.9128/1.3179 [D-Real: 0.6610 D-Fake: 0.6568] Epoch 098 | ET 71.40 min | Avg Losses >> G/D 0.9017/1.3157 [D-Real: 0.6608 D-Fake: 0.6549] Epoch 099 | ET 72.02 min | Avg Losses >> G/D 0.9581/1.3021 [D-Real: 0.6540 D-Fake: 0.6481] Epoch 100 | ET 72.63 min | Avg Losses >> G/D 0.8991/1.3120 [D-Real: 0.6593 D-Fake: 0.6527]
In [22]python · cell 37
python
#import pickle
# pickle.dump({'all_losses':all_losses,
# 'all_d_vals':all_d_vals,
# 'samples':epoch_samples},
# open('/content/drive/My Drive/Colab Notebooks/PyML-3rd-edition/ch17-vanila-learning.pkl', 'wb'))
#gen_model.save('/content/drive/My Drive/Colab Notebooks/PyML-3rd-edition/ch17-vanila-gan_gen.h5')
#disc_model.save('/content/drive/My Drive/Colab Notebooks/PyML-3rd-edition/ch17-vanila-gan_disc.h5')In [23]python · cell 38
python
import itertools
fig = plt.figure(figsize=(16, 6))
## Plotting the losses
ax = fig.add_subplot(1, 2, 1)
g_losses = [item[0] for item in itertools.chain(*all_losses)]
d_losses = [item[1]/2.0 for item in itertools.chain(*all_losses)]
plt.plot(g_losses, label='Generator loss', alpha=0.95)
plt.plot(d_losses, label='Discriminator loss', alpha=0.95)
plt.legend(fontsize=20)
ax.set_xlabel('Iteration', size=15)
ax.set_ylabel('Loss', size=15)
epochs = np.arange(1, 101)
epoch2iter = lambda e: e*len(all_losses[-1])
epoch_ticks = [1, 20, 40, 60, 80, 100]
newpos = [epoch2iter(e) for e in epoch_ticks]
ax2 = ax.twiny()
ax2.set_xticks(newpos)
ax2.set_xticklabels(epoch_ticks)
ax2.xaxis.set_ticks_position('bottom')
ax2.xaxis.set_label_position('bottom')
ax2.spines['bottom'].set_position(('outward', 60))
ax2.set_xlabel('Epoch', size=15)
ax2.set_xlim(ax.get_xlim())
ax.tick_params(axis='both', which='major', labelsize=15)
ax2.tick_params(axis='both', which='major', labelsize=15)
## Plotting the outputs of the discriminator
ax = fig.add_subplot(1, 2, 2)
d_vals_real = [item[0] for item in itertools.chain(*all_d_vals)]
d_vals_fake = [item[1] for item in itertools.chain(*all_d_vals)]
plt.plot(d_vals_real, alpha=0.75, label=r'Real: $D(\mathbf{x})$')
plt.plot(d_vals_fake, alpha=0.75, label=r'Fake: $D(G(\mathbf{z}))$')
plt.legend(fontsize=20)
ax.set_xlabel('Iteration', size=15)
ax.set_ylabel('Discriminator output', size=15)
ax2 = ax.twiny()
ax2.set_xticks(newpos)
ax2.set_xticklabels(epoch_ticks)
ax2.xaxis.set_ticks_position('bottom')
ax2.xaxis.set_label_position('bottom')
ax2.spines['bottom'].set_position(('outward', 60))
ax2.set_xlabel('Epoch', size=15)
ax2.set_xlim(ax.get_xlim())
ax.tick_params(axis='both', which='major', labelsize=15)
ax2.tick_params(axis='both', which='major', labelsize=15)
#plt.savefig('images/ch17-gan-learning-curve.pdf')
plt.show()Output
<Figure size 1152x432 with 4 Axes>
In [24]python · cell 39
python
selected_epochs = [1, 2, 4, 10, 50, 100]
fig = plt.figure(figsize=(10, 14))
for i,e in enumerate(selected_epochs):
for j in range(5):
ax = fig.add_subplot(6, 5, i*5+j+1)
ax.set_xticks([])
ax.set_yticks([])
if j == 0:
ax.text(
-0.06, 0.5, 'Epoch {}'.format(e),
rotation=90, size=18, color='red',
horizontalalignment='right',
verticalalignment='center',
transform=ax.transAxes)
image = epoch_samples[e-1][j]
ax.imshow(image, cmap='gray_r')
#plt.savefig('images/ch17-vanila-gan-samples.pdf')
plt.show()Output
<Figure size 720x1008 with 30 Axes>
[省略较大 image/png 输出]
Readers may ignore the next cell.
In [25]python · cell 42
python
! python ../.convert_notebook_to_script.py --input ch17_part1.ipynb --output ch17_part1.pyOutput
[NbConvertApp] Converting notebook ch17_part1.ipynb to script [NbConvertApp] Writing 13583 bytes to ch17_part1.py
