Building a discriminator network#
The purpose of the discriminator network is to identify whether a given example is a real one or a fake one.
1raise SystemExit("Stop right there!");
An exception has occurred, use %tb to see the full traceback.
SystemExit: Stop right there!
1# System
2import os
3
4# Mathematical operations and data manipulation
5import numpy as np
6from numpy.random import randn
7
8# Modelling
9from tensorflow.keras.models import Sequential
10from tensorflow.keras.layers import Dense
11
12# Warnings
13import warnings
14
15warnings.filterwarnings("ignore")
1os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
1# Function to generate real samples
2def realdata(location, batch):
3 # location is the random location or mean around which samples are centred
4 # Generating numbers to right of the random point
5 xr = np.arange(location, location + (0.1 * batch / 2), 0.1)
6 xr = xr[0 : int(batch / 2)]
7 # Generating numbers to left of the random point
8 xl = np.arange(location - (0.1 * batch / 2), location, 0.1)
9 # Concatenating both these series
10 x1 = np.concatenate((xl, xr))
11 # Second dependent variable
12 x2 = np.sin(x1)
13 # Reshaping both the variables and then concatenating them to an array
14 # of independent variables
15 x1 = x1.reshape(batch, 1)
16 x2 = x2.reshape(batch, 1)
17 x = np.concatenate((x1, x2), axis=1)
18 # Generating the labels for real data batch which is 'ones'
19 y = np.ones((batch, 1))
20 return x, y
1# Function to generate inputs for generator function
2def fakeinputs(batch, in_feats):
3 # Sampling data points equal to (batch x input feature size) from
4 # a random distribution
5 gen_input = randn(in_feats * batch)
6 # Reshaping the input
7 x = gen_input.reshape(batch, in_feats)
8 return x
1# Function for the generator model
2def model_gen(in_feats, out_feats):
3 # Defining the Generator model
4 genmodel = Sequential()
5 genmodel.add(
6 Dense(
7 32,
8 activation="linear",
9 kernel_initializer="he_uniform",
10 input_dim=in_feats,
11 )
12 )
13 genmodel.add(Dense(32, activation="relu", kernel_initializer="he_uniform"))
14 genmodel.add(Dense(64, activation="elu", kernel_initializer="he_uniform"))
15 genmodel.add(Dense(32, activation="elu", kernel_initializer="he_uniform"))
16 genmodel.add(Dense(32, activation="selu", kernel_initializer="he_uniform"))
17 genmodel.add(Dense(out_feats, activation="selu"))
18 return genmodel
1# Function to create fake samples using the generator model
2def fake_data_generator(genmodel, batch, in_feats):
3 # Generating the inputs to the model
4 gen_inputs = fakeinputs(batch, in_feats)
5 # Using these inputs inside the generator model to generate
6 # fake distribution
7 fake_x = genmodel.predict(gen_inputs)
8 # Generating the labels of fake data batch
9 fake_y = np.zeros((batch, 1))
10
11 return fake_x, fake_y
1# Defining the arguments (batch size,input feature size and output
2# feature size)
3batch_size = 128
4infeats = 10
5outfeats = 2
Next we develop the discriminator model which is a network having 4 layers
1# Defining the discriminator model
2model_disc = Sequential()
3model_disc.add(
4 Dense(
5 16,
6 activation="relu",
7 kernel_initializer="he_uniform",
8 input_dim=outfeats,
9 )
10)
11model_disc.add(Dense(16, activation="relu", kernel_initializer="he_uniform"))
12model_disc.add(Dense(16, activation="relu", kernel_initializer="he_uniform"))
13model_disc.add(Dense(1, activation="sigmoid"))
14# Compiling the model
15model_disc.compile(
16 loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]
17)
1# Print the summary of the discriminator model
2model_disc.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 16) 48
_________________________________________________________________
dense_1 (Dense) (None, 16) 272
_________________________________________________________________
dense_2 (Dense) (None, 16) 272
_________________________________________________________________
dense_3 (Dense) (None, 1) 17
=================================================================
Total params: 609
Trainable params: 609
Non-trainable params: 0
_________________________________________________________________
1# Calling the Generator model function
2model_gen = model_gen(infeats, outfeats)
3model_gen.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_4 (Dense) (None, 32) 352
_________________________________________________________________
dense_5 (Dense) (None, 32) 1056
_________________________________________________________________
dense_6 (Dense) (None, 64) 2112
_________________________________________________________________
dense_7 (Dense) (None, 32) 2080
_________________________________________________________________
dense_8 (Dense) (None, 32) 1056
_________________________________________________________________
dense_9 (Dense) (None, 2) 66
=================================================================
Total params: 6,722
Trainable params: 6,722
Non-trainable params: 0
_________________________________________________________________
1# Defining the number of epochs
2nEpochs = 20000
1# Training the discriminator network
2for i in range(nEpochs):
3 # Generating the random number for generating real samples
4 loc = np.random.normal(3, 1, 1)
5 # Generating samples equal to the bath size from the real distribution
6 x_real, y_real = realdata(loc, batch_size)
7 # Generating fake samples using the fake data generator function
8 x_fake, y_fake = fake_data_generator(model_gen, batch_size, infeats)
9 # Training the discriminator on the real samples
10 model_disc.train_on_batch(x_real, y_real)
11 # Training the discriminator on the fake samples
12 model_disc.train_on_batch(x_fake, y_fake)
13 # Printing the accuracy measures on the real and fake data for every
14 # 4000 epochs
15 if i % 4000 == 0:
16 # Evaluating the real distribution accuracy
17 _, realAccuracy = model_disc.evaluate(x_real, y_real, verbose=0)
18 # Evaluating fake distribution accuracy levels
19 _, fakeAccuracy = model_disc.evaluate(x_fake, y_fake, verbose=0)
20 print(
21 "Real accuracy:{R},Fake accuracy:{F}".format(
22 R=realAccuracy, F=fakeAccuracy
23 )
24 )
Real accuracy:0.0078125,Fake accuracy:1.0
Real accuracy:1.0,Fake accuracy:0.953125
Real accuracy:1.0,Fake accuracy:0.96875
Real accuracy:1.0,Fake accuracy:0.984375
Real accuracy:1.0,Fake accuracy:0.9921875