<jupyter_start><jupyter_text><jupyter_code>import keras
from keras.datasets import mnist
from keras.layers import *
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential,Model
from tensorflow.keras.optimizers import Adam # - Works

#from keras.optimizers import Adam
import warnings
import numpy as np
import matplotlib.pyplot as plt

(X_train,_),(_,_)=mnist.load_data()
X_train.shape
X_train=(X_train - 127.5) /127.5
X_train.min()
X_train.max()
EPOCH=50
BATCH_SIZE=250
HALF_BATCH=128
NO_OF_BATCH=int(X_train.shape[0]/BATCH_SIZE)
NO_OF_BATCH
NOISE_DIM=100
adam=Adam(lr=2e-4,beta_1=0.5)
generator=Sequential()
generator.add(Dense(units=7*7*128,input_shape=(NOISE_DIM,)))
generator.add(Reshape((7,7,128)))
generator.add(LeakyReLU(0.2))
generator.add(BatchNormalization())
generator.add(Conv2DTranspose(64,(3,3),strides=(2,2),padding='same'))
generator.add(LeakyReLU(0.2))
generator.add(BatchNormalization())
generator.add(Conv2DTranspose(1,(3,3),strides=(2,2),padding='same',activation='tanh'))
generator.compile(loss=keras.losses.binary_crossentropy,optimizer=adam)
generator.summary()
descriminator=Sequential()
descriminator.add(Conv2D(64,(3,3),strides=(2,2),padding='same',input_shape=(28,28,1)))
descriminator.add(LeakyReLU(0.2))
descriminator.add(Conv2D(128,(3,3),strides=(2,2),padding='same'))
descriminator.add(LeakyReLU(0.2))
descriminator.add(Flatten())

descriminator.add(Dense(100))
descriminator.add(LeakyReLU(0.2))
descriminator.add(Dense(1,activation='sigmoid'))
descriminator.compile(loss=keras.losses.binary_crossentropy,optimizer=adam)
descriminator.summary()

descriminator.trainable=False
gen_input=Input(shape=(NOISE_DIM,))
gen_img=generator(gen_input)
gen_out=descriminator(gen_img)
model=Model(gen_input,gen_out)
model.compile(loss=keras.losses.binary_crossentropy,optimizer=adam)
model.summary()

X_train=X_train.reshape(-1,28,28,1)
X_train.shape
def display_images(samples=25):
  noise=np.random.normal(0,1,size=(samples,NOISE_DIM))
  generated_img=generator.predict(noise)
  plt.figure(figsize=(10,10))
  for i in range(samples):
    plt.subplot(5,5,i+1)
    plt.imshow(generated_img[i].reshape(28,28),cmap='binary')
    plt.axes('off')
plt.show()    

d_losses=[]
g_losses=[]
for epoch in range(EPOCH):
  epoch_d_loss=0.0
  epoch_g_loss=0.0
  for step in range(NO_OF_BATCH):
    idx=np.random.randint(0,60000,HALF_BATCH)
    real_img=X_train[idx]
    noise=np.random.normal(0,1,size=(HALF_BATCH,NOISE_DIM))
    fake_img=generator.predict(noise)
    real_y=np.ones((HALF_BATCH,1))*0.9
    fake_y=np.zeros((HALF_BATCH,1))
    d_loss_real=descriminator.train_on_batch(real_img,real_y)
    d_loss_fake=descriminator.train_on_batch(fake_img,fake_y)
    d_loss=0.5*d_loss_real + 0.5*d_loss_fake
    epoch_d_loss += d_loss
    #######################
    noise=np.random.normal(0,1,size=(BATCH_SIZE,NOISE_DIM))
    ground_truth_y=np.ones((BATCH_SIZE,1))
    g_loss=model.train_on_batch(noise,ground_truth_y)
    epoch_g_loss += g_loss
    print(f"EPOCH{epoch+1},disc loss {epoch_d_loss/NO_OF_BATCH},Generator loss {epoch_g_loss/NO_OF_BATCH}")
    d_losses.append(epoch_d_loss/NO_OF_BATCH)
    g_losses.append(epoch_g_loss/NO_OF_BATCH)
    if (epoch +1) % 10 ==0:
      generator.save('generator.h5')
      display_images()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15568319226118502, 'fraction_numerical': 0.03990326481257558, 'mean_word_length': 8.090659340659341, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '453355', 'license': 'MIT', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1361, 'n_words': 137, 'path': 'dcGan.ipynb', 'repo_name': 'mo-9/GAN', 'size': '3308'}