Generative AI: Types, Example Code, and Real-Life Use Cases
Rajasaravanan M
Head of IT Department @ Exclusive Networks ME | Cyber Security, Data Management | ML | AI| Project Management | NITK
Generative AI has revolutionized various industries by enabling the creation of realistic, high-quality content across different modalities. This paper explores the types of generative AI models, delves into their mathematical underpinnings, presents example code, and highlights real-life applications.
1. Generative Adversarial Networks (GANs)
Description
GANs consist of two neural networks—a generator and a discriminator—trained in a competitive setting. The generator creates data resembling real-world examples, while the discriminator evaluates its authenticity.
Example Code
import tensorflow as tf
from tensorflow.keras import layers
# Generator model
def build_generator():
model = tf.keras.Sequential([
layers.Dense(128, activation="relu", input_dim=100),
layers.BatchNormalization(),
layers.Dense(256, activation="relu"),
layers.BatchNormalization(),
layers.Dense(28 * 28, activation="tanh"),
layers.Reshape((28, 28, 1))
])
return model
# Discriminator model
def build_discriminator():
model = tf.keras.Sequential([
layers.Flatten(input_shape=(28, 28, 1)),
layers.Dense(128, activation="relu"),
layers.Dropout(0.3),
layers.Dense(1, activation="sigmoid")
])
return model
gen = build_generator()
disc = build_discriminator()
Real-Life Use Cases
2. Variational Autoencoders (VAEs)
Description
VAEs are probabilistic models that encode input data into a latent space and reconstruct it, enabling new data generation.
Example Code
import tensorflow as tf
from tensorflow.keras import layers
# Encoder
encoder = tf.keras.Sequential([
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.Dense(20) # Mean and log variance for latent space
])
# Decoder
decoder = tf.keras.Sequential([
layers.Dense(128, activation="relu", input_dim=10),
layers.Dense(28 * 28, activation="sigmoid"),
layers.Reshape((28, 28))
])
Real-Life Use Cases
3. Diffusion Models
Description
Diffusion models add noise to data during training and learn to reverse this noise to generate new samples.
Example Code
import torch
import torch.nn as nn
class SimpleDiffusionModel(nn.Module):
def __init__(self):
super().__init__()
self.denoiser = nn.Sequential(
nn.Linear(100, 256),
nn.ReLU(),
nn.Linear(256, 100)
)
def forward(self, x):
return self.denoiser(x)
model = SimpleDiffusionModel()
Real-Life Use Cases
4. Transformer-Based Models
Description
Transformers leverage attention mechanisms, making them effective for sequential data generation, such as text and speech.
Example Code
from transformers import GPT2LMHeadModel, GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
input_text = "The future of AI is"
input_ids = tokenizer.encode(input_text, return_tensors="pt")
output = model.generate(input_ids, max_length=50)
print(tokenizer.decode(output[0], skip_special_tokens=True))
Real-Life Use Cases
5. Autoregressive Models
Description
These models generate sequences element by element, conditioning each output on previous ones.
Example Code
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.LSTM(128, return_sequences=True, input_shape=(100, 10)),
tf.keras.layers.Dense(1)
])
# Predict the next sequence
output = model(tf.random.normal([1, 100, 10]))
Real-Life Use Cases
6. Reinforcement Learning-Based Generative Models
Description
Reinforcement learning optimizes generative models by rewarding outputs that meet desired criteria.
Example Code
import gym
import numpy as np
env = gym.make("CartPole-v1")
state = env.reset()
for _ in range(1000):
action = env.action_space.sample() # Random action
state, reward, done, _ = env.step(action)
if done:
state = env.reset()
Real-Life Use Cases
7. Hybrid Models
Description
Hybrid models combine the strengths of different generative techniques, such as VAEs and GANs.
Example Code
# Example: VAE-GAN
class VAEDiscriminator(nn.Module):
def __init__(self):
super().__init__()
self.main = nn.Sequential(
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.main(x)
Real-Life Use Cases
8. Text-to-Anything Models
Description
These models generate content across multiple modalities, such as images, 3D models, and videos, from text prompts.
Example Code
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
output = pipeline("A futuristic cityscape at night")
output["images"][0].show()
Real-Life Use Cases
9. Audio Generative Models
Description
These models focus on generating or synthesizing audio content, including speech and music.
Example Code
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
Real-Life Use Cases
10. Bioinformatics Generative Models
Description
These models simulate biological data or predict complex biological structures.
Example Code
import biopython as bp
# Simulated code for protein folding prediction
Real-Life Use Cases
Conclusion
Generative AI spans diverse methodologies, each tailored to specific applications. By understanding these types, developers and researchers can harness the power of generative AI to create innovative solutions across industries.
#GenerativeAI#ArtificialIntelligence#DeepLearning#MachineLearning#AIResearch#GenerativeModels#NeuralNetworks#AIApplications#FutureOfAI#DataScience#NaturalLanguageProcessing#AIInnovation#GANs#TransformerModels#TechResearch#AIUseCases#AIEthics