<jupyter_start><jupyter_text><jupyter_code>import torchvision.datasets as datasets
import torch 
import matplotlib.pyplot as plt
import numpy as np
import torch.optim  as optim
from torch.utils.data import DataLoader,Dataset
import torchvision.transforms as transforms
import torch.nn as nn
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
transformer=transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize(mean=(0.5,),
                                                     std=(0.5,))
                                ])

train_data=datasets.MNIST(root='./data',train=True,transform=transformer,download=True)
train_loader=DataLoader(train_data,batch_size=128,shuffle=True)
# defining discriminator Class
class Discriminator(nn.Module):
  def __init__(self):
    super(Discriminator,self).__init__()
    self.model=nn.Sequential(nn.Linear(784,1024),
                             nn.LeakyReLU(0.2),
                             nn.Dropout(0.3),
                             nn.Linear(1024,512),
                             nn.LeakyReLU(0.2),
                             nn.Dropout(0.3),
                             nn.Linear(512,256),
                             nn.LeakyReLU(0.2),
                             nn.Dropout(0.3),
                             nn.Linear(256,1),
                             nn.Sigmoid()
                             )
    
  def forward(self,x):
    out=self.model(x)
    return out
from torchsummary import summary
discriminator=Discriminator().to(device)
print(discriminator)

class Generator(nn.Module):
  def __init__(self):
    super(Generator,self).__init__()
    self.model=nn.Sequential(
        nn.Linear(100,256),
        nn.LeakyReLU(0.2),
        nn.Linear(256,512),
        nn.LeakyReLU(0.2),
        nn.Linear(512,1024),
        nn.LeakyReLU(0.2),
        nn.Linear(1024,784),
        nn.Tanh()
    )
  def forward(self,x):
    return self.model(x)
generator=Generator().to(device)

print(generator)

! pip install -q torch_snippets
from torch_snippets import *
from torchvision.utils import make_grid
def noise(size):
  n=torch.randn(size,100)
  return n.to(device)

def discriminator_train(real_data,fake_data):
  d_optimizer.zero_grad()
  prediction_real=discriminator(real_data)
  error_real=loss(prediction_real,torch.ones(len(real_data),1).to(device))
  error_real.backward()
  prediction_fake=discriminator(fake_data)
  error_fake=loss(prediction_fake,torch.zeros(len(fake_data),1).to(device))
  error_fake.backward()
  d_optimizer.step()
  return error_real+error_fake

def generator_train(fake_data):
  g_optimizer.zero_grad()
  prediction=discriminator(fake_data)
  error=loss(prediction,torch.ones(len(real_data),1).to(device))
  error.backward()
  g_optimizer.step()
  return error
discriminator=Discriminator().to(device)
generator=Generator().to(device)
d_optimizer=optim.Adam(discriminator.parameters(),lr=0.0002)
g_optimizer=optim.Adam(generator.parameters(),lr=0.0002)

loss=nn.BCELoss()
num_epochs=15
log=Report(num_epochs)




for epoch in range(num_epochs):
  N=len(train_loader)
  for i, (images,_) in enumerate(train_loader):
    real_data=images.view(len(images),-1).to(device)
    fake_data=generator(noise(len(real_data))).to(device)
    fake_data=fake_data.detach()
    d_loss=discriminator_train(real_data,fake_data)
    fake_data=generator(noise(len(real_data))).to(device)
    g_loss=generator_train(fake_data)
    log.record(epoch+(1+i)/N, d_loss=d_loss.item(),g_loss=g_loss.item(),\
               end='\r')
  log.report_avgs(epoch+1)
log.plot_epochs(['d_loss','g_loss'])
z = torch.randn(64, 100).to(device)
sample_images = generator(z).data.cpu().view(64, 1, 28, 28)
grid = make_grid(sample_images, nrow=8, normalize=True)
show(grid.cpu().detach().permute(1,2,0), sz=5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13109199895751888, 'fraction_numerical': 0.02945009121709669, 'mean_word_length': 3.149189189189189, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '292997', 'license': 'Apache-2.0', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1307, 'n_words': 169, 'path': 'Day52/gansGeneratingNumber.ipynb', 'repo_name': 'golesuman/66-days-of-data-challenge-', 'size': '3837'}