The GANs consists of two neural networks, one called Discriminator and the other called Generator. The role of the generator is to estimate the probability distribution of the real samples in order to provide generated samples resembling real data. The discriminator, in turn, is trained to estimate the probability that a given sample came from the real data rather than being provided by the generator.

%% Cell type:code id: tags:
``` python
classDiscriminator(nn.Module):
def__init__(self):
super().__init__()
self.model=nn.Sequential(
nn.Linear(2,256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256,128),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(128,64),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(64,1),
nn.Sigmoid(),
)
defforward(self,x):
output=self.model(x)
returnoutput
```
%% Cell type:code id: tags:
``` python
classGenerator(nn.Module):
def__init__(self):
super().__init__()
self.model=nn.Sequential(
nn.Linear(2,16),
nn.ReLU(),
nn.Linear(16,32),
nn.ReLU(),
nn.Linear(32,2),
)
defforward(self,x):
output=self.model(x)
returnoutput
```
%% Cell type:markdown id: tags:
Instantiation
%% Cell type:code id: tags:
``` python
discriminator=Discriminator()
generator=Generator()
```
%% Cell type:markdown id: tags:
#### Set hyperparameters
%% Cell type:code id: tags:
``` python
lr=0.001
num_epochs=300
loss_function=nn.BCELoss()
batch_size=32
train_loader=torch.utils.data.DataLoader(
train_set,batch_size=batch_size,shuffle=True
)
```
%% Cell type:markdown id: tags:
#### Set the optimization object
What do optimizer do in neural network?

`Adam` combines the best properties of the AdaGrad and RMSProp algorithms to provide an optimization algorithm that can handle sparse gradients on noisy problems.
1. Train the discriminator using real(1) data and fake(0) data. The discriminator will need to distinguish between the fake and real data points
2. Train the generator using random numbers as input and let discriminator to do the classify output from generator. The loss is the difference of the classification result and true labels, all of which is of value 1