#### A-Simple-GAN

H
```{
"cells": [
{
"cell_type": "markdown",
"source": [
"# G.A.N\n",
"> -  The main focus for GAN is to generate data from scratch.\n",
"> -  It brings us closer to understanding intelligence.\n",
"- It trains two deep networks, called Generator and Discriminator, that compete and cooperate with each other. In the course of training, both networks eventually learn how to perform their tasks.\n",
"> > - The generator never actually sees examples from the domain and is adapted based on how well the discriminator performs."
]
},
{
"cell_type": "markdown",
"source": [
"[Original Article: How to Develop a 1D GAN](https://machinelearningmastery.com/how-to-develop-a-generative-adversarial-network-for-a-1-dimensional-function-from-scratch-in-keras/)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"# The Import Statements:\n",
"from matplotlib import pyplot\n",
"import numpy as np\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"from keras.utils.vis_utils import plot_model"
]
},
{
"cell_type": "markdown",
"source": [
"### Defining a 1 D function:\n",
"> y=f(x)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"def function_1D(x):\n",
"    return x*x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"inputs=np.arange(-0.5,0.6,0.1)\n",
"outputs=[function_1D(x) for x in inputs]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# plot the result\n",
"pyplot.plot(inputs, outputs)\n",
"pyplot.show()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [],
"source": [
"#Defining random values\n",
"def generate_samples(n=100):\n",
"    x1=np.random.rand(n)-0.5\n",
"    x2=x1*x1\n",
"    x1=x1.reshape(n,1)\n",
"    x2=x2.reshape(n,1)\n",
"    return np.hstack((x1,x2))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# generate samples and plotting them\n",
"data = generate_samples()\n",
"\n",
"pyplot.scatter(data[:, 0], data[:, 1])\n",
"pyplot.show()"
]
},
{
"cell_type": "markdown",
"source": [
"# Note:\n",
"> *a sample is comprised of a vector with two elements, one for the input and one for the output of our one-dimensional function.*"
]
},
{
"cell_type": "markdown",
"source": [
"### The Discriminator\n",
"- The difference from a typical CNN is the absence of max-pooling in between layers.\n",
"- will have 1 hidden layer with 25 nodes.\n",
"- will use the ReLU activation function\n",
"- The output layer will have 1 node for the binary classification using the sigmoid activation function.\n",
"- Loss Function: Binary Cross Entropy\n",
]
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [],
"source": [
"#Code for the Discriminator Unit:\n",
"def define_discriminator(n_inputs=2):\n",
"\tmodel = Sequential()\n",
"\t# compile model\n",
"\treturn model"
]
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential_1\"\n",
"_________________________________________________________________\n",
"Layer (type)                 Output Shape              Param #   \n",
"=================================================================\n",
"dense_1 (Dense)              (None, 25)                75        \n",
"_________________________________________________________________\n",
"dense_2 (Dense)              (None, 1)                 26        \n",
"=================================================================\n",
"Total params: 101\n",
"Trainable params: 101\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# define the discriminator model\n",
"model = define_discriminator()\n",
"# summarize the model\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [],
"source": [
"def generate_real_samples(n):\n",
"    x1=np.random.rand(n)-0.5\n",
"    x2=x1*x1\n",
"    x1=x1.reshape(n,1)\n",
"    x2=x2.reshape(n,1)\n",
"    X= np.hstack((x1,x2))\n",
"    y=np.ones((n,1))\n",
"    return X,y"
]
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"def generate_fake_samples(n):\n",
"\t# generate inputs in [-1, 1]\n",
"\tX1 = -1 + np.random.rand(n) * 2\n",
"\t# generate outputs in [-1, 1]\n",
"\tX2 = -1 + np.random.rand(n) * 2\n",
"\t# stack arrays\n",
"\tX1 = X1.reshape(n, 1)\n",
"\tX2 = X2.reshape(n, 1)\n",
"\tX = np.hstack((X1, X2))\n",
"\t# generate class labels\n",
"\ty = np.zeros((n, 1))\n",
"\treturn X, y"
]
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [],
"source": [
"#training the discriminator model\n",
"def train_discriminator(model, n_epochs=1000, n_batch=128):\n",
"\thalf_batch = int(n_batch / 2)\n",
"\t# run epochs manually\n",
"\tfor i in range(n_epochs):\n",
"\t\t# generate real examples\n",
"\t\tX_real, y_real = generate_real_samples(half_batch)\n",
"\t\t# update model\n",
"\t\tmodel.train_on_batch(X_real, y_real)\n",
"\t\t# generate fake examples\n",
"\t\tX_fake, y_fake = generate_fake_samples(half_batch)\n",
"\t\t# update model\n",
"\t\tmodel.train_on_batch(X_fake, y_fake)\n",
"\t\t# evaluate the model\n",
"\t\t_, acc_real = model.evaluate(X_real, y_real, verbose=0)\n",
"\t\t_, acc_fake = model.evaluate(X_fake, y_fake, verbose=0)\n",
"\t\tprint(i, acc_real, acc_fake)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0.5 0.15625\n",
"1 0.546875 0.25\n",
"2 0.578125 0.21875\n",
"3 0.5625 0.21875\n",
"4 0.640625 0.234375\n",
"5 0.515625 0.40625\n",
"6 0.515625 0.46875\n",
"7 0.59375 0.390625\n",
"8 0.5625 0.46875\n",
"9 0.53125 0.4375\n",
"10 0.46875 0.484375\n",
"11 0.421875 0.53125\n",
"12 0.46875 0.484375\n",
"13 0.515625 0.5625\n",
"14 0.46875 0.6875\n",
"15 0.5625 0.515625\n",
"16 0.515625 0.53125\n",
"17 0.53125 0.5\n",
"18 0.484375 0.609375\n",
"19 0.40625 0.5\n",
"20 0.46875 0.671875\n",
"21 0.484375 0.59375\n",
"22 0.546875 0.609375\n",
"23 0.375 0.53125\n",
"24 0.421875 0.53125\n",
"25 0.34375 0.65625\n",
"26 0.421875 0.484375\n",
"27 0.421875 0.578125\n",
"28 0.328125 0.546875\n",
"29 0.359375 0.640625\n",
"30 0.4375 0.609375\n",
"31 0.296875 0.484375\n",
"32 0.296875 0.53125\n",
"33 0.515625 0.625\n",
"34 0.359375 0.59375\n",
"35 0.40625 0.65625\n",
"36 0.359375 0.6875\n",
"37 0.28125 0.578125\n",
"38 0.328125 0.578125\n",
"39 0.328125 0.671875\n",
"40 0.40625 0.671875\n",
"41 0.296875 0.515625\n",
"42 0.296875 0.578125\n",
"43 0.296875 0.625\n",
"44 0.34375 0.625\n",
"45 0.390625 0.671875\n",
"46 0.375 0.609375\n",
"47 0.34375 0.59375\n",
"48 0.359375 0.578125\n",
"49 0.359375 0.6875\n",
"50 0.34375 0.65625\n",
"51 0.375 0.515625\n",
"52 0.390625 0.609375\n",
"53 0.234375 0.625\n",
"54 0.359375 0.625\n",
"55 0.234375 0.59375\n",
"56 0.40625 0.6875\n",
"57 0.34375 0.765625\n",
"58 0.296875 0.609375\n",
"59 0.265625 0.59375\n",
"60 0.25 0.65625\n",
"61 0.390625 0.65625\n",
"62 0.265625 0.65625\n",
"63 0.46875 0.703125\n",
"64 0.40625 0.59375\n",
"65 0.359375 0.625\n",
"66 0.28125 0.625\n",
"67 0.3125 0.625\n",
"68 0.328125 0.640625\n",
"69 0.390625 0.625\n",
"70 0.28125 0.640625\n",
"71 0.328125 0.609375\n",
"72 0.296875 0.59375\n",
"73 0.125 0.703125\n",
"74 0.359375 0.765625\n",
"75 0.359375 0.65625\n",
"76 0.34375 0.5625\n",
"77 0.234375 0.671875\n",
"78 0.34375 0.6875\n",
"79 0.21875 0.734375\n",
"80 0.3125 0.78125\n",
"81 0.28125 0.6875\n",
"82 0.34375 0.640625\n",
"83 0.375 0.765625\n",
"84 0.34375 0.640625\n",
"85 0.328125 0.703125\n",
"86 0.21875 0.75\n",
"87 0.25 0.65625\n",
"88 0.28125 0.640625\n",
"89 0.3125 0.78125\n",
"90 0.234375 0.71875\n",
"91 0.328125 0.71875\n",
"92 0.25 0.671875\n",
"93 0.25 0.703125\n",
"94 0.265625 0.546875\n",
"95 0.296875 0.546875\n",
"96 0.328125 0.625\n",
"97 0.265625 0.75\n",
"98 0.296875 0.671875\n",
"99 0.265625 0.703125\n",
"100 0.15625 0.734375\n",
"101 0.234375 0.796875\n",
"102 0.25 0.71875\n",
"103 0.21875 0.828125\n",
"104 0.21875 0.828125\n",
"105 0.328125 0.765625\n",
"106 0.21875 0.671875\n",
"107 0.28125 0.84375\n",
"108 0.21875 0.796875\n",
"109 0.21875 0.78125\n",
"110 0.25 0.65625\n",
"111 0.25 0.8125\n",
"112 0.21875 0.859375\n",
"113 0.28125 0.796875\n",
"114 0.234375 0.625\n",
"115 0.21875 0.671875\n",
"116 0.234375 0.859375\n",
"117 0.265625 0.71875\n",
"118 0.21875 0.765625\n",
"119 0.234375 0.765625\n",
"120 0.234375 0.6875\n",
"121 0.234375 0.8125\n",
"122 0.296875 0.828125\n",
"123 0.25 0.796875\n",
"124 0.234375 0.890625\n",
"125 0.359375 0.765625\n",
"126 0.203125 0.75\n",
"127 0.328125 0.703125\n",
"128 0.203125 0.78125\n",
"129 0.265625 0.796875\n",
"130 0.3125 0.828125\n",
"131 0.21875 0.828125\n",
"132 0.25 0.828125\n",
"133 0.296875 0.84375\n",
"134 0.203125 0.875\n",
"135 0.34375 0.734375\n",
"136 0.421875 0.765625\n",
"137 0.15625 0.796875\n",
"138 0.359375 0.75\n",
"139 0.234375 0.75\n",
"140 0.421875 0.71875\n",
"141 0.28125 0.703125\n",
"142 0.1875 0.6875\n",
"143 0.21875 0.828125\n",
"144 0.25 0.890625\n",
"145 0.265625 0.9375\n",
"146 0.171875 0.8125\n",
"147 0.265625 0.84375\n",
"148 0.296875 0.90625\n",
"149 0.171875 0.875\n",
"150 0.21875 0.765625\n",
"151 0.25 0.875\n",
"152 0.25 0.90625\n",
"153 0.25 0.90625\n",
"154 0.328125 0.796875\n",
"155 0.296875 0.921875\n",
"156 0.234375 0.84375\n",
"157 0.25 0.875\n",
"158 0.1875 0.9375\n",
"159 0.234375 0.859375\n",
"160 0.234375 0.890625\n",
"161 0.28125 0.984375\n",
"162 0.21875 0.90625\n",
"163 0.21875 0.90625\n",
"164 0.265625 0.953125\n",
"165 0.28125 0.953125\n",
"166 0.328125 0.984375\n",
"167 0.28125 0.96875\n",
"168 0.25 0.953125\n",
"169 0.359375 0.921875\n",
"170 0.34375 1.0\n",
"171 0.328125 0.953125\n",
"172 0.296875 0.96875\n",
"173 0.359375 0.921875\n",
"174 0.28125 0.9375\n",
"175 0.375 0.984375\n",
"176 0.359375 0.96875\n",
"177 0.265625 0.984375\n",
"178 0.21875 0.96875\n",
"179 0.28125 0.9375\n",
"180 0.359375 1.0\n",
"181 0.34375 0.984375\n",
"182 0.40625 0.984375\n",
"183 0.296875 0.953125\n",
"184 0.34375 0.984375\n",
"185 0.3125 0.984375\n",
"186 0.359375 0.96875\n",
"187 0.375 0.984375\n",
"188 0.53125 0.953125\n",
"189 0.40625 0.921875\n",
"190 0.453125 0.953125\n",
"191 0.375 0.96875\n",
"192 0.609375 0.9375\n",
"193 0.40625 0.96875\n",
"194 0.4375 0.96875\n",
"195 0.390625 0.96875\n",
"196 0.40625 0.984375\n",
"197 0.4375 1.0\n",
"198 0.546875 0.953125\n",
"199 0.328125 1.0\n",
"200 0.328125 0.984375\n",
"201 0.328125 0.984375\n",
"202 0.453125 0.96875\n",
"203 0.34375 0.96875\n",
"204 0.34375 0.984375\n",
"205 0.359375 0.96875\n",
"206 0.359375 0.984375\n",
"207 0.421875 0.96875\n",
"208 0.5 0.984375\n",
"209 0.40625 1.0\n",
"210 0.390625 0.96875\n",
"211 0.546875 0.96875\n",
"212 0.390625 0.984375\n",
"213 0.5 0.921875\n",
"214 0.578125 0.96875\n",
"215 0.671875 0.9375\n",
"216 0.484375 0.984375\n",
"217 0.4375 0.90625\n",
"218 0.453125 0.96875\n",
"219 0.421875 0.953125\n",
"220 0.625 0.9375\n",
"221 0.5 0.953125\n",
"222 0.578125 0.984375\n",
"223 0.53125 0.9375\n",
"224 0.5625 0.984375\n",
"225 0.546875 0.984375\n",
"226 0.5625 0.96875\n",
"227 0.578125 0.984375\n",
"228 0.5625 0.953125\n",
"229 0.546875 0.90625\n",
"230 0.546875 0.921875\n",
"231 0.75 1.0\n",
"232 0.546875 1.0\n",
"233 0.671875 1.0\n",
"234 0.5625 0.96875\n",
"235 0.640625 0.984375\n",
"236 0.6875 0.921875\n",
"237 0.59375 0.90625\n",
"238 0.71875 0.921875\n",
"239 0.609375 1.0\n",
"240 0.59375 0.953125\n",
"241 0.65625 0.953125\n",
"242 0.640625 0.984375\n",
"243 0.6875 0.984375\n",
"244 0.546875 0.9375\n",
"245 0.71875 1.0\n",
"246 0.71875 0.953125\n",
"247 0.609375 1.0\n",
"248 0.671875 0.96875\n",
"249 0.625 0.953125\n",
"250 0.53125 0.921875\n",
"251 0.6875 0.984375\n",
"252 0.625 0.921875\n",
"253 0.65625 0.984375\n",
"254 0.625 0.9375\n",
"255 0.75 0.96875\n",
"256 0.609375 0.984375\n",
"257 0.640625 0.921875\n",
"258 0.6875 0.984375\n",
"259 0.671875 1.0\n",
"260 0.625 0.9375\n",
"261 0.65625 0.984375\n",
"262 0.703125 0.96875\n",
"263 0.734375 1.0\n",
"264 0.65625 0.984375\n",
"265 0.578125 0.9375\n",
"266 0.59375 0.96875\n",
"267 0.703125 0.921875\n",
"268 0.734375 1.0\n",
"269 0.734375 1.0\n",
"270 0.703125 0.984375\n",
"271 0.734375 0.984375\n",
"272 0.6875 0.953125\n",
"273 0.703125 0.96875\n",
"274 0.71875 1.0\n",
"275 0.65625 0.953125\n",
"276 0.75 0.921875\n",
"277 0.609375 0.984375\n",
"278 0.6875 1.0\n",
"279 0.6875 0.953125\n",
"280 0.765625 1.0\n",
"281 0.796875 1.0\n",
"282 0.734375 0.96875\n",
"283 0.671875 0.984375\n",
"284 0.578125 1.0\n",
"285 0.671875 0.953125\n",
"286 0.671875 0.984375\n",
"287 0.75 0.9375\n",
"288 0.734375 0.984375\n",
"289 0.71875 0.96875\n",
"290 0.75 0.96875\n",
"291 0.6875 0.984375\n",
"292 0.65625 0.9375\n",
"293 0.734375 0.984375\n",
"294 0.65625 0.953125\n",
"295 0.703125 0.96875\n",
"296 0.6875 0.984375\n",
"297 0.71875 0.984375\n",
"298 0.734375 0.953125\n",
"299 0.71875 0.9375\n",
"300 0.734375 0.984375\n",
"301 0.78125 0.953125\n",
"302 0.75 0.9375\n",
"303 0.75 0.953125\n",
"304 0.796875 0.96875\n",
"305 0.734375 1.0\n",
"306 0.71875 0.921875\n",
"307 0.796875 0.953125\n",
"308 0.84375 0.96875\n",
"309 0.75 0.96875\n",
"310 0.71875 1.0\n",
"311 0.828125 0.96875\n",
"312 0.8125 0.984375\n",
"313 0.78125 0.9375\n",
"314 0.84375 0.96875\n",
"315 0.6875 0.984375\n",
"316 0.84375 0.953125\n",
"317 0.8125 0.984375\n",
"318 0.765625 0.9375\n",
"319 0.71875 0.9375\n",
"320 0.703125 0.9375\n",
"321 0.671875 0.96875\n",
"322 0.703125 0.921875\n",
"323 0.765625 0.96875\n",
"324 0.703125 0.953125\n",
"325 0.828125 0.96875\n",
"326 0.828125 0.96875\n",
"327 0.78125 0.984375\n",
"328 0.8125 0.953125\n",
"329 0.796875 0.984375\n",
"330 0.765625 0.9375\n",
"331 0.890625 0.90625\n",
"332 0.8125 0.90625\n",
"333 0.8125 1.0\n",
"334 0.78125 0.96875\n",
"335 0.875 0.96875\n",
"336 0.84375 0.984375\n",
"337 0.796875 1.0\n",
"338 0.8125 0.984375\n",
"339 0.78125 0.9375\n",
"340 0.875 0.953125\n",
"341 0.8125 0.921875\n",
"342 0.796875 0.90625\n",
"343 0.796875 0.953125\n",
"344 0.8125 0.984375\n",
"345 0.890625 0.9375\n",
"346 0.828125 0.9375\n",
"347 0.78125 0.96875\n",
"348 0.90625 0.953125\n",
"349 0.828125 0.96875\n",
"350 0.796875 0.953125\n",
"351 0.734375 0.921875\n",
"352 0.796875 0.984375\n",
"353 0.765625 0.921875\n",
"354 0.8125 0.90625\n",
"355 0.765625 0.96875\n",
"356 0.84375 0.96875\n",
"357 0.875 0.953125\n",
"358 0.8125 0.90625\n",
"359 0.828125 0.96875\n",
"360 0.828125 0.953125\n",
"361 0.84375 0.984375\n",
"362 0.859375 0.9375\n",
"363 0.859375 0.953125\n",
"364 0.859375 0.9375\n",
"365 0.875 0.96875\n",
"366 0.875 0.953125\n",
"367 0.8125 0.953125\n",
"368 0.90625 0.9375\n",
"369 0.875 0.984375\n",
"370 0.9375 0.890625\n",
"371 0.875 0.9375\n",
"372 0.796875 0.921875\n",
"373 0.828125 0.921875\n",
"374 0.90625 0.921875\n",
"375 0.8125 0.9375\n",
"376 0.8125 0.90625\n",
"377 0.90625 0.921875\n",
"378 0.765625 1.0\n",
"379 0.78125 0.953125\n",
"380 0.78125 0.953125\n",
"381 0.875 0.953125\n",
"382 0.875 0.9375\n",
"383 0.84375 0.890625\n",
"384 0.796875 0.9375\n",
"385 0.859375 0.96875\n",
"386 0.8125 0.96875\n",
"387 0.90625 0.953125\n",
"388 0.921875 0.9375\n",
"389 0.890625 0.953125\n",
"390 0.96875 0.921875\n",
"391 0.84375 0.875\n",
"392 0.875 0.921875\n",
"393 0.828125 0.9375\n",
"394 0.921875 0.9375\n",
"395 0.890625 0.9375\n",
"396 0.921875 0.921875\n",
"397 0.890625 1.0\n",
"398 0.875 0.984375\n",
"399 0.921875 0.9375\n",
"400 0.828125 0.953125\n",
"401 0.828125 0.90625\n",
"402 0.9375 0.953125\n",
"403 0.8125 0.890625\n",
"404 0.953125 0.953125\n",
"405 0.9375 0.890625\n",
"406 0.90625 0.96875\n",
"407 0.890625 0.984375\n",
"408 0.875 0.9375\n",
"409 0.921875 0.984375\n",
"410 0.859375 0.96875\n",
"411 0.890625 0.953125\n",
"412 0.921875 0.96875\n",
"413 0.921875 1.0\n",
"414 0.921875 0.96875\n",
"415 0.875 0.90625\n",
"416 0.96875 0.96875\n",
"417 0.890625 0.921875\n",
"418 0.90625 0.953125\n",
"419 0.875 0.9375\n",
"420 0.875 0.921875\n",
"421 0.96875 0.953125\n",
"422 0.921875 0.96875\n",
"423 0.890625 0.875\n",
"424 0.84375 0.90625\n",
"425 0.921875 0.984375\n",
"426 0.953125 0.890625\n",
"427 1.0 0.90625\n",
"428 0.890625 0.9375\n",
"429 0.921875 0.9375\n",
"430 0.9375 0.953125\n",
"431 0.96875 0.953125\n",
"432 0.90625 0.9375\n",
"433 0.953125 0.875\n",
"434 0.953125 0.890625\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"435 0.96875 0.90625\n",
"436 0.9375 0.953125\n",
"437 0.96875 0.953125\n",
"438 0.953125 0.890625\n",
"439 0.921875 0.953125\n",
"440 0.953125 0.90625\n",
"441 0.90625 0.984375\n",
"442 0.96875 0.96875\n",
"443 0.984375 0.9375\n",
"444 0.921875 0.9375\n",
"445 0.96875 0.90625\n",
"446 0.921875 0.96875\n",
"447 0.890625 0.875\n",
"448 0.953125 0.953125\n",
"449 0.984375 0.953125\n",
"450 0.921875 0.953125\n",
"451 1.0 0.90625\n",
"452 0.984375 0.96875\n",
"453 1.0 0.90625\n",
"454 0.953125 0.953125\n",
"455 0.96875 0.9375\n",
"456 0.953125 0.90625\n",
"457 1.0 0.921875\n",
"458 0.984375 0.96875\n",
"459 0.984375 0.90625\n",
"460 0.984375 0.984375\n",
"461 1.0 0.921875\n",
"462 0.984375 0.953125\n",
"463 0.96875 0.921875\n",
"464 1.0 0.921875\n",
"465 0.984375 0.859375\n",
"466 1.0 0.90625\n",
"467 1.0 0.890625\n",
"468 1.0 0.90625\n",
"469 1.0 0.875\n",
"470 1.0 0.84375\n",
"471 1.0 0.875\n",
"472 0.984375 0.859375\n",
"473 1.0 0.859375\n",
"474 1.0 0.921875\n",
"475 0.984375 0.96875\n",
"476 1.0 0.875\n",
"477 1.0 0.890625\n",
"478 1.0 0.921875\n",
"479 1.0 0.890625\n",
"480 1.0 0.953125\n",
"481 1.0 0.90625\n",
"482 1.0 0.890625\n",
"483 1.0 0.90625\n",
"484 1.0 0.90625\n",
"485 1.0 0.890625\n",
"486 1.0 0.921875\n",
"487 1.0 0.96875\n",
"488 1.0 0.9375\n",
"489 1.0 0.921875\n",
"490 1.0 0.890625\n",
"491 1.0 0.890625\n",
"492 1.0 0.9375\n",
"493 1.0 0.90625\n",
"494 0.984375 0.9375\n",
"495 1.0 0.875\n",
"496 1.0 0.890625\n",
"497 1.0 0.9375\n",
"498 1.0 0.921875\n",
"499 0.984375 0.921875\n",
"500 1.0 0.921875\n",
"501 1.0 0.875\n",
"502 1.0 0.90625\n",
"503 1.0 0.953125\n",
"504 1.0 0.921875\n",
"505 1.0 0.96875\n",
"506 1.0 0.953125\n",
"507 1.0 0.921875\n",
"508 1.0 0.921875\n",
"509 1.0 0.890625\n",
"510 1.0 0.96875\n",
"511 1.0 0.921875\n",
"512 1.0 0.921875\n",
"513 1.0 0.859375\n",
"514 1.0 0.875\n",
"515 1.0 0.921875\n",
"516 1.0 0.9375\n",
"517 1.0 0.890625\n",
"518 1.0 0.90625\n",
"519 1.0 0.875\n",
"520 1.0 0.90625\n",
"521 1.0 0.9375\n",
"522 1.0 0.921875\n",
"523 1.0 0.875\n",
"524 1.0 0.875\n",
"525 1.0 0.921875\n",
"526 1.0 0.953125\n",
"527 1.0 0.90625\n",
"528 1.0 0.90625\n",
"529 1.0 0.90625\n",
"530 1.0 0.890625\n",
"531 1.0 0.90625\n",
"532 1.0 0.921875\n",
"533 1.0 0.921875\n",
"534 1.0 0.953125\n",
"535 1.0 0.875\n",
"536 1.0 0.90625\n",
"537 1.0 0.859375\n",
"538 1.0 0.890625\n",
"539 1.0 0.9375\n",
"540 1.0 0.9375\n",
"541 1.0 0.953125\n",
"542 1.0 0.890625\n",
"543 1.0 0.90625\n",
"544 1.0 0.84375\n",
"545 1.0 0.890625\n",
"546 1.0 0.890625\n",
"547 1.0 0.953125\n",
"548 1.0 0.9375\n",
"549 1.0 0.9375\n",
"550 1.0 0.890625\n",
"551 1.0 0.96875\n",
"552 1.0 0.921875\n",
"553 1.0 0.890625\n",
"554 1.0 0.90625\n",
"555 1.0 0.96875\n",
"556 1.0 0.921875\n",
"557 1.0 0.953125\n",
"558 1.0 0.890625\n",
"559 1.0 0.9375\n",
"560 1.0 0.9375\n",
"561 1.0 0.90625\n",
"562 1.0 0.9375\n",
"563 1.0 0.953125\n",
"564 1.0 0.875\n",
"565 1.0 0.90625\n",
"566 1.0 0.859375\n",
"567 1.0 0.875\n",
"568 1.0 0.921875\n",
"569 1.0 0.84375\n",
"570 1.0 0.890625\n",
"571 1.0 0.890625\n",
"572 1.0 0.953125\n",
"573 1.0 0.90625\n",
"574 1.0 0.921875\n",
"575 1.0 0.921875\n",
"576 1.0 0.921875\n",
"577 1.0 0.859375\n",
"578 1.0 0.9375\n",
"579 1.0 0.953125\n",
"580 1.0 0.921875\n",
"581 1.0 0.875\n",
"582 1.0 0.875\n",
"583 1.0 0.875\n",
"584 1.0 0.921875\n",
"585 1.0 0.890625\n",
"586 1.0 0.890625\n",
"587 1.0 0.890625\n",
"588 1.0 0.90625\n",
"589 1.0 0.875\n",
"590 1.0 0.90625\n",
"591 1.0 0.859375\n",
"592 1.0 0.90625\n",
"593 1.0 0.84375\n",
"594 1.0 0.859375\n",
"595 1.0 0.890625\n",
"596 1.0 0.90625\n",
"597 1.0 0.875\n",
"598 1.0 0.953125\n",
"599 1.0 0.875\n",
"600 1.0 0.859375\n",
"601 1.0 0.9375\n",
"602 1.0 0.875\n",
"603 1.0 0.859375\n",
"604 1.0 0.90625\n",
"605 1.0 0.90625\n",
"606 1.0 0.78125\n",
"607 1.0 0.828125\n",
"608 1.0 0.9375\n",
"609 1.0 0.921875\n",
"610 1.0 0.90625\n",
"611 1.0 0.953125\n",
"612 1.0 0.890625\n",
"613 1.0 0.9375\n",
"614 1.0 0.921875\n",
"615 1.0 0.796875\n",
"616 1.0 0.890625\n",
"617 1.0 0.921875\n",
"618 1.0 0.90625\n",
"619 1.0 0.890625\n",
"620 1.0 0.859375\n",
"621 1.0 0.921875\n",
"622 1.0 0.828125\n",
"623 1.0 0.875\n",
"624 1.0 0.953125\n",
"625 1.0 0.84375\n",
"626 1.0 0.9375\n",
"627 1.0 0.9375\n",
"628 1.0 0.859375\n",
"629 1.0 0.9375\n",
"630 1.0 0.90625\n",
"631 1.0 0.9375\n",
"632 1.0 0.921875\n",
"633 1.0 0.90625\n",
"634 1.0 0.90625\n",
"635 1.0 0.890625\n",
"636 1.0 0.78125\n",
"637 1.0 0.90625\n",
"638 1.0 0.890625\n",
"639 1.0 0.921875\n",
"640 1.0 0.75\n",
"641 1.0 0.875\n",
"642 1.0 0.875\n",
"643 1.0 0.90625\n",
"644 1.0 0.953125\n",
"645 1.0 0.921875\n",
"646 1.0 0.890625\n",
"647 1.0 0.859375\n",
"648 1.0 0.84375\n",
"649 1.0 0.90625\n",
"650 1.0 0.84375\n",
"651 1.0 0.84375\n",
"652 1.0 0.921875\n",
"653 1.0 0.90625\n",
"654 1.0 0.921875\n",
"655 1.0 0.921875\n",
"656 1.0 0.921875\n",
"657 1.0 0.84375\n",
"658 1.0 0.84375\n",
"659 1.0 0.90625\n",
"660 1.0 0.953125\n",
"661 1.0 0.890625\n",
"662 1.0 0.875\n",
"663 1.0 0.84375\n",
"664 1.0 0.84375\n",
"665 1.0 0.8125\n",
"666 1.0 0.859375\n",
"667 1.0 0.90625\n",
"668 1.0 0.90625\n",
"669 1.0 0.875\n",
"670 1.0 0.921875\n",
"671 1.0 0.84375\n",
"672 1.0 0.953125\n",
"673 1.0 0.84375\n",
"674 1.0 0.8125\n",
"675 1.0 0.84375\n",
"676 1.0 0.921875\n",
"677 1.0 0.953125\n",
"678 1.0 0.84375\n",
"679 1.0 0.859375\n",
"680 1.0 0.859375\n",
"681 1.0 0.90625\n",
"682 1.0 0.90625\n",
"683 1.0 0.90625\n",
"684 1.0 0.828125\n",
"685 1.0 0.875\n",
"686 1.0 0.9375\n",
"687 1.0 0.859375\n",
"688 1.0 0.859375\n",
"689 1.0 0.84375\n",
"690 1.0 0.953125\n",
"691 1.0 0.84375\n",
"692 1.0 0.859375\n",
"693 1.0 0.9375\n",
"694 1.0 0.890625\n",
"695 1.0 0.859375\n",
"696 1.0 0.875\n",
"697 1.0 0.875\n",
"698 1.0 0.9375\n",
"699 1.0 0.890625\n",
"700 1.0 0.921875\n",
"701 1.0 0.828125\n",
"702 1.0 0.84375\n",
"703 1.0 0.90625\n",
"704 1.0 0.859375\n",
"705 1.0 0.90625\n",
"706 1.0 0.921875\n",
"707 1.0 0.9375\n",
"708 1.0 0.84375\n",
"709 1.0 0.875\n",
"710 1.0 0.90625\n",
"711 1.0 0.90625\n",
"712 1.0 0.890625\n",
"713 1.0 0.921875\n",
"714 1.0 0.90625\n",
"715 1.0 0.890625\n",
"716 1.0 0.8125\n",
"717 1.0 0.875\n",
"718 1.0 0.890625\n",
"719 1.0 0.890625\n",
"720 1.0 0.90625\n",
"721 1.0 0.875\n",
"722 1.0 0.84375\n",
"723 1.0 0.9375\n",
"724 1.0 0.90625\n",
"725 1.0 0.84375\n",
"726 1.0 0.9375\n",
"727 1.0 0.875\n",
"728 1.0 0.8125\n",
"729 1.0 0.84375\n",
"730 1.0 0.9375\n",
"731 1.0 0.84375\n",
"732 1.0 0.875\n",
"733 1.0 0.921875\n",
"734 1.0 0.78125\n",
"735 1.0 0.78125\n",
"736 1.0 0.859375\n",
"737 1.0 0.84375\n",
"738 1.0 0.90625\n",
"739 1.0 0.859375\n",
"740 1.0 0.875\n",
"741 1.0 0.859375\n",
"742 1.0 0.890625\n",
"743 1.0 0.875\n",
"744 1.0 0.890625\n",
"745 1.0 0.859375\n",
"746 1.0 0.8125\n",
"747 1.0 0.859375\n",
"748 1.0 0.953125\n",
"749 1.0 0.84375\n",
"750 1.0 0.890625\n",
"751 1.0 0.796875\n",
"752 1.0 0.890625\n",
"753 1.0 0.796875\n",
"754 1.0 0.84375\n",
"755 1.0 0.890625\n",
"756 1.0 0.875\n",
"757 1.0 0.890625\n",
"758 1.0 0.921875\n",
"759 1.0 0.84375\n",
"760 1.0 0.875\n",
"761 1.0 0.859375\n",
"762 1.0 0.953125\n",
"763 1.0 0.890625\n",
"764 1.0 0.859375\n",
"765 1.0 0.84375\n",
"766 1.0 0.828125\n",
"767 1.0 0.8125\n",
"768 1.0 0.84375\n",
"769 1.0 0.859375\n",
"770 1.0 0.875\n",
"771 1.0 0.890625\n",
"772 1.0 0.90625\n",
"773 1.0 0.90625\n",
"774 1.0 0.953125\n",
"775 1.0 0.90625\n",
"776 1.0 0.890625\n",
"777 1.0 0.890625\n",
"778 1.0 0.921875\n",
"779 1.0 0.90625\n",
"780 1.0 0.90625\n",
"781 1.0 0.90625\n",
"782 1.0 0.796875\n",
"783 1.0 0.765625\n",
"784 1.0 0.921875\n",
"785 1.0 0.875\n",
"786 1.0 0.890625\n",
"787 1.0 0.90625\n",
"788 1.0 0.90625\n",
"789 1.0 0.890625\n",
"790 1.0 0.859375\n",
"791 1.0 0.890625\n",
"792 1.0 0.875\n",
"793 1.0 0.796875\n",
"794 1.0 0.859375\n",
"795 1.0 0.828125\n",
"796 1.0 0.890625\n",
"797 1.0 0.875\n",
"798 1.0 0.890625\n",
"799 1.0 0.8125\n",
"800 1.0 0.875\n",
"801 1.0 0.921875\n",
"802 1.0 0.90625\n",
"803 1.0 0.890625\n",
"804 1.0 0.90625\n",
"805 1.0 0.890625\n",
"806 1.0 0.90625\n",
"807 1.0 0.9375\n",
"808 1.0 0.90625\n",
"809 1.0 0.90625\n",
"810 1.0 0.859375\n",
"811 1.0 0.859375\n",
"812 1.0 0.890625\n",
"813 1.0 0.875\n",
"814 1.0 0.8125\n",
"815 1.0 0.875\n",
"816 1.0 0.921875\n",
"817 1.0 0.9375\n",
"818 1.0 0.890625\n",
"819 1.0 0.90625\n",
"820 1.0 0.921875\n",
"821 1.0 0.875\n",
"822 1.0 0.9375\n",
"823 1.0 0.90625\n",
"824 1.0 0.875\n",
"825 1.0 0.84375\n",
"826 1.0 0.875\n",
"827 1.0 0.8125\n",
"828 1.0 0.8125\n",
"829 1.0 0.890625\n",
"830 1.0 0.78125\n",
"831 1.0 0.890625\n",
"832 1.0 0.890625\n",
"833 1.0 0.84375\n",
"834 1.0 0.859375\n",
"835 1.0 0.875\n",
"836 1.0 0.890625\n",
"837 1.0 0.9375\n",
"838 1.0 0.953125\n",
"839 1.0 0.875\n",
"840 1.0 0.890625\n",
"841 1.0 0.78125\n",
"842 1.0 0.921875\n",
"843 1.0 0.875\n",
"844 1.0 0.84375\n",
"845 1.0 0.859375\n",
"846 1.0 0.828125\n",
"847 1.0 0.8125\n",
"848 1.0 0.921875\n",
"849 1.0 0.890625\n",
"850 1.0 0.84375\n",
"851 1.0 0.96875\n",
"852 1.0 0.90625\n",
"853 1.0 0.9375\n",
"854 1.0 0.875\n",
"855 1.0 0.890625\n",
"856 1.0 0.859375\n",
"857 1.0 0.890625\n",
"858 1.0 0.8125\n",
"859 1.0 0.875\n",
"860 1.0 0.875\n",
"861 1.0 0.875\n",
"862 1.0 0.859375\n",
"863 1.0 0.875\n",
"864 1.0 0.9375\n",
"865 1.0 0.796875\n",
"866 1.0 0.84375\n",
"867 1.0 0.90625\n",
"868 1.0 0.875\n",
"869 1.0 0.890625\n",
"870 1.0 0.953125\n",
"871 1.0 0.890625\n",
"872 1.0 0.90625\n",
"873 1.0 0.828125\n",
"874 1.0 0.96875\n",
"875 1.0 0.90625\n",
"876 1.0 0.84375\n",
"877 1.0 0.765625\n",
"878 1.0 0.96875\n",
"879 1.0 0.890625\n",
"880 1.0 0.890625\n",
"881 1.0 0.84375\n",
"882 1.0 0.890625\n",
"883 1.0 0.84375\n",
"884 1.0 0.859375\n",
"885 1.0 0.84375\n",
"886 1.0 0.859375\n",
"887 1.0 0.875\n",
"888 1.0 0.921875\n",
"889 1.0 0.90625\n",
"890 1.0 0.90625\n",
"891 1.0 0.859375\n",
"892 1.0 0.859375\n",
"893 1.0 0.9375\n",
"894 1.0 0.84375\n",
"895 1.0 0.859375\n",
"896 1.0 0.90625\n",
"897 1.0 0.9375\n",
"898 1.0 0.84375\n",
"899 1.0 0.890625\n",
"900 1.0 0.828125\n",
"901 1.0 0.90625\n",
"902 1.0 0.90625\n",
"903 1.0 0.859375\n",
"904 1.0 0.875\n",
"905 1.0 0.84375\n",
"906 1.0 0.875\n",
"907 1.0 0.875\n",
"908 1.0 0.859375\n",
"909 1.0 0.875\n",
"910 1.0 0.9375\n",
"911 1.0 0.796875\n",
"912 1.0 0.90625\n",
"913 1.0 0.859375\n",
"914 1.0 0.9375\n",
"915 1.0 0.875\n",
"916 1.0 0.859375\n",
"917 1.0 0.890625\n",
"918 1.0 0.890625\n",
"919 1.0 0.875\n",
"920 1.0 0.953125\n",
"921 1.0 0.921875\n",
"922 1.0 0.921875\n",
"923 1.0 0.890625\n",
"924 1.0 0.9375\n",
"925 1.0 0.859375\n",
"926 1.0 0.90625\n",
"927 1.0 0.921875\n",
"928 1.0 0.859375\n",
"929 1.0 0.859375\n",
"930 1.0 0.875\n",
"931 1.0 0.890625\n",
"932 1.0 0.890625\n",
"933 1.0 0.859375\n",
"934 1.0 0.859375\n",
"935 1.0 0.828125\n",
"936 1.0 0.96875\n",
"937 1.0 0.875\n",
"938 1.0 0.75\n",
"939 1.0 0.9375\n",
"940 1.0 0.828125\n",
"941 1.0 0.890625\n",
"942 1.0 0.828125\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"943 1.0 0.859375\n",
"944 1.0 0.875\n",
"945 1.0 0.921875\n",
"946 1.0 0.890625\n",
"947 1.0 0.921875\n",
"948 1.0 0.9375\n",
"949 1.0 0.953125\n",
"950 1.0 0.875\n",
"951 1.0 0.859375\n",
"952 1.0 0.890625\n",
"953 1.0 0.84375\n",
"954 1.0 0.875\n",
"955 1.0 0.875\n",
"956 1.0 0.90625\n",
"957 1.0 0.84375\n",
"958 1.0 0.828125\n",
"959 1.0 0.75\n",
"960 1.0 0.890625\n",
"961 1.0 0.875\n",
"962 1.0 0.921875\n",
"963 1.0 0.796875\n",
"964 1.0 0.875\n",
"965 1.0 0.90625\n",
"966 1.0 0.921875\n",
"967 1.0 0.796875\n",
"968 1.0 0.875\n",
"969 1.0 0.921875\n",
"970 1.0 0.828125\n",
"971 1.0 0.9375\n",
"972 1.0 0.890625\n",
"973 1.0 0.921875\n",
"974 1.0 0.875\n",
"975 1.0 0.9375\n",
"976 1.0 0.90625\n",
"977 1.0 0.953125\n",
"978 1.0 0.859375\n",
"979 1.0 0.828125\n",
"980 1.0 0.9375\n",
"981 1.0 0.953125\n",
"982 1.0 0.90625\n",
"983 1.0 0.828125\n",
"984 1.0 0.78125\n",
"985 1.0 0.90625\n",
"986 1.0 0.921875\n",
"987 1.0 0.90625\n",
"988 1.0 0.921875\n",
"989 1.0 0.890625\n",
"990 1.0 0.90625\n",
"991 1.0 0.890625\n",
"992 1.0 0.78125\n",
"993 1.0 0.859375\n",
"994 1.0 0.765625\n",
"995 1.0 0.8125\n",
"996 1.0 0.890625\n",
"997 1.0 0.890625\n",
"998 1.0 0.875\n",
"999 1.0 0.84375\n"
]
}
],
"source": [
"# define the discriminator model\n",
"model = define_discriminator()\n",
"# fit the model\n",
"train_discriminator(model)"
]
},
{
"cell_type": "markdown",
"source": [
"\"*The goal is to train a generator model, not a discriminator model, and that is where the complexity of GANs truly lies.*\" "
]
},
{
"cell_type": "markdown",
"source": [
"# The Generator Model\n",
"*We will define a small latent space of five dimensions and use the standard approach in the GAN literature of using a Gaussian distribution for each variable in the latent space. We will generate new inputs by drawing random numbers from a standard Gaussian distribution, i.e. mean of zero and a standard deviation of one.*\n",
"\n",
"* Specs:\n",
"> * Single Hidden Layer with 5 nodes\n",
"> * ReLU activation Function\n",
"> * He weight initialization\n",
"> * Output layer will have 2 nodes+ will use linear activation function"
]
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [],
"source": [
"# define the generator model unit\n",
"def define_generator(latent_dim, n_outputs=2):\n",
"\tmodel = Sequential()\n",
"\treturn model"
]
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential_3\"\n",
"_________________________________________________________________\n",
"Layer (type)                 Output Shape              Param #   \n",
"=================================================================\n",
"dense_5 (Dense)              (None, 15)                90        \n",
"_________________________________________________________________\n",
"dense_6 (Dense)              (None, 2)                 32        \n",
"=================================================================\n",
"Total params: 122\n",
"Trainable params: 122\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# define the discriminator model\n",
"model = define_generator(5)\n",
"# summarize the model\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"outputs": [],
"source": [
"# generate points in latent space as input for the generator\n",
"def generate_latent_points(latent_dim, n):\n",
"\t# generate points in the latent space\n",
"\tx_input = np.random.randn(latent_dim * n)\n",
"\t# reshape into a batch of inputs for the network\n",
"\tx_input = x_input.reshape(n, latent_dim)\n",
"\treturn x_input"
]
},
{
"cell_type": "code",
"execution_count": 21,
"outputs": [],
"source": [
"# use the generator to generate n fake examples and plot the results\n",
"def generate_fake_samples(generator, latent_dim, n):\n",
"\t# generate points in latent space\n",
"\tx_input = generate_latent_points(latent_dim, n)\n",
"\t# predict outputs\n",
"\tX = generator.predict(x_input)\n",
"    # create class labels\n",
"\ty = np.zeros((n, 1))\n",
"\treturn X, y"
]
},
{
"cell_type": "markdown",
"source": [
"*When the discriminator is good at detecting fake samples, the generator is updated more, and when the discriminator model is relatively poor or confused when detecting fake samples, the generator model is updated less.*"
]
},
{
"cell_type": "code",
"execution_count": 17,
"outputs": [],
"source": [
"# define the combined generator and discriminator model, for updating the generator\n",
"def define_gan(generator, discriminator):\n",
"\t# make weights in the discriminator not trainable\n",
"\tdiscriminator.trainable = False\n",
"\t# connect them\n",
"\tmodel = Sequential()\n",
"\t# compile model\n",
"\treturn model"
]
},
{
"cell_type": "code",
"execution_count": 23,
"outputs": [],
"source": [
"# train the generator and discriminator\n",
"def train(g_model, d_model, gan_model, latent_dim, n_epochs=10000, n_batch=128, n_eval=2000):\n",
"\t# determine half the size of one batch, for updating the discriminator\n",
"\thalf_batch = int(n_batch / 2)\n",
"\t# manually enumerate epochs\n",
"\tfor i in range(n_epochs):\n",
"\t\t# prepare real samples\n",
"\t\tx_real, y_real = generate_real_samples(half_batch)\n",
"\t\t# prepare fake examples\n",
"\t\tx_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)\n",
"\t\t# update discriminator\n",
"\t\td_model.train_on_batch(x_real, y_real)\n",
"\t\td_model.train_on_batch(x_fake, y_fake)\n",
"\t\t# prepare points in latent space as input for the generator\n",
"\t\tx_gan = generate_latent_points(latent_dim, n_batch)\n",
"\t\t# create inverted labels for the fake samples\n",
"\t\ty_gan = np.ones((n_batch, 1))\n",
"\t\t# update the generator via the discriminator's error\n",
"\t\tgan_model.train_on_batch(x_gan, y_gan)\n",
"\t\t# evaluate the model every n_eval epochs\n",
"\t\tif (i+1) % n_eval == 0:\n",
"\t\t\tsummarize_performance(i, g_model, d_model, latent_dim)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"outputs": [],
"source": [
"# evaluate the discriminator and plot real and fake points\n",
"def summarize_performance(epoch, generator, discriminator, latent_dim, n=100):\n",
"\t# prepare real samples\n",
"\tx_real, y_real = generate_real_samples(n)\n",
"\t# evaluate discriminator on real examples\n",
"\t_, acc_real = discriminator.evaluate(x_real, y_real, verbose=0)\n",
"\t# prepare fake examples\n",
"\tx_fake, y_fake = generate_fake_samples(generator, latent_dim, n)\n",
"\t# evaluate discriminator on fake examples\n",
"\t_, acc_fake = discriminator.evaluate(x_fake, y_fake, verbose=0)\n",
"\t# summarize discriminator performance\n",
"\tprint(epoch, acc_real, acc_fake)\n",
"\t# scatter plot real and fake data points\n",
"\tpyplot.scatter(x_real[:, 0], x_real[:, 1], color='red')\n",
"\tpyplot.scatter(x_fake[:, 0], x_fake[:, 1], color='blue')\n",
"\tpyplot.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1999 0.7400000095367432 0.4399999976158142\n"
]
},
{
"data": {
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# size of the latent space\n",
"latent_dim = 5\n",
"# create the discriminator\n",
"discriminator = define_discriminator()\n",
"# create the generator\n",
"generator = define_generator(latent_dim)\n",
"# create the gan\n",
"gan_model = define_gan(generator, discriminator)\n",
"# train model\n",
"train(generator, discriminator, gan_model, latent_dim)"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": []
}
],
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
```

# G.A.N

• The main focus for GAN is to generate data from scratch.
• It brings us closer to understanding intelligence.
• It trains two deep networks, called Generator and Discriminator, that compete and cooperate with each other. In the course of training, both networks eventually learn how to perform their tasks.
• The generator never actually sees examples from the domain and is adapted based on how well the discriminator performs.
``````# The Import Statements:
from matplotlib import pyplot
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model``````
```Using TensorFlow backend.
```

### Defining a 1 D function:

y=f(x)

``````def function_1D(x):
return x*x``````
``````inputs=np.arange(-0.5,0.6,0.1)
outputs=[function_1D(x) for x in inputs]``````
``````# plot the result
pyplot.plot(inputs, outputs)
pyplot.show()``````
``````#Defining random values
def generate_samples(n=100):
x1=np.random.rand(n)-0.5
x2=x1*x1
x1=x1.reshape(n,1)
x2=x2.reshape(n,1)
return np.hstack((x1,x2))``````
``````# generate samples and plotting them
data = generate_samples()

pyplot.scatter(data[:, 0], data[:, 1])
pyplot.show()``````

# Note:

a sample is comprised of a vector with two elements, one for the input and one for the output of our one-dimensional function.

### The Discriminator

• The difference from a typical CNN is the absence of max-pooling in between layers.
• will have 1 hidden layer with 25 nodes.
• will use the ReLU activation function
• The output layer will have 1 node for the binary classification using the sigmoid activation function.
• Loss Function: Binary Cross Entropy
``````#Code for the Discriminator Unit:
def define_discriminator(n_inputs=2):
model = Sequential()
# compile model
return model``````
``````# define the discriminator model
model = define_discriminator()
# summarize the model
model.summary()``````
```Model: &quot;sequential_1&quot;
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense_1 (Dense)              (None, 25)                75
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 26
=================================================================
Total params: 101
Trainable params: 101
Non-trainable params: 0
_________________________________________________________________
```
``````def generate_real_samples(n):
x1=np.random.rand(n)-0.5
x2=x1*x1
x1=x1.reshape(n,1)
x2=x2.reshape(n,1)
X= np.hstack((x1,x2))
y=np.ones((n,1))
return X,y``````
``````def generate_fake_samples(n):
# generate inputs in [-1, 1]
X1 = -1 + np.random.rand(n) * 2
# generate outputs in [-1, 1]
X2 = -1 + np.random.rand(n) * 2
# stack arrays
X1 = X1.reshape(n, 1)
X2 = X2.reshape(n, 1)
X = np.hstack((X1, X2))
# generate class labels
y = np.zeros((n, 1))
return X, y``````
``````#training the discriminator model
def train_discriminator(model, n_epochs=1000, n_batch=128):
half_batch = int(n_batch / 2)
# run epochs manually
for i in range(n_epochs):
# generate real examples
X_real, y_real = generate_real_samples(half_batch)
# update model
model.train_on_batch(X_real, y_real)
# generate fake examples
X_fake, y_fake = generate_fake_samples(half_batch)
# update model
model.train_on_batch(X_fake, y_fake)
# evaluate the model
_, acc_real = model.evaluate(X_real, y_real, verbose=0)
_, acc_fake = model.evaluate(X_fake, y_fake, verbose=0)
print(i, acc_real, acc_fake)``````
``````# define the discriminator model
model = define_discriminator()
# fit the model
train_discriminator(model)``````
```0 0.5 0.15625
1 0.546875 0.25
2 0.578125 0.21875
3 0.5625 0.21875
4 0.640625 0.234375
5 0.515625 0.40625
6 0.515625 0.46875
7 0.59375 0.390625
8 0.5625 0.46875
9 0.53125 0.4375
10 0.46875 0.484375
11 0.421875 0.53125
12 0.46875 0.484375
13 0.515625 0.5625
14 0.46875 0.6875
15 0.5625 0.515625
16 0.515625 0.53125
17 0.53125 0.5
18 0.484375 0.609375
19 0.40625 0.5
20 0.46875 0.671875
21 0.484375 0.59375
22 0.546875 0.609375
23 0.375 0.53125
24 0.421875 0.53125
25 0.34375 0.65625
26 0.421875 0.484375
27 0.421875 0.578125
28 0.328125 0.546875
29 0.359375 0.640625
30 0.4375 0.609375
31 0.296875 0.484375
32 0.296875 0.53125
33 0.515625 0.625
34 0.359375 0.59375
35 0.40625 0.65625
36 0.359375 0.6875
37 0.28125 0.578125
38 0.328125 0.578125
39 0.328125 0.671875
40 0.40625 0.671875
41 0.296875 0.515625
42 0.296875 0.578125
43 0.296875 0.625
44 0.34375 0.625
45 0.390625 0.671875
46 0.375 0.609375
47 0.34375 0.59375
48 0.359375 0.578125
49 0.359375 0.6875
50 0.34375 0.65625
51 0.375 0.515625
52 0.390625 0.609375
53 0.234375 0.625
54 0.359375 0.625
55 0.234375 0.59375
56 0.40625 0.6875
57 0.34375 0.765625
58 0.296875 0.609375
59 0.265625 0.59375
60 0.25 0.65625
61 0.390625 0.65625
62 0.265625 0.65625
63 0.46875 0.703125
64 0.40625 0.59375
65 0.359375 0.625
66 0.28125 0.625
67 0.3125 0.625
68 0.328125 0.640625
69 0.390625 0.625
70 0.28125 0.640625
71 0.328125 0.609375
72 0.296875 0.59375
73 0.125 0.703125
74 0.359375 0.765625
75 0.359375 0.65625
76 0.34375 0.5625
77 0.234375 0.671875
78 0.34375 0.6875
79 0.21875 0.734375
80 0.3125 0.78125
81 0.28125 0.6875
82 0.34375 0.640625
83 0.375 0.765625
84 0.34375 0.640625
85 0.328125 0.703125
86 0.21875 0.75
87 0.25 0.65625
88 0.28125 0.640625
89 0.3125 0.78125
90 0.234375 0.71875
91 0.328125 0.71875
92 0.25 0.671875
93 0.25 0.703125
94 0.265625 0.546875
95 0.296875 0.546875
96 0.328125 0.625
97 0.265625 0.75
98 0.296875 0.671875
99 0.265625 0.703125
100 0.15625 0.734375
101 0.234375 0.796875
102 0.25 0.71875
103 0.21875 0.828125
104 0.21875 0.828125
105 0.328125 0.765625
106 0.21875 0.671875
107 0.28125 0.84375
108 0.21875 0.796875
109 0.21875 0.78125
110 0.25 0.65625
111 0.25 0.8125
112 0.21875 0.859375
113 0.28125 0.796875
114 0.234375 0.625
115 0.21875 0.671875
116 0.234375 0.859375
117 0.265625 0.71875
118 0.21875 0.765625
119 0.234375 0.765625
120 0.234375 0.6875
121 0.234375 0.8125
122 0.296875 0.828125
123 0.25 0.796875
124 0.234375 0.890625
125 0.359375 0.765625
126 0.203125 0.75
127 0.328125 0.703125
128 0.203125 0.78125
129 0.265625 0.796875
130 0.3125 0.828125
131 0.21875 0.828125
132 0.25 0.828125
133 0.296875 0.84375
134 0.203125 0.875
135 0.34375 0.734375
136 0.421875 0.765625
137 0.15625 0.796875
138 0.359375 0.75
139 0.234375 0.75
140 0.421875 0.71875
141 0.28125 0.703125
142 0.1875 0.6875
143 0.21875 0.828125
144 0.25 0.890625
145 0.265625 0.9375
146 0.171875 0.8125
147 0.265625 0.84375
148 0.296875 0.90625
149 0.171875 0.875
150 0.21875 0.765625
151 0.25 0.875
152 0.25 0.90625
153 0.25 0.90625
154 0.328125 0.796875
155 0.296875 0.921875
156 0.234375 0.84375
157 0.25 0.875
158 0.1875 0.9375
159 0.234375 0.859375
160 0.234375 0.890625
161 0.28125 0.984375
162 0.21875 0.90625
163 0.21875 0.90625
164 0.265625 0.953125
165 0.28125 0.953125
166 0.328125 0.984375
167 0.28125 0.96875
168 0.25 0.953125
169 0.359375 0.921875
170 0.34375 1.0
171 0.328125 0.953125
172 0.296875 0.96875
173 0.359375 0.921875
174 0.28125 0.9375
175 0.375 0.984375
176 0.359375 0.96875
177 0.265625 0.984375
178 0.21875 0.96875
179 0.28125 0.9375
180 0.359375 1.0
181 0.34375 0.984375
182 0.40625 0.984375
183 0.296875 0.953125
184 0.34375 0.984375
185 0.3125 0.984375
186 0.359375 0.96875
187 0.375 0.984375
188 0.53125 0.953125
189 0.40625 0.921875
190 0.453125 0.953125
191 0.375 0.96875
192 0.609375 0.9375
193 0.40625 0.96875
194 0.4375 0.96875
195 0.390625 0.96875
196 0.40625 0.984375
197 0.4375 1.0
198 0.546875 0.953125
199 0.328125 1.0
200 0.328125 0.984375
201 0.328125 0.984375
202 0.453125 0.96875
203 0.34375 0.96875
204 0.34375 0.984375
205 0.359375 0.96875
206 0.359375 0.984375
207 0.421875 0.96875
208 0.5 0.984375
209 0.40625 1.0
210 0.390625 0.96875
211 0.546875 0.96875
212 0.390625 0.984375
213 0.5 0.921875
214 0.578125 0.96875
215 0.671875 0.9375
216 0.484375 0.984375
217 0.4375 0.90625
218 0.453125 0.96875
219 0.421875 0.953125
220 0.625 0.9375
221 0.5 0.953125
222 0.578125 0.984375
223 0.53125 0.9375
224 0.5625 0.984375
225 0.546875 0.984375
226 0.5625 0.96875
227 0.578125 0.984375
228 0.5625 0.953125
229 0.546875 0.90625
230 0.546875 0.921875
231 0.75 1.0
232 0.546875 1.0
233 0.671875 1.0
234 0.5625 0.96875
235 0.640625 0.984375
236 0.6875 0.921875
237 0.59375 0.90625
238 0.71875 0.921875
239 0.609375 1.0
240 0.59375 0.953125
241 0.65625 0.953125
242 0.640625 0.984375
243 0.6875 0.984375
244 0.546875 0.9375
245 0.71875 1.0
246 0.71875 0.953125
247 0.609375 1.0
248 0.671875 0.96875
249 0.625 0.953125
250 0.53125 0.921875
251 0.6875 0.984375
252 0.625 0.921875
253 0.65625 0.984375
254 0.625 0.9375
255 0.75 0.96875
256 0.609375 0.984375
257 0.640625 0.921875
258 0.6875 0.984375
259 0.671875 1.0
260 0.625 0.9375
261 0.65625 0.984375
262 0.703125 0.96875
263 0.734375 1.0
264 0.65625 0.984375
265 0.578125 0.9375
266 0.59375 0.96875
267 0.703125 0.921875
268 0.734375 1.0
269 0.734375 1.0
270 0.703125 0.984375
271 0.734375 0.984375
272 0.6875 0.953125
273 0.703125 0.96875
274 0.71875 1.0
275 0.65625 0.953125
276 0.75 0.921875
277 0.609375 0.984375
278 0.6875 1.0
279 0.6875 0.953125
280 0.765625 1.0
281 0.796875 1.0
282 0.734375 0.96875
283 0.671875 0.984375
284 0.578125 1.0
285 0.671875 0.953125
286 0.671875 0.984375
287 0.75 0.9375
288 0.734375 0.984375
289 0.71875 0.96875
290 0.75 0.96875
291 0.6875 0.984375
292 0.65625 0.9375
293 0.734375 0.984375
294 0.65625 0.953125
295 0.703125 0.96875
296 0.6875 0.984375
297 0.71875 0.984375
298 0.734375 0.953125
299 0.71875 0.9375
300 0.734375 0.984375
301 0.78125 0.953125
302 0.75 0.9375
303 0.75 0.953125
304 0.796875 0.96875
305 0.734375 1.0
306 0.71875 0.921875
307 0.796875 0.953125
308 0.84375 0.96875
309 0.75 0.96875
310 0.71875 1.0
311 0.828125 0.96875
312 0.8125 0.984375
313 0.78125 0.9375
314 0.84375 0.96875
315 0.6875 0.984375
316 0.84375 0.953125
317 0.8125 0.984375
318 0.765625 0.9375
319 0.71875 0.9375
320 0.703125 0.9375
321 0.671875 0.96875
322 0.703125 0.921875
323 0.765625 0.96875
324 0.703125 0.953125
325 0.828125 0.96875
326 0.828125 0.96875
327 0.78125 0.984375
328 0.8125 0.953125
329 0.796875 0.984375
330 0.765625 0.9375
331 0.890625 0.90625
332 0.8125 0.90625
333 0.8125 1.0
334 0.78125 0.96875
335 0.875 0.96875
336 0.84375 0.984375
337 0.796875 1.0
338 0.8125 0.984375
339 0.78125 0.9375
340 0.875 0.953125
341 0.8125 0.921875
342 0.796875 0.90625
343 0.796875 0.953125
344 0.8125 0.984375
345 0.890625 0.9375
346 0.828125 0.9375
347 0.78125 0.96875
348 0.90625 0.953125
349 0.828125 0.96875
350 0.796875 0.953125
351 0.734375 0.921875
352 0.796875 0.984375
353 0.765625 0.921875
354 0.8125 0.90625
355 0.765625 0.96875
356 0.84375 0.96875
357 0.875 0.953125
358 0.8125 0.90625
359 0.828125 0.96875
360 0.828125 0.953125
361 0.84375 0.984375
362 0.859375 0.9375
363 0.859375 0.953125
364 0.859375 0.9375
365 0.875 0.96875
366 0.875 0.953125
367 0.8125 0.953125
368 0.90625 0.9375
369 0.875 0.984375
370 0.9375 0.890625
371 0.875 0.9375
372 0.796875 0.921875
373 0.828125 0.921875
374 0.90625 0.921875
375 0.8125 0.9375
376 0.8125 0.90625
377 0.90625 0.921875
378 0.765625 1.0
379 0.78125 0.953125
380 0.78125 0.953125
381 0.875 0.953125
382 0.875 0.9375
383 0.84375 0.890625
384 0.796875 0.9375
385 0.859375 0.96875
386 0.8125 0.96875
387 0.90625 0.953125
388 0.921875 0.9375
389 0.890625 0.953125
390 0.96875 0.921875
391 0.84375 0.875
392 0.875 0.921875
393 0.828125 0.9375
394 0.921875 0.9375
395 0.890625 0.9375
396 0.921875 0.921875
397 0.890625 1.0
398 0.875 0.984375
399 0.921875 0.9375
400 0.828125 0.953125
401 0.828125 0.90625
402 0.9375 0.953125
403 0.8125 0.890625
404 0.953125 0.953125
405 0.9375 0.890625
406 0.90625 0.96875
407 0.890625 0.984375
408 0.875 0.9375
409 0.921875 0.984375
410 0.859375 0.96875
411 0.890625 0.953125
412 0.921875 0.96875
413 0.921875 1.0
414 0.921875 0.96875
415 0.875 0.90625
416 0.96875 0.96875
417 0.890625 0.921875
418 0.90625 0.953125
419 0.875 0.9375
420 0.875 0.921875
421 0.96875 0.953125
422 0.921875 0.96875
423 0.890625 0.875
424 0.84375 0.90625
425 0.921875 0.984375
426 0.953125 0.890625
427 1.0 0.90625
428 0.890625 0.9375
429 0.921875 0.9375
430 0.9375 0.953125
431 0.96875 0.953125
432 0.90625 0.9375
433 0.953125 0.875
434 0.953125 0.890625
435 0.96875 0.90625
436 0.9375 0.953125
437 0.96875 0.953125
438 0.953125 0.890625
439 0.921875 0.953125
440 0.953125 0.90625
441 0.90625 0.984375
442 0.96875 0.96875
443 0.984375 0.9375
444 0.921875 0.9375
445 0.96875 0.90625
446 0.921875 0.96875
447 0.890625 0.875
448 0.953125 0.953125
449 0.984375 0.953125
450 0.921875 0.953125
451 1.0 0.90625
452 0.984375 0.96875
453 1.0 0.90625
454 0.953125 0.953125
455 0.96875 0.9375
456 0.953125 0.90625
457 1.0 0.921875
458 0.984375 0.96875
459 0.984375 0.90625
460 0.984375 0.984375
461 1.0 0.921875
462 0.984375 0.953125
463 0.96875 0.921875
464 1.0 0.921875
465 0.984375 0.859375
466 1.0 0.90625
467 1.0 0.890625
468 1.0 0.90625
469 1.0 0.875
470 1.0 0.84375
471 1.0 0.875
472 0.984375 0.859375
473 1.0 0.859375
474 1.0 0.921875
475 0.984375 0.96875
476 1.0 0.875
477 1.0 0.890625
478 1.0 0.921875
479 1.0 0.890625
480 1.0 0.953125
481 1.0 0.90625
482 1.0 0.890625
483 1.0 0.90625
484 1.0 0.90625
485 1.0 0.890625
486 1.0 0.921875
487 1.0 0.96875
488 1.0 0.9375
489 1.0 0.921875
490 1.0 0.890625
491 1.0 0.890625
492 1.0 0.9375
493 1.0 0.90625
494 0.984375 0.9375
495 1.0 0.875
496 1.0 0.890625
497 1.0 0.9375
498 1.0 0.921875
499 0.984375 0.921875
500 1.0 0.921875
501 1.0 0.875
502 1.0 0.90625
503 1.0 0.953125
504 1.0 0.921875
505 1.0 0.96875
506 1.0 0.953125
507 1.0 0.921875
508 1.0 0.921875
509 1.0 0.890625
510 1.0 0.96875
511 1.0 0.921875
512 1.0 0.921875
513 1.0 0.859375
514 1.0 0.875
515 1.0 0.921875
516 1.0 0.9375
517 1.0 0.890625
518 1.0 0.90625
519 1.0 0.875
520 1.0 0.90625
521 1.0 0.9375
522 1.0 0.921875
523 1.0 0.875
524 1.0 0.875
525 1.0 0.921875
526 1.0 0.953125
527 1.0 0.90625
528 1.0 0.90625
529 1.0 0.90625
530 1.0 0.890625
531 1.0 0.90625
532 1.0 0.921875
533 1.0 0.921875
534 1.0 0.953125
535 1.0 0.875
536 1.0 0.90625
537 1.0 0.859375
538 1.0 0.890625
539 1.0 0.9375
540 1.0 0.9375
541 1.0 0.953125
542 1.0 0.890625
543 1.0 0.90625
544 1.0 0.84375
545 1.0 0.890625
546 1.0 0.890625
547 1.0 0.953125
548 1.0 0.9375
549 1.0 0.9375
550 1.0 0.890625
551 1.0 0.96875
552 1.0 0.921875
553 1.0 0.890625
554 1.0 0.90625
555 1.0 0.96875
556 1.0 0.921875
557 1.0 0.953125
558 1.0 0.890625
559 1.0 0.9375
560 1.0 0.9375
561 1.0 0.90625
562 1.0 0.9375
563 1.0 0.953125
564 1.0 0.875
565 1.0 0.90625
566 1.0 0.859375
567 1.0 0.875
568 1.0 0.921875
569 1.0 0.84375
570 1.0 0.890625
571 1.0 0.890625
572 1.0 0.953125
573 1.0 0.90625
574 1.0 0.921875
575 1.0 0.921875
576 1.0 0.921875
577 1.0 0.859375
578 1.0 0.9375
579 1.0 0.953125
580 1.0 0.921875
581 1.0 0.875
582 1.0 0.875
583 1.0 0.875
584 1.0 0.921875
585 1.0 0.890625
586 1.0 0.890625
587 1.0 0.890625
588 1.0 0.90625
589 1.0 0.875
590 1.0 0.90625
591 1.0 0.859375
592 1.0 0.90625
593 1.0 0.84375
594 1.0 0.859375
595 1.0 0.890625
596 1.0 0.90625
597 1.0 0.875
598 1.0 0.953125
599 1.0 0.875
600 1.0 0.859375
601 1.0 0.9375
602 1.0 0.875
603 1.0 0.859375
604 1.0 0.90625
605 1.0 0.90625
606 1.0 0.78125
607 1.0 0.828125
608 1.0 0.9375
609 1.0 0.921875
610 1.0 0.90625
611 1.0 0.953125
612 1.0 0.890625
613 1.0 0.9375
614 1.0 0.921875
615 1.0 0.796875
616 1.0 0.890625
617 1.0 0.921875
618 1.0 0.90625
619 1.0 0.890625
620 1.0 0.859375
621 1.0 0.921875
622 1.0 0.828125
623 1.0 0.875
624 1.0 0.953125
625 1.0 0.84375
626 1.0 0.9375
627 1.0 0.9375
628 1.0 0.859375
629 1.0 0.9375
630 1.0 0.90625
631 1.0 0.9375
632 1.0 0.921875
633 1.0 0.90625
634 1.0 0.90625
635 1.0 0.890625
636 1.0 0.78125
637 1.0 0.90625
638 1.0 0.890625
639 1.0 0.921875
640 1.0 0.75
641 1.0 0.875
642 1.0 0.875
643 1.0 0.90625
644 1.0 0.953125
645 1.0 0.921875
646 1.0 0.890625
647 1.0 0.859375
648 1.0 0.84375
649 1.0 0.90625
650 1.0 0.84375
651 1.0 0.84375
652 1.0 0.921875
653 1.0 0.90625
654 1.0 0.921875
655 1.0 0.921875
656 1.0 0.921875
657 1.0 0.84375
658 1.0 0.84375
659 1.0 0.90625
660 1.0 0.953125
661 1.0 0.890625
662 1.0 0.875
663 1.0 0.84375
664 1.0 0.84375
665 1.0 0.8125
666 1.0 0.859375
667 1.0 0.90625
668 1.0 0.90625
669 1.0 0.875
670 1.0 0.921875
671 1.0 0.84375
672 1.0 0.953125
673 1.0 0.84375
674 1.0 0.8125
675 1.0 0.84375
676 1.0 0.921875
677 1.0 0.953125
678 1.0 0.84375
679 1.0 0.859375
680 1.0 0.859375
681 1.0 0.90625
682 1.0 0.90625
683 1.0 0.90625
684 1.0 0.828125
685 1.0 0.875
686 1.0 0.9375
687 1.0 0.859375
688 1.0 0.859375
689 1.0 0.84375
690 1.0 0.953125
691 1.0 0.84375
692 1.0 0.859375
693 1.0 0.9375
694 1.0 0.890625
695 1.0 0.859375
696 1.0 0.875
697 1.0 0.875
698 1.0 0.9375
699 1.0 0.890625
700 1.0 0.921875
701 1.0 0.828125
702 1.0 0.84375
703 1.0 0.90625
704 1.0 0.859375
705 1.0 0.90625
706 1.0 0.921875
707 1.0 0.9375
708 1.0 0.84375
709 1.0 0.875
710 1.0 0.90625
711 1.0 0.90625
712 1.0 0.890625
713 1.0 0.921875
714 1.0 0.90625
715 1.0 0.890625
716 1.0 0.8125
717 1.0 0.875
718 1.0 0.890625
719 1.0 0.890625
720 1.0 0.90625
721 1.0 0.875
722 1.0 0.84375
723 1.0 0.9375
724 1.0 0.90625
725 1.0 0.84375
726 1.0 0.9375
727 1.0 0.875
728 1.0 0.8125
729 1.0 0.84375
730 1.0 0.9375
731 1.0 0.84375
732 1.0 0.875
733 1.0 0.921875
734 1.0 0.78125
735 1.0 0.78125
736 1.0 0.859375
737 1.0 0.84375
738 1.0 0.90625
739 1.0 0.859375
740 1.0 0.875
741 1.0 0.859375
742 1.0 0.890625
743 1.0 0.875
744 1.0 0.890625
745 1.0 0.859375
746 1.0 0.8125
747 1.0 0.859375
748 1.0 0.953125
749 1.0 0.84375
750 1.0 0.890625
751 1.0 0.796875
752 1.0 0.890625
753 1.0 0.796875
754 1.0 0.84375
755 1.0 0.890625
756 1.0 0.875
757 1.0 0.890625
758 1.0 0.921875
759 1.0 0.84375
760 1.0 0.875
761 1.0 0.859375
762 1.0 0.953125
763 1.0 0.890625
764 1.0 0.859375
765 1.0 0.84375
766 1.0 0.828125
767 1.0 0.8125
768 1.0 0.84375
769 1.0 0.859375
770 1.0 0.875
771 1.0 0.890625
772 1.0 0.90625
773 1.0 0.90625
774 1.0 0.953125
775 1.0 0.90625
776 1.0 0.890625
777 1.0 0.890625
778 1.0 0.921875
779 1.0 0.90625
780 1.0 0.90625
781 1.0 0.90625
782 1.0 0.796875
783 1.0 0.765625
784 1.0 0.921875
785 1.0 0.875
786 1.0 0.890625
787 1.0 0.90625
788 1.0 0.90625
789 1.0 0.890625
790 1.0 0.859375
791 1.0 0.890625
792 1.0 0.875
793 1.0 0.796875
794 1.0 0.859375
795 1.0 0.828125
796 1.0 0.890625
797 1.0 0.875
798 1.0 0.890625
799 1.0 0.8125
800 1.0 0.875
801 1.0 0.921875
802 1.0 0.90625
803 1.0 0.890625
804 1.0 0.90625
805 1.0 0.890625
806 1.0 0.90625
807 1.0 0.9375
808 1.0 0.90625
809 1.0 0.90625
810 1.0 0.859375
811 1.0 0.859375
812 1.0 0.890625
813 1.0 0.875
814 1.0 0.8125
815 1.0 0.875
816 1.0 0.921875
817 1.0 0.9375
818 1.0 0.890625
819 1.0 0.90625
820 1.0 0.921875
821 1.0 0.875
822 1.0 0.9375
823 1.0 0.90625
824 1.0 0.875
825 1.0 0.84375
826 1.0 0.875
827 1.0 0.8125
828 1.0 0.8125
829 1.0 0.890625
830 1.0 0.78125
831 1.0 0.890625
832 1.0 0.890625
833 1.0 0.84375
834 1.0 0.859375
835 1.0 0.875
836 1.0 0.890625
837 1.0 0.9375
838 1.0 0.953125
839 1.0 0.875
840 1.0 0.890625
841 1.0 0.78125
842 1.0 0.921875
843 1.0 0.875
844 1.0 0.84375
845 1.0 0.859375
846 1.0 0.828125
847 1.0 0.8125
848 1.0 0.921875
849 1.0 0.890625
850 1.0 0.84375
851 1.0 0.96875
852 1.0 0.90625
853 1.0 0.9375
854 1.0 0.875
855 1.0 0.890625
856 1.0 0.859375
857 1.0 0.890625
858 1.0 0.8125
859 1.0 0.875
860 1.0 0.875
861 1.0 0.875
862 1.0 0.859375
863 1.0 0.875
864 1.0 0.9375
865 1.0 0.796875
866 1.0 0.84375
867 1.0 0.90625
868 1.0 0.875
869 1.0 0.890625
870 1.0 0.953125
871 1.0 0.890625
872 1.0 0.90625
873 1.0 0.828125
874 1.0 0.96875
875 1.0 0.90625
876 1.0 0.84375
877 1.0 0.765625
878 1.0 0.96875
879 1.0 0.890625
880 1.0 0.890625
881 1.0 0.84375
882 1.0 0.890625
883 1.0 0.84375
884 1.0 0.859375
885 1.0 0.84375
886 1.0 0.859375
887 1.0 0.875
888 1.0 0.921875
889 1.0 0.90625
890 1.0 0.90625
891 1.0 0.859375
892 1.0 0.859375
893 1.0 0.9375
894 1.0 0.84375
895 1.0 0.859375
896 1.0 0.90625
897 1.0 0.9375
898 1.0 0.84375
899 1.0 0.890625
900 1.0 0.828125
901 1.0 0.90625
902 1.0 0.90625
903 1.0 0.859375
904 1.0 0.875
905 1.0 0.84375
906 1.0 0.875
907 1.0 0.875
908 1.0 0.859375
909 1.0 0.875
910 1.0 0.9375
911 1.0 0.796875
912 1.0 0.90625
913 1.0 0.859375
914 1.0 0.9375
915 1.0 0.875
916 1.0 0.859375
917 1.0 0.890625
918 1.0 0.890625
919 1.0 0.875
920 1.0 0.953125
921 1.0 0.921875
922 1.0 0.921875
923 1.0 0.890625
924 1.0 0.9375
925 1.0 0.859375
926 1.0 0.90625
927 1.0 0.921875
928 1.0 0.859375
929 1.0 0.859375
930 1.0 0.875
931 1.0 0.890625
932 1.0 0.890625
933 1.0 0.859375
934 1.0 0.859375
935 1.0 0.828125
936 1.0 0.96875
937 1.0 0.875
938 1.0 0.75
939 1.0 0.9375
940 1.0 0.828125
941 1.0 0.890625
942 1.0 0.828125
943 1.0 0.859375
944 1.0 0.875
945 1.0 0.921875
946 1.0 0.890625
947 1.0 0.921875
948 1.0 0.9375
949 1.0 0.953125
950 1.0 0.875
951 1.0 0.859375
952 1.0 0.890625
953 1.0 0.84375
954 1.0 0.875
955 1.0 0.875
956 1.0 0.90625
957 1.0 0.84375
958 1.0 0.828125
959 1.0 0.75
960 1.0 0.890625
961 1.0 0.875
962 1.0 0.921875
963 1.0 0.796875
964 1.0 0.875
965 1.0 0.90625
966 1.0 0.921875
967 1.0 0.796875
968 1.0 0.875
969 1.0 0.921875
970 1.0 0.828125
971 1.0 0.9375
972 1.0 0.890625
973 1.0 0.921875
974 1.0 0.875
975 1.0 0.9375
976 1.0 0.90625
977 1.0 0.953125
978 1.0 0.859375
979 1.0 0.828125
980 1.0 0.9375
981 1.0 0.953125
982 1.0 0.90625
983 1.0 0.828125
984 1.0 0.78125
985 1.0 0.90625
986 1.0 0.921875
987 1.0 0.90625
988 1.0 0.921875
989 1.0 0.890625
990 1.0 0.90625
991 1.0 0.890625
992 1.0 0.78125
993 1.0 0.859375
994 1.0 0.765625
995 1.0 0.8125
996 1.0 0.890625
997 1.0 0.890625
998 1.0 0.875
999 1.0 0.84375
```

"The goal is to train a generator model, not a discriminator model, and that is where the complexity of GANs truly lies."

# The Generator Model

We will define a small latent space of five dimensions and use the standard approach in the GAN literature of using a Gaussian distribution for each variable in the latent space. We will generate new inputs by drawing random numbers from a standard Gaussian distribution, i.e. mean of zero and a standard deviation of one.

• Specs:
• Single Hidden Layer with 5 nodes
• ReLU activation Function
• He weight initialization
• Output layer will have 2 nodes+ will use linear activation function
``````# define the generator model unit
def define_generator(latent_dim, n_outputs=2):
model = Sequential()
return model``````
``````# define the discriminator model
model = define_generator(5)
# summarize the model
model.summary()``````
```Model: &quot;sequential_3&quot;
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense_5 (Dense)              (None, 15)                90
_________________________________________________________________
dense_6 (Dense)              (None, 2)                 32
=================================================================
Total params: 122
Trainable params: 122
Non-trainable params: 0
_________________________________________________________________
```
``````# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n):
# generate points in the latent space
x_input = np.random.randn(latent_dim * n)
# reshape into a batch of inputs for the network
x_input = x_input.reshape(n, latent_dim)
return x_input``````
``````# use the generator to generate n fake examples and plot the results
def generate_fake_samples(generator, latent_dim, n):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n)
# predict outputs
X = generator.predict(x_input)
# create class labels
y = np.zeros((n, 1))
return X, y``````

When the discriminator is good at detecting fake samples, the generator is updated more, and when the discriminator model is relatively poor or confused when detecting fake samples, the generator model is updated less.

``````# define the combined generator and discriminator model, for updating the generator
def define_gan(generator, discriminator):
# make weights in the discriminator not trainable
discriminator.trainable = False
# connect them
model = Sequential()
# compile model
return model``````
``````# train the generator and discriminator
def train(g_model, d_model, gan_model, latent_dim, n_epochs=10000, n_batch=128, n_eval=2000):
# determine half the size of one batch, for updating the discriminator
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_epochs):
# prepare real samples
x_real, y_real = generate_real_samples(half_batch)
# prepare fake examples
x_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
# update discriminator
d_model.train_on_batch(x_real, y_real)
d_model.train_on_batch(x_fake, y_fake)
# prepare points in latent space as input for the generator
x_gan = generate_latent_points(latent_dim, n_batch)
# create inverted labels for the fake samples
y_gan = np.ones((n_batch, 1))
# update the generator via the discriminator's error
gan_model.train_on_batch(x_gan, y_gan)
# evaluate the model every n_eval epochs
if (i+1) % n_eval == 0:
summarize_performance(i, g_model, d_model, latent_dim)``````
``````# evaluate the discriminator and plot real and fake points
def summarize_performance(epoch, generator, discriminator, latent_dim, n=100):
# prepare real samples
x_real, y_real = generate_real_samples(n)
# evaluate discriminator on real examples
_, acc_real = discriminator.evaluate(x_real, y_real, verbose=0)
# prepare fake examples
x_fake, y_fake = generate_fake_samples(generator, latent_dim, n)
# evaluate discriminator on fake examples
_, acc_fake = discriminator.evaluate(x_fake, y_fake, verbose=0)
# summarize discriminator performance
print(epoch, acc_real, acc_fake)
# scatter plot real and fake data points
pyplot.scatter(x_real[:, 0], x_real[:, 1], color='red')
pyplot.scatter(x_fake[:, 0], x_fake[:, 1], color='blue')
pyplot.show()``````
``````# size of the latent space
latent_dim = 5
# create the discriminator
discriminator = define_discriminator()
# create the generator
generator = define_generator(latent_dim)
# create the gan
gan_model = define_gan(generator, discriminator)
# train model
train(generator, discriminator, gan_model, latent_dim)``````
```1999 0.7400000095367432 0.4399999976158142
```