Close Menu
    Trending
    • Qantas data breach to impact 6 million airline customers
    • He Went From $471K in Debt to Teaching Others How to Succeed
    • An Introduction to Remote Model Context Protocol Servers
    • Blazing-Fast ML Model Serving with FastAPI + Redis (Boost 10x Speed!) | by Sarayavalasaravikiran | AI Simplified in Plain English | Jul, 2025
    • AI Knowledge Bases vs. Traditional Support: Who Wins in 2025?
    • Why Your Finance Team Needs an AI Strategy, Now
    • How to Access NASA’s Climate Data — And How It’s Powering the Fight Against Climate Change Pt. 1
    • From Training to Drift Monitoring: End-to-End Fraud Detection in Python | by Aakash Chavan Ravindranath, Ph.D | Jul, 2025
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Machine Learning»Day 24 — Generative Adversarial Networks (GANs) | by Ime Eti-mfon | Feb, 2025
    Machine Learning

    Day 24 — Generative Adversarial Networks (GANs) | by Ime Eti-mfon | Feb, 2025

    Team_AIBS NewsBy Team_AIBS NewsFebruary 18, 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    30 DAYS DATA SCIENCE SERIES

    • Idea: Generative fashions.
    • Implementation: Generator, discriminator.
    • Analysis: Adversarial loss.

    CONCEPT

    Generative Adversarial Networks (GANs) are a sort of deep studying framework launched by Ian Goodfellow and colleagues in 2014. GANs are used for producing new knowledge samples just like a given dataset. They encompass two neural networks: a generator and a discriminator, that are skilled concurrently in a aggressive method.

    KEY COMPONENTS

    1. Generator: Takes random noise as enter and generates faux knowledge samples.
    2. Discriminator: Takes each actual and generated knowledge samples as enter and predicts whether or not the samples are actual or faux.
    3. Adversarial Coaching: The generator and discriminator are skilled alternately: the generator goals to idiot the discriminator by producing practical samples, whereas the discriminator learns to differentiate between actual and pretend samples.

    KEY STEPS

    1. Generator Coaching: Replace the generator to attenuate the discriminator’s means to differentiate between actual and generated samples.
    2. Discriminator Coaching: Replace the discriminator to higher distinguish between actual and generated samples.

    IMPLEMENTATION

    Let’s implement a easy GAN utilizing TensorFlow/Keras to generate handwritten digits just like these within the MNIST dataset.

    # Import essential libraries

    import numpy as np
    import matplotlib.pyplot as plt
    from tensorflow.keras.datasets import mnist
    from tensorflow.keras.layers import Dense, Flatten, Reshape, Enter
    from tensorflow.keras.layers import LeakyReLU, BatchNormalization
    from tensorflow.keras.fashions import Sequential, Mannequin
    from tensorflow.keras.optimizers import Adam

    import warnings
    warnings.simplefilter(motion = 'ignore')

    # Load the MNIST dataset

    (X_train, _), (_, _) = mnist.load_data()

    # Normalize the info

    X_train = (X_train.astype(np.float32) - 127.5) / 127.5
    X_train = X_train.reshape(X_train.form[0], 784)

    # Outline the generator mannequin

    generator = Sequential([
    Dense(256, input_dim = 100),
    LeakyReLU(alpha=0.2),
    BatchNormalization(),
    Dense(512),
    LeakyReLU(alpha=0.2),
    BatchNormalization(),
    Dense(1024),
    LeakyReLU(alpha=0.2),
    BatchNormalization(),
    Dense(784, activation='tanh'),
    Reshape((28, 28))
    ])

    # Outline the discriminator mannequin

    discriminator = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(1024),
    LeakyReLU(alpha=0.2),
    Dense(512),
    LeakyReLU(alpha=0.2),
    Dense(256),
    LeakyReLU(alpha=0.2),
    Dense(1, activation='sigmoid')
    ])

    # Compile the discriminator

    discriminator.compile(optimizer=Adam(learning_rate=0.0002, beta_1=0.5), loss='binary_crossentropy', metrics=['accuracy'])

    # Compile the GAN mannequin

    discriminator.trainable = False
    gan_input = Enter(form=(100,))
    x = generator(gan_input)
    gan_output = discriminator(x)
    gan = Mannequin(gan_input, gan_output)
    gan.compile(optimizer=Adam(learning_rate=0.0002, beta_1=0.5), loss='binary_crossentropy')

    # Perform to coach the GAN

    def train_gan(epochs=1, batch_size=128):
    """
    Prepare the Generative Adversarial Community (GAN).

    Args:
    - epochs (int): Variety of epochs to coach the GAN. Default is 1.
    - batch_size (int): Batch measurement for coaching. Default is 128.
    """
    # Calculate the variety of batches per epoch
    batch_count = X_train.form[0] // batch_size

    for e in vary(epochs):
    for _ in vary(batch_count):

    # Generate random noise as enter for the generator
    noise = np.random.regular(0, 1, measurement=(batch_size, 100))

    # Generate faux photographs utilizing the generator
    generated_images = generator.predict(noise)

    # Get a random batch of actual photographs from the dataset
    batch_idx = np.random.randint(0, X_train.form[0], batch_size)
    real_images = X_train[batch_idx]

    # Concatenate actual and pretend photographs
    X = np.concatenate((real_images, generated_images))

    # Labels for generated and actual knowledge
    y_dis = np.zeros(2 * batch_size)
    y_dis[:batch_size] = 0.9 # One-sided label smoothing

    # Prepare the discriminator
    discriminator.trainable = True
    d_loss = discriminator.train_on_batch(X, y_dis)

    # Prepare the generator (through the GAN mannequin)
    noise = np.random.regular(0, 1, measurement=(batch_size, 100))
    y_gen = np.ones(batch_size)
    discriminator.trainable = False
    g_loss = gan.train_on_batch(noise, y_gen)

    # Print the progress and save the generated photographs
    print(f"Epoch {e+1}, Discriminator Loss: {d_loss[0]}, Generator Loss: {g_loss}")
    if e % 10 == 0:
    plot_generated_images(e, generator)

    # Instance utilization:
    # train_gan(epochs=100, batch_size=128)

    # Perform to plot generated photographs

    def plot_generated_images(epoch, generator, examples=10, dim=(1, 10), figsize=(10, 1)):
    noise = np.random.regular(0, 1, measurement=[examples, 100])
    generated_images = generator.predict(noise)
    generated_images = generated_images.reshape(examples, 28, 28)

    plt.determine(figsize=figsize)
    for i in vary(examples):
    plt.subplot(dim[0], dim[1], i+1)
    plt.imshow(generated_images[i], interpolation='nearest', cmap='grey')
    plt.axis('off')
    plt.tight_layout()
    plt.savefig(f'gan_generated_image_epoch_{epoch}.png')
    plt.present()

    # Prepare the GAN

    train_gan(epochs=100, batch_size=128)

    EXPLANATION OF THE CODE

    1. Knowledge Loading and Preprocessing: Load the MNIST dataset and normalize the pixel values to the vary [-1, 1].
    2. Generator Mannequin: — Sequential mannequin with a number of dense layers adopted by batch normalization and LeakyReLU activation, ending with a tanh activation layer to generate faux photographs.
    3. Discriminator Mannequin: — Sequential mannequin to categorise actual and pretend photographs, utilizing dense layers with LeakyReLU activation and a sigmoid output layer.
    4. GAN Mannequin: — Mixed mannequin the place the generator takes random noise as enter and produces faux photographs, and the discriminator is skilled to differentiate between actual and pretend photographs.
    5. Coaching Loop: — Alternately trains the discriminator and the generator on batches of actual and pretend photographs. — The generator goals to idiot the discriminator by producing practical photographs, whereas the discriminator goals to appropriately classify actual and pretend photographs.
    6. Picture Era: — Periodically saves generated photographs to visualise the coaching progress.

    APPLICATIONS

    Generative Adversarial Networks have functions in:

    • Picture Era: Producing practical photographs of faces, objects, or scenes.
    • Knowledge Augmentation: Creating new coaching examples to enhance the efficiency of machine studying fashions.
    • Picture Modifying: Modifying current photographs by altering particular attributes.
    • Textual content-to-Picture Synthesis: Producing photographs primarily based on textual descriptions.
    • Video Era: Creating new video frames primarily based on current frames.

    GANs’ means to generate high-quality, practical knowledge has led to important developments in numerous fields, together with laptop imaginative and prescient, pure language processing, and biomedical imaging.

    Obtain the Jupyter Pocket book file for Day 24 with the hyperlink beneath:



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUnlock the Power of AI in Intelligent Operations
    Next Article ‘I always have a vague, abstract idea of things that I want to do’: Peter Berg on how staying busy fuels his creativity
    Team_AIBS News
    • Website

    Related Posts

    Machine Learning

    Blazing-Fast ML Model Serving with FastAPI + Redis (Boost 10x Speed!) | by Sarayavalasaravikiran | AI Simplified in Plain English | Jul, 2025

    July 2, 2025
    Machine Learning

    From Training to Drift Monitoring: End-to-End Fraud Detection in Python | by Aakash Chavan Ravindranath, Ph.D | Jul, 2025

    July 1, 2025
    Machine Learning

    Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Qantas data breach to impact 6 million airline customers

    July 2, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    What Is Feature Engineering — and Why It Matters So Much? | by Karan Kumar | May, 2025

    May 2, 2025

    Build and Query Knowledge Graphs with LLMs

    May 3, 2025

    Will Stargate liftoff?. The internet has been abuzz with the… | by Darryl Williams II | Jan, 2025

    January 23, 2025
    Our Picks

    Qantas data breach to impact 6 million airline customers

    July 2, 2025

    He Went From $471K in Debt to Teaching Others How to Succeed

    July 2, 2025

    An Introduction to Remote Model Context Protocol Servers

    July 2, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.