"""
![Adsız.png](attachment:e3e40ce0-0aab-43fa-b29e-e2f74b72d830.png)

<center><h1 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold">Introduction 📖 </h1></center>
<p style = "color:black;font-family:Segoe Print;font-weight:bold">The mnist_train.csv file contains the 60,000 training examples and labels. The mnist_test.csv contains 10,000 test examples and labels. Each row consists of 785 values: the first value is the label (a number from 0 to 9) and the remaining 784 values are the pixel values (a number from 0 to 255).</p>

<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" > What are we going to do in this notebook? </p>

<p style = "color:black;font-family:Segoe Print;font-weight:bold">Yes, without going into too much detail in this notebook, what are GANs? We will talk about topics such as what are the usage areas. We will perform the steps below one by one. While doing these, we will use visuals so that they can be better visualized in our minds.</p>

<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" > So, let's get started. </p>
    
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold">Content :</h2>

<ul>
    <li style = "color:gray;font-size:16px"> <a href = "#1" style = "color:black;font-family:Segoe Print;font-weight:bold"> Generative Adversarial Networks 📚 ❓ </a>
    <ul>
        <li style = "color:darkblue;font-size:16px"> <a href = "#2" style = "color:darkblue;font-family:Segoe Print;font-weight:bold"> How does this learn to create new data? </a> </li> 
    </ul>
    </li> 
        <li style = "color:gray;font-size:16px"> <a href = "#3" style = "color:black;font-family:Segoe Print;font-weight:bold"> More detailed GANs ⁉️ </a>
    <ul>
        <li style = "color:darkblue;font-size:16px"> <a href = "#4" style = "color:darkblue;font-family:Segoe Print;font-weight:bold"> How is the Generator Trained? </a> </li> 
    </ul>
    </li> 
        <li style = "color:gray;font-size:16px"> <a href = "#5" style = "color:black;font-family:Segoe Print;font-weight:bold"> What are the uses of GANs ❓ </a> </li>
        <li style = "color:gray;font-size:16px"> <a href = "#6" style = "color:black;font-family:Segoe Print;font-weight:bold"> Load and Check Data 🗸 </a> </li> 
    <li  style = "color:gray;font-size:16px" > <a href = "#7" style = "color:black;font-family:Segoe Print;font-weight:bold" > Train - Test Split </a>  </li>
            <li style = "color:gray;font-size:16px" ><a href = "#8" style = "color:black;font-family:Segoe Print;font-weight:bold" >  Normalization ⁉️  </a> 
                <ul>
                    <li style = "color:gray;font-size:16px" ><a href = "#9" style = "color:darkblue;font-family:Segoe Print;font-weight:bold" >   Why do we want to normalize the pictures? </a></li>
                </ul>
            </li>
            <li style = "color:gray;font-size:16px" ><a href = "#10" style = "color:black;font-family:Segoe Print;font-weight:bold" >  Reshape ❓ </a> 
                <ul>
                    <li style = "color:gray;font-size:16px" ><a href = "#11" style = "color:darkblue;font-family:Segoe Print;font-weight:bold" >   Why do we need to reshape the pictures? </a></li> </ul></li>
      <li style = "color:black;font-size:16px" ><a href = "#12" style = "color:black;font-family:Segoe Print;font-weight:bold"> Implementing with Keras ❔</a>
          <ul>
        <li style = "color:darkgreen;font-size:16px"> <a href = "#13" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Import Model Library  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#14" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Create Generator </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#15" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Generator Output  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#16" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Generator Compile  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#17" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Generator Summmary </a> </li> 
              <li style = "color:darkgreen;font-size:16px"> <a href = "#18" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Create Discriminator </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#19" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Discriminator Output  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#20" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Discriminator Compile  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#21" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Discriminator Summmary </a> </li> 
                                    <li style = "color:darkgreen;font-size:16px"> <a href = "#22" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Create GANs  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#23" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> GANs Summmary </a> </li> 
                                                  <li style = "color:darkgreen;font-size:16px"> <a href = "#24" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Epochs and Batch Size ❓  </a> </li> 
                      <li style = "color:darkgreen;font-size:16px"> <a href = "#25" style = "color:darkgreen;font-family:Segoe Print;font-weight:bold"> Fit the Model </a> </li> 
    </ul> </li>
<li style = "color:gray;font-size:16px"> <a href = "#26" style = "color:black;font-family:Segoe Print;font-weight:bold"> Result </a> </li>
    <li style = "color:gray;font-size:16px"> <a href = "#27" style = "color:black;font-family:Segoe Print;font-weight:bold"> CONCLUSION </a> </li>
</ul>
"""
"""
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Import Libraries 🔖 </h2>
"""
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

#import warnings
import warnings
warnings.filterwarnings('ignore')

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
<a id ='1' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Generative Adversarial Networks 📚 ❓ </h2>

![Adsız.png](attachment:6c638db0-a8ef-48bf-aa21-dbde7048c2f9.png)

<p style = "color:black;font-family:Segoe Print;font-weight:bold" > Before moving on to the detailed explanation, let's talk briefly. GANs are structures that learn to generate new data that have the same statistics as the training set. </p>

<a id ='2' ></a>
<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" > How does this learn to create new data? </p>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" > Let us explain this in the most famous way of describing it. In the picture you see above, there is a real picture, a detective and a counterfeiter. </p>

<ol>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >We have one real picture.</li>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >And we have a counterfeiter who paints fake pictures.</li>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >The real picture and the fake picture go to the detective.</li>
        <li style = "color:black;font-family:Segoe Print;font-weight:bold" >and the detective realize which of them is fake and real.</li>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >Meanwhile, the counterfeiter begins to draw pictures that are very close to the real picture.</li>
        <li style = "color:black;font-family:Segoe Print;font-weight:bold" >Thus, it creates data with the same statistics as the real data.</li>
</ol>
"""
"""
<a id ='3' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
More detailed GANs ⁉️ </h2>

![pn.PNG](attachment:d5426892-88a8-411a-a2a8-c4d271cd9fcd.PNG)

<p style = "color:black;font-family:Segoe Print;font-weight:bold" > What is our purpose here? Isn't it generating new data? Yes, as we can see from the picture, we have real pictures and we also have a generator that will generate fake pictures. The picture produced by the generator and the real picture go to the detective and the detective decides which is real and which is fake. As a result of this, a loss occurs and the transaction is continued by back propagation. </p>

<a id ='4' ></a>
<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" > How is the Generator Trained? </p>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" > Let me explain this a little bit more narratively. At first the generator will produce a picture that does not look like the real picture because the room does not know what it is producing and will label the picture it produces, so it will say that this is the real picture and send it to the detective. The detective will say you couldn't fool me it's not real and send it back. This pattern generator, experiment by experiment, will now produce a picture that is very similar to the real picture and will deceive the detective. In this way, a new data will be produced. </p>
"""
"""
<a id ='5' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
What are the uses of GANs ❓ </h2>

![Adsız.png](attachment:c53272c4-8199-4f78-8fcc-dce03c3284fe.png)

<ul>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >It can be used in the gaming industry. For example, to produce anime characters.</li>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" >It can be used to increase the resolution of images.</li>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >can be used to complete the pictures.</li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" >It can be used to produce new pictures from different angles.</li>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" >Turning the information obtained from the text into a picture.</li>
</ul>
"""
"""
<a id ='6' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Load and Check Data 🗸 </h2>
"""
train_data = pd.read_csv("/kaggle/input/mnist-in-csv/mnist_train.csv")
test_data = pd.read_csv("/kaggle/input/mnist-in-csv/mnist_test.csv")
print("Train Shape --> ",train_data.shape)
print("Test Shape --> ",test_data.shape)
train_data.head()
"""
<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print" > As you can see, our data consists of pixel values ​​and label. Let's take a visual look at the distribution of numbers. </p> </li>
</ul>
"""
# how many of which numbers are there?
plt.figure(figsize=(15,7))
g = sns.countplot(train_data["label"], palette="cubehelix")
plt.title("Number of digit classes")
print(" \t # Train Data value counts # \n",train_data["label"].value_counts())
"""
<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> We also looked at the distribution of numbers in our data. </p> </li>
<ul>
"""
"""
<a id ='7' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Train -Test Split </h2>
"""
X_train = train_data.drop(["label"], axis = 1).values
y_train = train_data.label.values

X_test = test_data.drop(["label"], axis = 1).values
y_test = test_data.label.values
print("X_train shape --> ",X_train.shape)
print("y_train shape --> ",y_train.shape)
print("X_test shape --> ",X_test.shape)
print("y_test shape --> ",y_test.shape)
plt.figure(figsize=(10,8))
img = X_train[244]
img = img.reshape((28,28))
plt.subplot(1,2,1)
plt.imshow(img,cmap='gray')
plt.title(train_data.iloc[244,0])
plt.grid()

img1 = X_train[25]
img1 = img1.reshape((28,28))
plt.subplot(1,2,2)
plt.imshow(img1,cmap='gray')
plt.title(train_data.iloc[25,0])
plt.grid()
plt.show()
"""
<a id ='8' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Normalization ⁉️ </h2>

<a id ='9' ></a>
<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" > Why do we want to normalize the pictures? </p>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" >If we do not normalize, there may be errors due to certain colors. So, we first need to normalize. </p>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> Images are represented by values ​​between 0 and 255. Therefore </p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> There are many ways to normalize. Here we prefer to subtract the average from the pixel value and divide by the average. In this way, we will bring all of our images to the [-1,1] range.</p> </li>
 </ul>

"""
X_train = (X_train.astype(np.float32)-127.5)/127.5
print("X_train Shape --> ",X_train.shape)
"""
<a id ='10' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Reshape ⁉️ </h2>

<a id ='11' ></a>
<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" > Why do we need to reshape the pictures? </p>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" >Because that's how keras works. For example, the input value for CNN is (28,28,1). Different models may require different input shapes. </p>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> Our input type for this model is (60000, 784). </p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> Our data has already been prepared in this way, but I will show it in the comment lines in case it comes across in a different way./p> </li>
        </ul>

"""
# in --> (60000, 28, 28)
#X_train = X_train.reshape(X_train.shape[0],X_train.shape[1]*X_train.shape[2])
# out --> (60000, 784)
"""
<a id ='12' ></a>
<h2 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Implementing with Keras ❔ </h2>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" > Our In this section we will create our GANs model using keras.</p>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Import Model Library 📗</p> </li>
     <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">Generator ❗</p> 
         <ul> 
             <li style = "color:darkblue;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkblue;font-family:Segoe Print;font-weight:bold">Create Generator</p> </li>
        <li style = "color:darkblue;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkblue;font-family:Segoe Print;font-weight:bold">Generator Output</p> </li>
        <li style = "color:darkblue;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkblue;font-family:Segoe Print;font-weight:bold">Generator Compile</p> </li> 
                     <li style = "color:darkblue;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkblue;font-family:Segoe Print;font-weight:bold">Generator Summary</p> </li> 
         </ul> 
    </li>
     <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">Discriminator ❗</p> 
         <ul> 
             <li style = "color:darkgreen;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkgreen;font-family:Segoe Print;font-weight:bold">Create Discriminator</p> </li>
        <li style = "color:darkgreen;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkgreen;font-family:Segoe Print;font-weight:bold">Discriminator Output</p> </li>
        <li style = "color:darkgreen;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkgreen;font-family:Segoe Print;font-weight:bold">Discriminator Compile</p> </li> 
                     <li style = "color:darkgreen;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkgreen;font-family:Segoe Print;font-weight:bold">Discriminator Summary</p> </li> 
         </ul> 
    </li>
     <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">GANs ❗❗</p> 
         <ul> 
             <li style = "color:orange;font-family:Segoe Print;font-weight:bold" > <p style = "color:orange;font-family:Segoe Print;font-weight:bold">Create GANs</p> </li>
                     <li style = "color:orange;font-family:Segoe Print;font-weight:bold" > <p style = "color:orange;font-family:Segoe Print;font-weight:bold">GANs Summary</p> </li> 
         </ul> 
    </li>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Epochs and Batch Size ❓</p> </li>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Fit the Model ❓</p> </li>
</ul>
"""
"""
<a id ='13' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Import Model Library </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> We will import the libraries we will use for our GANs model.</p> </li>
  </ul>
"""
from keras.layers import Dense, Dropout, Input, ReLU
from keras.models import Sequential, Model
from keras.optimizers import Adam
"""
<a id ='14' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Create Generator </h3>

![dddddd.PNG](attachment:93e31b3b-b267-453a-af31-d6681bef03f2.PNG)

<ul>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> I wanted to show the generator model that we will create above by visualizing it so that it can be better understood and fit in the head.</p> </li>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> In this section, we will create our manufacturer model. While doing this, we will do it step by step for better understanding.</p> </li>
  </ul>
"""
generator = Sequential()
generator.add(Dense(units = 512, input_dim = 100))
generator.add(ReLU())

generator.add(Dense(units = 512))
generator.add(ReLU())

generator.add(Dense(units = 1024))
generator.add(ReLU())
"""
<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> We create our generator model. Below we will add an output layer to our model. </p> </li>
  </ul>
"""
"""
<a id ='15' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Generator Output </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Yes, we are creating the output layer of our generator module. Since our image size is 784 here, our output will be this shape. As a result, we produce an image.</p> </li>
  </ul>
"""
generator.add(Dense(units = 784, activation = "tanh"))
"""
<a id ='16' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Generator Compile </h4>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" > Yes, now we need to compile our model.</p>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold"> optimizer --> The optimizer does the process of updating our parameters for us here. some kind of healer I can say. There are methods used for multiple optimizers, and you should choose the most suitable one for the model.
 </p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> loss --> It is a number that indicates how good or bad the model is to its prediction. As it approaches 0, the error starts to decrease.
 </p> </li>
        </ul>
"""
generator.compile(loss = "binary_crossentropy",
                optimizer = Adam(lr = 0.0001, beta_1 = 0.5))
"""
<a id ='17' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Generator Summary </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold"> We will look at our generator model that we created above. </p> </li>
  </ul>
"""
generator.summary()
"""
<a id ='18' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Create Discriminator </h3>

![dddddd.PNG](attachment:805dd1f0-aba0-4113-aacc-280e3ca4d12f.PNG)

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">In this section, we will create our discriminator layers.</p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">In other words, we are creating the detective department that will distinguish whether it is real or fake for us.</p> </li>
  </ul>
"""
discriminator = Sequential()
discriminator.add(Dense(units = 1024, input_dim = 784))
discriminator.add(ReLU())
discriminator.add(Dropout(0.4))

discriminator.add(Dense(units = 512))
discriminator.add(ReLU())
discriminator.add(Dropout(0.4))

discriminator.add(Dense(units = 256))
discriminator.add(ReLU())
"""
<a id ='19' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Discriminator Output </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">we created our detective model above. Here we will create the output part of it.</p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">We will have one output value. Either fake or real.</p> </li>
  </ul>
"""
discriminator.add(Dense(units = 1, activation = "sigmoid"))
"""
<a id ='20' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Discriminator Compile </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Here we will compile our model. We described this above. So I'm just writing the code.</p> </li>
  </ul>
"""
discriminator.compile(loss = "binary_crossentropy",
                      optimizer = Adam(lr = 0.0001, beta_1 = 0.5))
"""
<a id ='21' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Discriminator Summary </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">We take a look at the discriminator model we created.</p> </li>
  </ul>
"""
discriminator.summary()
"""
<a id ='22' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Create GANs </h3>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">We created our generator and discriminator models above.</p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Now we will combine these two to create your GANs model.</p> </li>
  </ul>
"""
discriminator.trainable = False
gan_input = Input(shape = (100,))
x = generator(gan_input)
gan_output = discriminator(x)
gan = Model(inputs = gan_input, outputs = gan_output)
gan.compile(loss = "binary_crossentropy", optimizer = "adam")
"""
<a id ='23' ></a>
<h4 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
GANs Summary </h4>

<ul>
    <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Yes, now that we've built our GANs model, we can take a look at it.</p> </li>
  </ul>
"""
gan.summary()
"""
<a id ='24' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Epochs and Batch Size ❓ </h3>

<ul>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Epochs : the forward and backward processing of data one by one. In other words, we can simply call it a complete educational tour. Education is determined by the number of eras.</p> </li>
        <li style = "color:black;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Batch Size : Basically, in the periods I mentioned above, it takes a long time for us to train the data one by one. During training how much of the data,indicates that it will be trained.</p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">To give an example of this, imagine we have 10 loaves of bread. If we select batch_size as 2, imagine that we eat these breads 2 times and 2 times.</p> </li>
  </ul>
"""
epochs = 50
batch_size = 256
"""
<a id ='25' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Fit the model ❓ </h3>

![Adsız.png](attachment:2f3dfa2f-b510-4fa8-b09e-24c7fc635069.png)

<ul>
     <li style = "color:black;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">In the picture above, I wanted to briefly summarize what we will do in this section.</p> </li>
    <li style = "color:black;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">Yes, it's time to train the gans model we've built so far.</p> </li>
        <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">For a better understanding of the training code. I will explain by writing comments on the code.</p> </li>
  </ul>
"""
for e in range(epochs):
    for _ in range(batch_size):
        
        """
         Yes, we first generate noise to give it to the generator.
         The generator will try to generate us a new image using it.
        """
        noise = np.random.normal(0,1, [batch_size, 100])  
        
        """
         We give the noise we have produced above to my generator.
         We have already produced this generator above. This will produce us an image as output
        """
        generated_images = generator.predict(noise)
        
        """
        we produced our fake pictures above. It's time to choose our real pictures. We will do this here.
        """
        image_batch = X_train[np.random.randint(low = 0, high = X_train.shape[0], size = batch_size)]
        
        """
        We combine our two data to send it to the discriminator.
        """
        x = np.concatenate([image_batch, generated_images])
        
        """
        Yes, we got our pictures but they don't have tags. Here we add tags to your images.
        Real Images --> 1
        Generated Images --> 0
        """
        y_dis = np.zeros(batch_size*2)
        y_dis[:batch_size] = 1
        
        """
        Now we can train the discriminator.
        We set it to True to train the discriminator. Because we closed it above.
        inputs --> x (real image, fake image)
        """
        discriminator.trainable = True
        discriminator.train_on_batch(x,y_dis)
        
        """
        Yes, now we can move on to the generated training.
        Generated Images --> 1
        Here why we showed fake pictures as 1.
        Because we want to deceive the discriminator. This will make him more successful.
        """
        noise = np.random.normal(0,1, [batch_size,100]) 
        y_gen = np.ones(batch_size)
        
        discriminator.trainable = False
        
        gan.train_on_batch(noise, y_gen)
    print("epochs: ",e)
        
"""
<a id ='26' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
Result </h3>

<ul>
     <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:black;font-family:Segoe Print;font-weight:bold">In this section, we will take a look at the images we produced using the GANs model.</p> </li>
  </ul>
"""
plt.figure(figsize=(25,16))
noise= np.random.normal(loc=0, scale=1, size=[100, 100])
generated_images = generator.predict(noise)
generated_images = generated_images.reshape(100,28,28)

for i in range(25):
    plt.subplot(5,5,i+1)
    plt.imshow(generated_images[i], interpolation='nearest', cmap = "gray")
    plt.axis('off')

plt.suptitle("Pictures Produced Using the GANs Model", fontsize = 20 , color = "darkred", fontweight = 'bold')
plt.show()
"""
<ul>
     <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">Yes, we took a look at the images we created using the GANs model.</p> </li>
         <li style = "color:darkred;font-family:Segoe Print;font-weight:bold" > <p style = "color:darkred;font-family:Segoe Print;font-weight:bold">Even though it's a 50 epochs model, we produced pretty good pictures. There are pictures that are pretty close to the real thing.</p> </li>
  </ul>
"""
"""
<a id ='27' ></a>
<h3 style = "background:#0C0C0C ;color:white;border:0;font-family:Segoe Print;font-weight:bold"> 
CONCLUSION </h3>

<p style = "color:darkred;font-family:Segoe Print;font-weight:bold" >Would be glad to hear your nice comments or questions. </p>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" >  You can check out other tutorials here. <br> <br>
    RNN Tutorial -->  
    <a href = "https://www.kaggle.com/rafetcan/recurrent-neural-n-rnn-tutorial-for-beginners" >https://www.kaggle.com/rafetcan/recurrent-neural-n-rnn-tutorial-for-beginners</a>

<p style = "color:black;font-family:Segoe Print;font-weight:bold" >  Seaborn Tutorial -->  
    <a href = "https://www.kaggle.com/rafetcan/visualization-tutorial-with-seaborn" >https://www.kaggle.com/rafetcan/visualization-tutorial-with-seaborn</a>
</p>
<p style = "color:black;font-family:Segoe Print;font-weight:bold" >  Plotly Tutorial -->  
    <a href = "https://www.kaggle.com/rafetcan/plotly-tutorial-for-beginners" >https://www.kaggle.com/rafetcan/plotly-tutorial-for-beginners</a></p>
    <p style = "color:black;font-family:Segoe Print;font-weight:bold" >  CNN Tutorial -->  
    <a href = "https://www.kaggle.com/rafetcan/convolutional-neural-network-cnn-tutorial" >https://www.kaggle.com/rafetcan/convolutional-neural-network-cnn-tutorial</a>
</p>
    <p style = "color:black;font-family:Segoe Print;font-weight:bold" >  Long Short-Term Memory N. (LSTM) for Beginners 📚 -->  
    <a href = "https://www.kaggle.com/rafetcan/long-short-term-memory-n-lstm-for-beginners" >https://www.kaggle.com/rafetcan/long-short-term-memory-n-lstm-for-beginners</a>
</p>

<p style = "text-shadow: 12px 12px 2px #333;color:darkgreen;font-family:Segoe Print;font-weight:bold" > I YES, WE HAVE COME TO AN END. THANK YOU</p>
"""