0% found this document useful (0 votes)
26 views70 pages

Generative Ai - Record

The document outlines the curriculum and structure for the NM1081 - Generative AI course at PET Engineering College, detailing the course's focus on artificial intelligence, its history, types, and applications across various sectors. It includes a bonafide certificate for students, a list of experiments, and an introduction to OpenAI's GPT models, highlighting their architecture, training processes, and applications. The document also addresses the challenges and ethical considerations surrounding AI development and deployment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views70 pages

Generative Ai - Record

The document outlines the curriculum and structure for the NM1081 - Generative AI course at PET Engineering College, detailing the course's focus on artificial intelligence, its history, types, and applications across various sectors. It includes a bonafide certificate for students, a list of experiments, and an introduction to OpenAI's GPT models, highlighting their architecture, training processes, and applications. The document also addresses the challenges and ethical considerations surrounding AI development and deployment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

PET ENGINEERING COLLEGE

(An ISO 9001:2015 Certified Institution)


Affiliated to Anna university & Approved by AICTE

Department of Mechanical Engineering

DEPARTMENT OF MECHANICAL ENGINEERING

NM1081 – GENERATIVE AI

NAME : _______________________

REGISTER NUMBER : _______________________

BRANCH : _______________________

YEAR/SEM : _______________________
PET ENGINEERING COLLEGE
(An ISO 9001:2015 Certified Institution)
Affiliated to Anna university & Approved by AICTE

Department of Mechanical Engineering

BONAFIDE CERTIFICATE

Certified that this is bonafide record of work done by _________________________

of the Fourth Semester in Mechanical Engineering of this College in the NM1081

– GENERATIVE AI during 2023-2027 in Partial fulfillment of the requirements of

the B.E. Degree Course of the Anna University.

Staff in-Charge Head of the Department

University Registration Number : _________________________

University Examination held on : _________________________

Internal Examiner External Examiner


CONTENTS

EX. DATE EXPERIMENT NAME PAGE STAFF


NO NO SIGNATURE

1 Introduction to Artificial Intelligence

2 Introduction to OpenAI GPT Models

3 Generative Models for Developers

4 AI-First Software Engineering

Prompt Engineering
5
OpenAI Generative Pre-Trained
6 Transformer 3 (Gpt-3) for Developers

7 Generating SQL Commands

8 Information Extraction

9 Recipe Generator

10 Image Reshape

11 Guided Project

12 Capstone Project 1

13 Capstone Project 2
1

1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE

What is Artificial Intelligence (AI)?

Artificial Intelligence (AI) refers to the simulation of human intelligence in machines.


These machines are programmed to think like humans, reason, learn from experience, and
perform tasks that typically require human intelligence. AI systems can perform a wide
range of functions—from recognizing speech and images to making decisions and
predicting outcomes.

The term "Artificial Intelligence" was coined by John McCarthy in 1956 during the
Dartmouth Conference. Since then, AI has grown from simple rule-based systems to
complex neural networks and machine learning models.

AI works by processing large amounts of data and identifying patterns. It combines inputs
(data), algorithms (processing), and outputs (actions or decisions). AI doesn’t “think” like
humans, but it can mimic human-like decision-making processes to a remarkable degree.

AI is transforming industries, enabling automation, and opening new possibilities in


everything from healthcare to education, finance to entertainment.

History and Evolution of AI

The Early Days (1940s–1950s):

The roots of AI can be traced to classical philosophers who attempted to describe human
thinking as a symbolic system. The modern history of AI began with the development of
digital computers in the 1940s. Alan Turing's famous paper, “Computing Machinery and
Intelligence,” posed the question, “Can machines think?”

The Birth of AI (1956):

The Dartmouth Conference of 1956, organized by John McCarthy, Marvin Minsky,


Nathaniel Rochester, and Claude Shannon, marked the official beginning of AI as a field.
Early work focused on symbolic reasoning and problem-solving.

The First AI Winter (1970s):


2

After initial enthusiasm, AI hit a roadblock due to unrealistic expectations and limited
computing power, leading to reduced funding and interest—this period is known as the
first AI winter.

Revival and Expert Systems (1980s):

AI saw a resurgence with the development of expert systems that mimicked decision-
making abilities of human experts. MYCIN and DENDRAL were early examples.

Second AI Winter (Late 1980s–1990s):

Again, progress slowed due to the high cost of developing expert systems and their
limitations. Funding was withdrawn, leading to the second AI winter.

Modern AI (2000s–Present):

The explosion of data (big data), increased computational power (GPUs), and the
development of machine learning and deep learning models led to a renaissance in AI.
Breakthroughs in NLP (e.g., GPT models), computer vision, and robotics marked the dawn
of the AI era we see today.

Types of Artificial Intelligence

AI can be categorized based on capabilities and functionalities:

Based on Capabilities:

1. Narrow AI (Weak AI): Performs specific tasks (e.g., facial recognition, voice
assistants).

2. General AI (Strong AI): Can perform any intellectual task that a human can do
(still theoretical).

3. Super AI: Surpasses human intelligence in all aspects (a hypothetical future


concept).

Based on Functionalities:

1. Reactive Machines: Respond to specific inputs without memory (e.g., IBM’s


Deep Blue).
3

2. Limited Memory: Can use past experiences for future decisions (e.g., self-driving
cars).

3. Theory of Mind: AI understands emotions and social behavior (under


development).

4. Self-aware AI: Has its own consciousness and self-awareness (currently


theoretical).

Subfields of AI

AI is an umbrella term that encompasses multiple subfields:

1. Machine Learning (ML):

ML enables machines to learn from data and improve over time without being explicitly
programmed.

2. Deep Learning (DL):

A subset of ML involving neural networks with many layers, particularly good at


processing images and speech.

3. Natural Language Processing (NLP):

Allows machines to understand, interpret, and generate human language (e.g., translation,
chatbots).

4. Computer Vision:

Enables machines to see and interpret visual information from the world.

5. Robotics:

Combines AI with mechanical systems to develop intelligent machines capable of physical


interaction.

6. Expert Systems:

Rule-based systems that mimic human experts to solve complex problems in specific
domains.
4

7. Fuzzy Logic:

Deals with reasoning that is approximate rather than fixed and exact.

Each subfield plays a vital role in building intelligent systems and enhancing machine
capabilities across industries.

Key Technologies Enabling AI

AI has become viable due to advancements in various technologies:

 Big Data: Provides the vast amount of information needed to train models.

 Cloud Computing: Offers the computational infrastructure required for large-


scale AI.

 High-Performance GPUs: Accelerate deep learning model training.

 IoT Devices: Generate real-time data that can be analyzed by AI systems.

 Edge Computing: Allows AI processing to happen closer to data sources for faster
responses.

AI development also benefits from open-source platforms like TensorFlow, PyTorch, and
Scikit-learn, making AI research and implementation more accessible.

Machine Learning and Deep Learning

Machine Learning (ML):

ML is a key component of AI that enables machines to learn from data and make
predictions or decisions.

Types of ML:

1. Supervised Learning: Uses labeled data to train models (e.g., spam detection).

2. Unsupervised Learning: Finds patterns in unlabeled data (e.g., customer


segmentation).

3. Reinforcement Learning: Models learn by trial and error using rewards (e.g.,
game playing).
5

Deep Learning (DL):

Deep learning uses artificial neural networks to simulate human brain function. It is ideal
for tasks like speech recognition, image classification, and natural language generation.

DL models require large datasets and high computation, but they offer superior
performance for complex tasks.

Applications of AI in Different Sectors

AI is transforming nearly every industry:

1. Healthcare:

 Disease prediction and diagnosis

 Personalized medicine

 AI-assisted surgeries

2. Finance:

 Fraud detection

 Algorithmic trading

 Customer service chatbots

3. Retail and E-Commerce:

 Recommendation engines

 Inventory management

 Personalized marketing

4. Manufacturing:

 Predictive maintenance

 Quality control

 Robotics in assembly lines

5. Education:
6

 Intelligent tutoring systems

 Automated grading

 Adaptive learning platforms

6. Transportation:

 Autonomous vehicles

 Route optimization

 Traffic prediction

7. Agriculture:

 Crop monitoring

 Yield prediction

 Automated irrigation

8. Entertainment:

 Content recommendations (Netflix, Spotify)

 AI-generated art, music, and stories

These applications enhance efficiency, reduce costs, and improve user experiences.

Challenges and Limitations of AI

Despite rapid advancement, AI has several challenges:

 Data Quality and Quantity: Poor or biased data leads to poor models.

 Lack of Explainability: Deep learning models often work as “black boxes.”

 Security and Privacy Concerns: AI systems can be manipulated or leak sensitive


data.

 Dependency on Resources: High cost of training large models.

 Regulatory and Legal Challenges: There is still a lack of clear AI governance


globally.
7

Overcoming these challenges requires collaboration among governments, researchers, and


industries.

Ethics and Responsible AI

The power of AI necessitates responsible development and deployment:

Key Ethical Concerns:

 Bias and Fairness: AI can perpetuate or amplify biases present in training data.

 Transparency: Users need to understand how AI decisions are made.

 Accountability: Who is responsible if AI makes a mistake?

 Job Displacement: AI automation may lead to unemployment in some sectors.

 Surveillance and Autonomy: AI-powered surveillance raises civil liberty


concerns.

Principles of Responsible AI:

 Human-centered design

 Fairness and inclusivity

 Data privacy and security

 Open communication and governance

Organizations like UNESCO and the EU are actively working on global AI ethics
frameworks.

The Future of Artificial Intelligence

AI is expected to evolve in several exciting ways:

 General AI: Capable of performing a wide range of tasks like a human.

 Multimodal AI: Combining text, image, video, and audio inputs (e.g., GPT-4).

 Smarter and Leaner Models: More efficient models for mobile and edge devices.

 AI in Creativity: Generating music, art, and code autonomously.


8

 Human-AI Collaboration: AI assisting humans in complex decision-making


rather than replacing them.

Continuous research, innovation, and ethical oversight will shape the trajectory of AI in
the coming decades.

Conclusion

Artificial Intelligence is reshaping the world by enabling machines to learn, adapt, and
make decisions. From its theoretical origins to its practical applications across sectors, AI
has emerged as a key driver of innovation and efficiency.

The field is broad, encompassing machine learning, NLP, computer vision, and robotics,
and it’s powered by breakthroughs in algorithms, hardware, and data availability.
9

2. INTRODUCTION TO OPENAI GPT MODELS


Introduction to Artificial Intelligence and Natural Language Processing

Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that
are programmed to think and learn like humans. One of the most prominent subfields of
AI is Natural Language Processing (NLP), which focuses on enabling machines to
understand, interpret, and generate human language.

In recent years, NLP has seen exponential growth due to advancements in machine
learning, access to massive datasets, and computational power. From chatbots and virtual
assistants to language translation and summarization, NLP applications have become
deeply integrated into our daily lives.

Language models like GPT represent a significant leap in NLP capabilities. These models
are trained on vast corpora of text and can perform a wide variety of language-related tasks
with minimal task-specific tuning.

Evolution of Language Models

Language modeling has evolved from basic statistical models to deep learning-based
transformers:

 Rule-Based and Statistical Models: Early models relied on manually crafted


rules and statistical probabilities such as N-grams to predict the next word in a
sequence.

 Recurrent Neural Networks (RNNs): These models introduced a way to handle


sequential data but struggled with long-term dependencies.

 Long Short-Term Memory (LSTM): LSTMs improved upon RNNs by


preserving information over longer sequences but still had limitations in scalability
and context management.

 Transformer Architecture: Introduced by Vaswani et al. in 2017, transformers


revolutionized NLP with self-attention mechanisms and parallel processing
capabilities.
10

 Generative Pretrained Transformers (GPT): GPT models by OpenAI built


upon transformers, pushing boundaries by pretraining on large corpora and fine-
tuning on downstream tasks.

Introduction to OpenAI and GPT

OpenAI is an AI research and deployment company with a mission to ensure that artificial
general intelligence (AGI) benefits all of humanity. One of its groundbreaking
contributions is the development of the GPT (Generative Pre-trained Transformer)
series of models.

 GPT-1 (2018): Introduced the concept of generative pretraining and demonstrated


promising results across NLP tasks.

 GPT-2 (2019): Gained attention for its ability to generate coherent and
contextually relevant text. Due to its capabilities, OpenAI initially withheld full
release for safety concerns.

 GPT-3 (2020): With 175 billion parameters, it significantly outperformed its


predecessors, becoming capable of performing tasks like question answering,
translation, and summarization with minimal examples.

 GPT-4 and Beyond: GPT-4 improved in accuracy, reasoning, and instruction-


following. These models are integrated into tools like ChatGPT, Microsoft
Copilot, and more.

Architecture of GPT Models

GPT models are based on the transformer decoder architecture. Key components include:

 Embedding Layer: Converts input tokens into vectors.

 Positional Encoding: Adds sequence information to the token vectors.

 Self-Attention Mechanism: Allows the model to weigh the importance of


different words in the input.

 Feedforward Neural Networks: Applies non-linear transformations.


11

 Layer Normalization and Residual Connections: Helps with training stability


and performance.

GPT uses a unidirectional (left-to-right) attention mechanism which differs from BERT’s
bidirectional approach. This design is ideal for tasks like text generation, where predicting
the next word is essential.

The number of parameters (weights) increases with each GPT version, leading to improved
performance and context understanding. For example, GPT-3 has 175 billion parameters
while GPT-4 is even more capable, though its exact size is not officially disclosed.

Training Process and Datasets

Training GPT models involves two major phases:

 Pretraining: The model is trained on a massive corpus of internet text (e.g., books,
articles, websites) using unsupervised learning. The objective is to predict the next
word in a sentence, learning grammar, facts, and reasoning patterns.

 Fine-tuning: Optionally, the model can be fine-tuned on a smaller, task-specific


dataset using supervised learning or reinforcement learning from human feedback
(RLHF).

Training is computationally expensive and requires extensive infrastructure including


GPU/TPU clusters. Datasets are curated for quality and diversity, although ensuring
unbiased and safe content is a challenge.

Applications of GPT Models

GPT models are versatile and can be applied in numerous domains:

 Conversational Agents: Chatbots, virtual assistants, and customer service


automation.

 Content Generation: Writing articles, poetry, code, and social media posts.

 Translation and Summarization: High-quality language translation and


document summarization.

 Education: Tutoring, question-answering, and interactive learning platforms.


12

 Programming Help: Code generation, explanation, and debugging (e.g., GitHub


Copilot).

 Business Intelligence: Analyzing and summarizing reports, extracting insights.

Industry applications are rapidly expanding, including healthcare, legal, finance,


entertainment, and more. GPT's adaptability reduces the need for building separate models
for each task.

Limitations and Challenges

Despite its capabilities, GPT has limitations:

 Factual Inaccuracy: Sometimes "hallucinates" or generates incorrect


information.

 Lack of True Understanding: GPT models mimic understanding but don’t


possess real-world experience or reasoning.

 Context Length Limitations: Earlier versions struggled with long documents.


GPT-4 improves on this but is not perfect.

 Data Biases: The training data may include biased or harmful content, which can
be reflected in outputs.

 Resource Intensity: Training and running large models is expensive and energy-
intensive.

These limitations underscore the importance of combining human oversight with AI


systems.

Ethical Considerations and Responsible AI

As powerful AI systems become widespread, ethical considerations become critical:

 Bias and Fairness: Ensuring GPT models do not reinforce societal biases or
discrimination.

 Misinformation: Preventing the spread of false information or malicious content.


13

 Privacy: Avoiding leakage of sensitive data that may have been present in training
datasets.

 Misuse: Regulating the deployment of AI tools for harmful purposes like


impersonation or deepfakes.

OpenAI and others advocate for Responsible AI practices, including transparency, human-
in-the-loop systems, usage guidelines, and alignment research.

Future of GPT and Language Models

The future of GPT and generative AI is promising and still unfolding:

 Multimodal Models: Integration of text, images, audio, and video inputs (e.g.,
GPT-4 with image capabilities).

 Specialized Models: Fine-tuned variants for medical, legal, scientific domains.

 Smaller, Efficient Models: Research is ongoing to create lightweight models for


on-device applications.

 AI Alignment and Interpretability: Enhancing transparency and aligning models


with human values.

 Open Source Innovations: Community-driven models like LLaMA, Falcon, and


Mistral offer alternatives and foster competition.

We’re moving toward AI systems that can collaborate with humans across various
domains, supporting creativity, productivity, and decision-making.

Conclusion

"Introduction to OpenAI GPT Models" is not just about understanding a technology—it's


about envisioning a future where humans and machines work together ethically, creatively,
and responsibly.
14

3. GENERATIVE MODELS FOR DEVELOPERS

Introduction to Generative Models

Generative models are a class of machine learning models that can generate new data
instances that resemble a given dataset. Unlike discriminative models that focus on
classifying input data, generative models learn the distribution of input data and can
produce synthetic outputs such as text, images, videos, music, and more.

For developers, these models offer powerful capabilities to simulate data, enhance
creativity, and solve real-world problems through AI-generated content. With the rise of
tools like ChatGPT, DALL·E, and Stable Diffusion, understanding generative models has
become essential for AI development.

Key advantages of generative models include:

 Data augmentation for training ML models

 Automation in content creation

 Simulating real-world scenarios in training environments

 Enabling human-computer collaboration

Historical Background and Evolution

The journey of generative models began with the development of probabilistic models like
Gaussian Mixture Models and Hidden Markov Models. As computational power increased,
so did the complexity of these models.

Key historical milestones include:

 1980s–1990s: Probabilistic graphical models laid the foundation.

 2006: Geoffrey Hinton introduced deep belief networks, sparking interest in deep
learning.

 2014: Ian Goodfellow proposed Generative Adversarial Networks (GANs),


revolutionizing generative learning.
15

 2016–2020: Introduction of VAEs and Transformer architectures pushed the


envelope further.

 2020s: GPT-3, DALL·E, and other large-scale generative models made public
waves.

The convergence of computing, large-scale data, and research advancements has made
generative models highly accessible and impactful for developers.

Core Concepts and Terminology

Before diving deeper, developers should be familiar with the following key concepts:

 Latent Space: An abstract, multidimensional space where data representations


(like embeddings) are encoded.

 Loss Functions: Specific functions like adversarial loss or reconstruction loss


guide model optimization.

 Sampling: Drawing outputs from learned distributions, such as in text generation


or image synthesis.

 Overfitting and Mode Collapse: Common pitfalls in generative training.

 Conditional vs. Unconditional Generation: Conditional models generate outputs


based on an input label or context; unconditional models generate freely from
noise or prompts.

Grasping these basics helps developers troubleshoot and fine-tune generative models
effectively.

Types of Generative Models

There are various architectures and approaches to building generative models, including:

1. Autoregressive Models

These models predict future data points based on past ones, making them ideal for
sequential data like language and audio.

Examples:
16

 GPT (Generative Pre-trained Transformer)

 PixelRNN

2. Autoencoders

These neural networks learn compressed representations and reconstruct data. The latent
space can be sampled to generate new instances.

 Basic Autoencoders

 Denoising Autoencoders

3. Variational Autoencoders (VAEs)

Extend traditional autoencoders with a probabilistic approach to learn distributions over


the latent space.

4. GANs (Generative Adversarial Networks)

Involve a generator and discriminator in a minimax game. The generator tries to produce
realistic data, while the discriminator attempts to distinguish between real and fake.

5. Flow-based Models

Use invertible transformations to learn data distribution and allow exact likelihood
computation.

 RealNVP

 Glow

Each of these models has trade-offs in terms of training complexity, quality of output, and
controllability.

Generative Adversarial Networks (GANs)

What Are GANs?

Introduced in 2014, GANs consist of two neural networks:

 Generator: Learns to create fake data from random noise.

 Discriminator: Evaluates whether data is real or generated.


17

They are trained simultaneously, with the generator trying to “fool” the discriminator.

Types of GANs

 DCGAN (Deep Convolutional GANs)

 Conditional GANs (cGANs)

 StyleGAN (used in face generation)

 CycleGAN (for image-to-image translation)

Training Challenges

 Mode collapse

 Instability

 Non-convergence

Best Practices for Developers

 Use batch normalization

 Tune learning rates carefully

 Apply regularization

 Visualize outputs regularly

GANs are widely used for:

 Image synthesis

 Art generation

 Super-resolution

Variational Autoencoders (VAEs)

VAEs bridge the gap between deep learning and probabilistic inference. They learn a latent
variable model by optimizing the evidence lower bound (ELBO).

Key Characteristics:

 Generate diverse and continuous output


18

 Require KL-divergence and reconstruction loss

Applications:

 Anomaly detection

 Text generation

 Drug discovery

VAEs offer more controlled generation than GANs but may produce blurrier images in
visual tasks.

Transformer-based Generative Models

Transformers have redefined generative modeling, especially in NLP.

Key Innovations:

 Attention Mechanism: Focuses on relevant parts of input.

 Large-scale Pretraining: Models trained on massive datasets generalize better.

Popular Models:

 GPT (Generative Pre-trained Transformer)

 BERT (Bidirectional Encoder Representations from Transformers)

 T5, BART, and PaLM

These models are the foundation of:

 Chatbots

 Summarization tools

 Code generation engines (e.g., GitHub Copilot)

Developers can use Hugging Face libraries and OpenAI APIs to fine-tune or deploy these
models.

Tools and Frameworks for Developers

Here are key tools used by developers to work with generative models:
19

 TensorFlow/Keras: Widely used for building and training models.

 PyTorch: Preferred by researchers for dynamic computation.

 Hugging Face Transformers: Pre-trained models and tokenizers.

 OpenAI API: Access GPT models via simple APIs.

 Weights & Biases: For monitoring training and experiments.

 RunwayML and Replicate: Platforms for deploying generative AI without deep


coding.

These tools reduce development time and allow experimentation with cutting-edge models.

Use Cases and Applications

1. Text Generation

 AI writing assistants

 Code generation

2. Image Generation

 AI art and avatars

 Product visualization

3. Music and Audio

 AI music composition

 Voice cloning

 Audio enhancement

4. Game Development

 Procedural content generation

 Virtual characters and NPCs

5. Healthcare

 Synthetic data for model training


20

 Medical imaging simulation

6. Education and Research

 Quiz and content generation

 Literature reviews

Generative AI opens doors to creative, personalized, and automated digital solutions across
domains.

Challenges in Building and Deploying Generative Models

While powerful, generative models come with hurdles:

 Data Requirements: High-quality data is essential.

 Computational Costs: Training large models demands GPUs/TPUs.

 Evaluation Complexity: Hard to quantify creativity or realism.

 Overfitting: Models may memorize training data.

 Latency and Real-time Use: Deploying in production environments needs


optimization.

Mitigating these issues requires knowledge of model compression, transfer learning, and
scalable architecture.

Ethical Considerations and Responsible AI

Generative models can create realistic yet synthetic content, which raises serious ethical
questions:

Key Concerns:

 Deep fakes and Misinformation

 Plagiarism and Copyright

 Bias and Fairness

 Privacy Violations
21

Developer Responsibilities:

 Ensure transparency in model outputs

 Audit datasets for bias

 Watermark AI-generated content

 Educate users on risks

Following guidelines from AI ethics boards and regulatory frameworks is essential for
responsible development.

Future Trends and Developer Opportunities

The future of generative models is rich with innovation:

 Multimodal Models: Combine image, text, video, and audio.

 Real-time Generation: For gaming, simulation, and AR/VR.

 AI Co-creators: Collaborate with humans in music, design, writing.

 On-device Generation: Efficient models for mobile and IoT.

 Synthetic Data for AI: Expand training sets while preserving privacy.

Developers skilled in AI pipelines, prompt engineering, and model fine-tuning will be in


high demand.

Conclusion

Generative models are revolutionizing how developers create and interact with data. From
simple text generators to complex multimodal architectures, these tools empower
developers to automate, innovate, and elevate user experiences.

With increasing access to frameworks and pre-trained models, developers can integrate
generative capabilities into applications across industries. However, they must also address
ethical concerns and technical limitations responsibly.

The journey of generative AI is only beginning. By mastering these tools, developers can
not only build the future but imagine it.
22

4. AI-FIRST SOFTWARE ENGINEERING

Introduction to AI-first Paradigm

The AI-first paradigm in software engineering marks a transformative shift where artificial
intelligence is no longer just a component but the driving force of the entire development
lifecycle. Unlike traditional methodologies that view AI as an add-on, AI-first software
engineering embeds machine learning (ML), natural language processing (NLP), and data-
driven automation at the core of software design, development, deployment, and evolution.

In the AI-first model, software isn’t merely programmed — it learns, adapts, and evolves.
Applications are expected to anticipate user needs, automate tasks intelligently, and
optimize themselves in real-time. This evolution has been made possible by rapid advances
in deep learning, the availability of massive datasets, and high-performance computing.

Infosys Springboard’s “AI-first Software Engineering” course introduces this modern


approach, preparing developers and engineers to integrate AI across the software
development lifecycle (SDLC). It emphasizes the importance of reimagining roles, tools,
processes, and even user expectations in this new era of intelligent software.

Evolution of Software Engineering in the Age of AI

Software engineering has evolved dramatically from manual coding and static architecture
to agile, DevOps, and now AI-augmented pipelines. Initially, development was code-
centric and linear. Agile introduced iterative and incremental progress, and DevOps further
integrated operations for continuous delivery.

However, AI has introduced a paradigm shift:

 Pre-AI Era: Human-centric decision-making, manual bug detection, rule-based


logic.

 Transition Era: Automated testing, basic analytics, initial ML integrations.

 AI-first Era: AI predicts bugs, generates code, analyzes user behavior, and
optimizes performance autonomously.
23

The AI-first approach alters traditional boundaries. Development becomes data-driven, and
engineering processes are infused with learning mechanisms that make software
increasingly autonomous.

Key Principles of AI-first Software Development

The AI-first methodology rests on several core principles:

1. Data-centric Development: Data becomes the fuel for learning, improvement,


and personalization.

2. Learning-enabled Systems: Software can adapt and evolve over time using ML
models.

3. Automation First: From code generation to deployment, automation is


prioritized.

4. Continuous Optimization: AI refines performance, UX, and resource utilization.

5. Human-AI Collaboration: Developers work alongside AI tools (e.g., Copilot,


Codex).

These principles guide software engineers to build systems that are intelligent by design
rather than by integration.

AI in Software Requirements Engineering

In requirements engineering, AI helps by extracting, analyzing, and validating


requirements using natural language understanding (NLU) and pattern recognition:

 Text Mining: AI analyzes existing documents, emails, and tickets to identify


requirements.

 Sentiment Analysis: Determines stakeholder intent and urgency.

 Requirement Validation: AI can identify inconsistencies and ambiguities.

 Predictive Modeling: Forecasts future requirements based on historical data.

Using AI in this stage reduces communication gaps and ensures that software reflects real
user needs.
24

AI-powered Design and Architecture

AI enhances design by helping architects and designers simulate, validate, and optimize
software models:

 Generative Design: AI proposes multiple design solutions based on constraints.

 Architecture Search: Automated discovery of optimal software architectures


using reinforcement learning.

 Impact Analysis: AI assesses how design decisions affect performance and


scalability.

 UML/NLP Converters: Convert plain text requirements to UML diagrams using


NLP.

This approach significantly accelerates and improves the decision-making process during
software design.

Intelligent Coding Assistants and Code Generation

AI tools like GitHub Copilot, Amazon CodeWhisperer, and OpenAI Codex have
revolutionized coding:

 Auto-complete & Suggestions: Predicts lines or blocks of code in real time.

 Code Translation: Converts code between languages or from pseudocode.

 Bug Detection: Identifies errors before execution using static and semantic
analysis.

 Documentation Generation: Writes function-level or module-level


documentation automatically.

These assistants not only improve productivity but also enhance code quality and reduce
onboarding time for developers.

AI in Testing and Quality Assurance

Software testing benefits greatly from AI-driven automation:

 Test Case Generation: AI generates test cases based on code and requirements.
25

 Test Optimization: Prioritizes tests based on historical defect data and impact
prediction.

 Visual Testing: AI compares UI changes to expected baselines.

 Anomaly Detection: Identifies performance or logic anomalies during testing.

AI ensures higher coverage, faster testing cycles, and early detection of issues—especially
critical in agile environments.

Deployment, Monitoring, and Feedback Loops using AI

In deployment, AI facilitates:

 Predictive Scaling: Anticipates resource needs and adjusts cloud configurations.

 Anomaly Detection in Production: Monitors logs and metrics for abnormal


patterns.

 User Feedback Analysis: NLP parses reviews and tickets for feedback loops.

 Self-healing Systems: AI can trigger recovery actions based on diagnostics.

AI closes the DevOps feedback loop, enabling continuous learning and improvement in
production environments.

AI Tools and Platforms for Developers

Numerous tools support AI-first software engineering:

 ML Libraries: TensorFlow, PyTorch, Scikit-learn.

 NLP APIs: OpenAI, Google Cloud NLP, AWS Comprehend.

 AutoML Platforms: Google AutoML, Azure ML, H2O.ai.

 Integrated Development Environments (IDEs): IntelliJ with Copilot plugin, VS


Code with Codex.

Developers must choose tools based on their use cases — from code generation and
modeling to deployment automation.

Ethics, Bias, and Explainability in AI-first Engineering


26

AI-first engineering must be grounded in ethical practices:

 Bias Detection: Use tools to detect training data or model bias.

 Fairness Audits: Ensure AI treats different user groups equitably.

 Explainability Tools: Use SHAP, LIME, or integrated explainers to interpret AI


outputs.

 Governance: Implement model versioning, audit logs, and human-in-the-loop


verification.

Ethical software engineering ensures AI respects privacy, fairness, and accountability.

AI-first Development Lifecycle vs Traditional Lifecycle

Traditional SDLC AI-first SDLC

Manual coding & review AI-assisted coding

Reactive testing Predictive QA

Linear progress Iterative with feedback loops

Performance tuning post-deployment Real-time adaptive performance

AI-first approaches encourage feedback integration, automation, and continuous learning


at every phase of SDLC.

Skills and Mindset for AI-first Software Engineers

To succeed in AI-first environments, engineers must adopt:

 AI Literacy: Understand ML concepts, model training, and limitations.

 Data Fluency: Ability to work with structured/unstructured data.

 Agility: Comfortable with iterative, feedback-driven processes.

 Tool Proficiency: Mastery over AI/ML platforms and APIs.

Soft skills like problem-solving, ethics, and communication remain critical, especially
when collaborating with multidisciplinary teams.
27

Case Studies and Real-world Applications

Case Study 1: AI-enhanced eCommerce Platform

An e-commerce company uses AI for product recommendation, user behavior analysis,


and auto-responses. It reduced cart abandonment by 30%.

Case Study 2: Healthcare Diagnostics App

An app uses AI-first design to assist doctors with preliminary diagnoses from X-ray
images. This reduced diagnostic time by 50%.

Case Study 3: Intelligent Document Processing

Infosys implemented AI-first document handling in financial services, automating


compliance checks using NLP and OCR, boosting speed and accuracy.

These cases show how AI-first engineering delivers measurable business value.

Challenges and Limitations

 Data Dependency: Poor or biased data leads to poor models.

 Model Drift: Performance can degrade over time.

 Explainability: Black-box nature of AI models can reduce trust.

 Skill Gaps: Not all teams are ready for AI-first adoption.

 Ethical Risks: AI can reinforce societal biases if not handled responsibly.

Mitigating these challenges requires governance, monitoring, training, and transparency.

Future Directions and Opportunities

The future of AI-first software engineering includes:

 Multimodal Systems: Integration of text, image, audio, and video in one pipeline.

 AI-native IDEs: Fully intelligent development environments.

 Hyperautomation: Orchestration of end-to-end workflows via AI.

 Federated Learning: Decentralized, privacy-preserving model training.


28

 AI-led Innovation: From smart cities to personalized education platforms.

Engineers who align their careers with AI-first principles will be well-positioned in the
evolving tech landscape.

Conclusion

AI-first software engineering represents a revolution in how we build and evolve digital
systems. Rather than merely applying AI after the fact, the entire development lifecycle is
reimagined with AI at the core — from understanding user needs and generating code to
automating testing, deployment, and refinement.

Infosys Springboard’s approach emphasizes both technical skill and ethical grounding,
preparing learners to engineer software that is intelligent, responsible, and scalable.

By mastering AI-first principles, developers not only accelerate delivery but also unlock
innovation, enhance user experiences, and shape the digital future.
29

5. PROMPT ENGINEERING

Introduction to Prompt Engineering

Prompt engineering is the process of crafting effective inputs (prompts) to elicit accurate,
relevant, and efficient outputs from language models like GPT-3, GPT-4, and other
generative AI systems. As these models grow in capability, the need to control and fine-
tune their behavior through smart prompting becomes critical.

Unlike traditional programming, where behavior is controlled through code, prompt


engineering leverages language and context to guide the model’s output. It plays a central
role in building AI-driven chatbots, content generators, decision support systems, and
educational tools.

Infosys Springboard's course emphasizes that prompt engineering is not just about
formulating questions—it's about designing structured interactions that guide the AI
toward useful and meaningful results.

Evolution of Prompt Engineering in AI

Prompt engineering emerged with the rise of large language models (LLMs). Early
versions, like GPT-2, had limited capabilities and required developers to tweak underlying
code. With the launch of GPT-3 and beyond, LLMs became general-purpose tools—
capable of answering questions, writing stories, solving equations, and more—based on
simple text prompts.

The evolution occurred in phases:

 Pre-LLM Phase: Interaction through APIs and rule-based NLP.

 LLM 1.0 (e.g., GPT-2): Limited understanding; output was often unpredictable.

 LLM 2.0 (GPT-3, GPT-3.5): Massive improvement; prompt engineering


becomes key.

 LLM 3.0 (GPT-4 and beyond): Context length increases, enabling rich dialogues
and task completion.
30

Today, prompt engineering is a skill set on its own—merging linguistics, logic,


programming, and AI awareness.

Understanding Language Models

Large Language Models (LLMs) like those developed by OpenAI, Google, and Meta are
trained on vast amounts of text data to predict and generate human-like responses. These
models learn the probability distribution of sequences of words, enabling them to:

 Answer questions

 Translate languages

 Generate stories and code

 Simulate conversation

LLMs use architectures like the transformer model, which relies on attention mechanisms
to handle context and semantics. Understanding how these models work helps in designing
better prompts. Key aspects include:

 Tokenization: Models process text as tokens (words or subwords).

 Context Window: Limited memory span that affects performance.

 Temperature: Controls randomness in output.

 Top-p and Top-k Sampling: Control output diversity.

Prompt engineering leverages these concepts to craft inputs that work well with how the
model reasons and responds.

Anatomy of a Prompt

A well-structured prompt typically includes:

 Instruction: What you want the model to do (e.g., “Summarize this text…”).

 Input Data: The context or text for processing.

 Output Indicator: Specifies format or style of the output.

 Constraints: Optional rules like length, tone, or format.


31

Example:

“Summarize the following news article in 100 words, using a neutral tone.”

Good prompts are clear, specific, and goal-oriented. Ambiguity leads to unreliable outputs,
while overly detailed prompts may confuse the model.

Types of Prompts

Prompt types vary depending on application:

1. Instructional Prompts
Direct commands: “Translate this to French.”

2. Interrogative Prompts
Questions: “What are the benefits of renewable energy?”

3. Contextual Prompts
Input with examples:
“Example 1: Input – X, Output – Y. Now, Input – Z, Output – ?”

4. Dialogic Prompts
Chat-style or conversational:
User: Tell me a joke. AI: Here’s one...

5. Creative Prompts
For story generation, poetry, or art.

6. Chain-of-thought Prompts
Breaks down reasoning step-by-step:
“Let’s think step by step...”

Understanding and choosing the right prompt type improves interaction success rates.

Prompting Techniques and Strategies

Key techniques include:

 Few-shot prompting: Giving 2–5 examples before the actual task.

 Zero-shot prompting: Task is performed with no examples.


32

 Chain-of-thought: Encouraging step-by-step reasoning.

 Role-based prompting: Assigns a persona to the model.


“Act as a data scientist…”

 Multi-turn prompting: Builds context across multiple prompts.

Effective prompt strategies also include:

 Refinement: Iteratively improving prompt clarity.

 Constraints: Limiting the scope of output.

 Semantic Anchoring: Embedding keywords or phrases to maintain focus.

Mastering these strategies ensures consistent, controllable, and insightful results.

Common Prompting Challenges

Some issues developers face include:

 Ambiguity: Vague prompts lead to vague responses.

 Overgeneralization: Responses may become too broad or obvious.

 Token Limitations: Long prompts exceed model memory.

 Hallucination: Models generating inaccurate or false information.

 Context Loss: In multi-turn dialogues, earlier context may be lost.

Overcoming these requires better structure, clarity, and prompt segmentation.

Role of Context and Examples

Context is crucial in prompt engineering. Including relevant examples (few-shot learning)


enhances understanding. The more aligned the prompt is with the expected output, the
better the performance.

For instance:

Prompt with no context:

“Solve the equation.”


33

Prompt with context:

“Solve the following math problem step-by-step: 3x + 5 = 20.”

Few-shot prompts provide models with training-like data that improves the quality of
output. However, excessive examples may overload the model’s context window.

Evaluating Prompt Performance

Prompt evaluation should be:

 Qualitative: Human judgment on relevance, coherence, creativity.

 Quantitative: Accuracy, completion time, relevance scores.

 A/B Testing: Comparing two prompt variants.

 Automated Metrics: BLEU, ROUGE, and METEOR for NLP tasks.

Infosys Springboard encourages systematic experimentation to optimize prompts for


production use.

Use Cases in Real-World Applications

Prompt engineering drives innovation across sectors:

 Customer Support: AI chatbots that resolve queries with human-like empathy.

 Education: Personalized tutoring and automated grading.

 Marketing: Generating ad copy, slogans, and campaign ideas.

 Healthcare: Assisting with medical documentation and summarization.

 Finance: Automated report writing and data interpretation.

 Software Development: Code completion, bug detection using prompt-based


models like Copilot.

Each use case involves tuning prompts to meet domain-specific needs.

Prompt Engineering Tools and Platforms

Some tools aiding prompt engineering:


34

 OpenAI Playground: Experiment with prompts using GPT-3/4.

 PromptLayer: Logs and tracks prompt performance.

 LangChain: Framework for building AI applications using chains of prompts.

 PromptPerfect: Optimizes and evaluates prompt structures.

 Datasets: Hugging Face provides pre-trained prompts for various tasks.

These platforms assist in rapid prototyping, testing, and refining prompts for deployment.

Ethics and Responsible Prompting

Responsible prompt engineering ensures AI outputs align with ethical standards:

 Bias Control: Prevent prompts that reinforce stereotypes.

 Toxicity Management: Avoid generating harmful or offensive content.

 Privacy Compliance: No prompting for personal or sensitive data.

 Transparency: Clear intention and disclosure in user-facing apps.

Infosys Springboard advocates for AI aligned with societal good, emphasizing responsible
development and usage.

Advanced Techniques: Few-shot and Zero-shot Learning

 Few-shot learning uses prior examples to train on-the-fly. It’s powerful in low-
data environments.
Prompt: “Translate English to French. English: Hello → French: Bonjour...”

 Zero-shot learning asks the model to perform without examples. It requires


precise wording.
Prompt: “Translate ‘Good night’ to French.”

Recent models like GPT-4 perform well with zero-shot prompts, but few-shot remains
essential for nuanced or technical tasks.

The Future of Prompt Engineering

The field is evolving toward:


35

 Multimodal Prompting: Combining text with images, audio, or video.

 Prompt Tuning: Optimizing prompt parameters using automated methods.

 Retrieval-Augmented Generation (RAG): Enhancing prompts with real-time


data.

 Prompt Markets: Online communities sharing reusable prompt templates.

 Prompt-Aware IDEs: Developer tools embedding AI-native workflows.

Prompt engineering is transitioning from manual to semi-automated and soon to fully


optimized pipelines.

Conclusion

Prompt engineering is a critical skill for harnessing the full potential of large language
models. It combines technical understanding with linguistic clarity, logical structure, and
ethical awareness. By crafting precise, goal-oriented prompts, developers can build
intelligent systems that perform tasks with minimal supervision and high reliability.
36

6. OPENAI GENERATIVE PRE-TRAINED TRANSFORMER 3 (GPT-3) FOR


DEVELOPERS
Introduction to GPT-3

GPT-3 (Generative Pre-trained Transformer 3) is a cutting-edge natural language


processing model developed by OpenAI. Known for its ability to understand and generate
human-like text, GPT-3 marks a significant leap in the capabilities of AI-driven systems.
It’s designed to perform tasks such as translation, summarization, question-answering,
and even writing code, all with minimal input from the user.

The Infosys Springboard course introduces GPT-3 from a developer’s perspective,


focusing on its technical foundations, practical use cases, and integration techniques in
real-world applications. It empowers developers with the knowledge to leverage GPT-3
APIs effectively and build intelligent, language-driven systems.

The Evolution of Transformer Models

Transformer models were introduced by Vaswani et al. in 2017, revolutionizing the field
of deep learning. Unlike RNNs or LSTMs, transformers can process input data in parallel
and capture long-range dependencies more efficiently using self-attention mechanisms.

OpenAI’s progression through GPT-1, GPT-2, and GPT-3 has refined these ideas, scaling
model size and capabilities:

 GPT-1: 117M parameters

 GPT-2: 1.5B parameters

 GPT-3: 175B parameters

The leap from GPT-2 to GPT-3 brought unprecedented fluency, coherence, and
generalization power. This evolution has enabled applications previously thought
infeasible using traditional rule-based NLP techniques.

GPT-3 Architecture and Model Details

GPT-3 is a unidirectional transformer model that utilizes 96 attention layers and 96 heads.
It processes language as a sequence of tokens, predicting the next word based on the
37

preceding context. Each layer contributes to refining the prediction through multiple self-
attention computations and feed-forward networks.

The key architectural features include:

 Tokenization: Using Byte Pair Encoding (BPE) to split text into manageable
chunks.

 Positional Encoding: Helps the model retain sequence order.

 Self-Attention Mechanism: Enables contextual understanding by focusing on


relevant parts of the text.

 Layer Normalization and Residuals: Improve convergence and model depth.

Its size and architecture allow GPT-3 to perform multiple tasks in a zero-shot or few-shot
fashion, eliminating the need for extensive fine-tuning.

Key Capabilities and Features

GPT-3 is capable of:

 Text Generation: Creative writing, article summarization, and email drafting.

 Language Translation: From English to various other languages.

 Question Answering: From factual to contextual queries.

 Conversational AI: Engaging human-like responses in chatbot systems.

 Code Generation: Writing Python, JavaScript, SQL, and more.

 Semantic Search: Enhancing search engines using vectorized understanding.

These features make GPT-3 highly versatile for developers looking to embed AI into their
applications without building models from scratch.

Getting Started with GPT-3 as a Developer

To begin using GPT-3, developers need access to the OpenAI API, which provides
endpoints for querying the model. The steps include:

1. Creating an OpenAI Account


38

2. Generating an API Key

3. Accessing the Playground for Experiments

4. Using SDKs or HTTP Requests for Integration

Languages like Python, Node.js, and Java are commonly used. Python's openai package
makes interaction straightforward:

python

import openai

openai.api_key = "your-api-key"

response = openai.Completion.create(

engine="text-davinci-003",

prompt="Explain recursion in simple terms.",

max_tokens=100

Understanding the API: Authentication, Endpoints, and Usage

OpenAI’s API for GPT-3 includes several endpoints, such as:

 /completions: Text generation and prediction

 /edits: Text editing and refinement

 /embeddings: Semantic representation

 /chat/completions: Chat-based interactions (GPT-3.5/GPT-4)

Authentication is handled via a secret API key. Developers must manage rate limits and
token usage wisely to optimize costs.

Key parameters:

 prompt: The input instruction.

 max_tokens: Controls output length.


39

 temperature: Adjusts creativity vs. determinism.

 top_p: Limits sampling to top probability mass.

 stop: Ends the generation on specific tokens.

Proper understanding of these options ensures developers get tailored, useful outputs from
GPT-3.

Prompt Engineering with GPT-3

Prompt engineering is the art of crafting inputs to generate accurate and relevant outputs.
It’s critical to GPT-3's effective use.

Prompt styles include:

 Instructional: “Write a summary of this article.”

 Conversational: “Human: What is AI? AI:”

 Few-shot: Providing examples within the prompt.

 Zero-shot: Providing no examples, only instruction.

Best practices:

 Be specific and clear.

 Provide format expectations.

 Use delimiters for context.

 Experiment with temperature and stop sequences.

Example:

python

prompt = """Translate the following English text to French:

English: Hello, how are you?

French:"""

Code Generation and Completion


40

GPT-3 can generate code from comments or instructions, aiding productivity in IDEs.

Example prompt:

# Python function to calculate factorial

def factorial(n):

GPT-3 will complete:

python

if n == 0:

return 1

else:

return n * factorial(n-1)

It also supports SQL generation, debugging assistance, and code explanation, making it
a valuable tool in software development pipelines.

Building Intelligent Chatbots

GPT-3’s conversational ability enables developers to create realistic chatbots. Using chat
endpoints and conversational formatting (system, user, assistant roles), developers can
build multi-turn dialogue systems.

Example setup:

python

messages = [

{"role": "system", "content": "You are a helpful assistant."},

{"role": "user", "content": "What's the capital of France?"}

The model returns:

{"role": "assistant", "content": "The capital of France is Paris."}


41

Customizing tone, domain knowledge, and context allows for diverse applications in
support, healthcare, education, and HR.

Integrating GPT-3 in Real-Time Applications

Developers can integrate GPT-3 in apps using:

 Webhooks

 Serverless Functions

 REST APIs

Use cases include:

 Real-time query resolution

 In-app content generation

 Dynamic documentation

 Educational feedback systems

Frameworks like Flask, React, and Node.js help bridge GPT-3 with front-end
applications. Careful caching, token control, and latency management are essential for
seamless user experiences.

Use Cases Across Industries

GPT-3 finds applications in various sectors:

 Education: Automated tutors and feedback systems.

 Healthcare: Symptom checkers and EMR summarization.

 Finance: Report generation, financial insights.

 Legal: Document review and drafting.

 E-commerce: Personalized product descriptions.

 Entertainment: Story and script writing.

 Recruitment: Resume screening and job description creation.


42

These use cases demonstrate the model’s adaptability and productivity-enhancing


potential across domains.

Responsible AI Development with GPT-3

Responsible development includes:

 Bias Mitigation: Avoiding prompts that amplify stereotypes.

 Toxicity Filters: Using moderation endpoints to prevent harmful content.

 User Transparency: Informing users of AI involvement.

 Security: Safeguarding API keys and usage logs.

OpenAI enforces use-case policies to avoid misuse in spam, deepfakes, and


misinformation. Developers must implement safety nets and monitor system behavior
constantly.

Limitations and Known Challenges

Despite its power, GPT-3 has limitations:

 Hallucination: Confidently wrong answers.

 Context Window Limit: Can’t remember long histories.

 Lack of Real-Time Knowledge: Trained only on past data.

 Sensitivity to Prompt Changes: Minor tweaks can alter output significantly.

Understanding these drawbacks helps developers mitigate risk and build more robust
systems.

Performance Optimization Techniques

Tips for efficient usage:

 Optimize prompts for brevity and clarity.

 Set appropriate max_tokens limits.

 Reduce redundant API calls through caching.


43

 Use embeddings for contextual memory.

 Preprocess inputs to filter noise.

Monitoring logs and refining prompts continuously leads to faster, more cost-effective
GPT-3 applications.

Future of GPT-3 and GPT-4+

With GPT-4 and beyond, OpenAI introduces improvements in:

 Multimodal Processing: Images + text understanding.

 Larger Context Windows: Improved memory.

 Better Accuracy: Fewer hallucinations.

 Tool Use: Integrating browsing, code execution, etc.

Developers will soon design AI workflows that interact with tools, APIs, and live data
autonomously, setting the stage for AI-native apps.

Conclusion

The Infosys Springboard course “OpenAI GPT-3 for Developers” empowers learners to
harness GPT-3’s capabilities for solving real-world problems. From simple text
generation to complex chatbot design and code automation, GPT-3 provides a versatile
foundation for intelligent applications.

As the field evolves, developers equipped with prompt engineering, API mastery, and
ethical awareness will lead the way in shaping AI-first products and experiences.
44

EXPERIMENT

Problem 1 : Generating SQL commands


Problem 2 : Information extraction
Problem 3 : Recipe Generator
Problem 4 : Image Reshape
Problem 5 : Guided Project
Problem 6 : Capstone Project 1
a. Use GPT-3 model from OpenAI to generate python code for the following
tasks using NLP.
• Exploratory data analysis using pandas.

• Data visualization using matplotlib and seaborn.

b. Generate a Resume from the paragraph about your experience and skillset
using GPT-3 API.
c. Generate set of interview questions in any of the programming language of
your choice.
d. Generate a summary from meeting notes. Consider the following as an
example:
Max: Profits up 50%
Ruby: New servers are online
Kyle: Need more time to fix software
Walker: Happy to help
Parkman: Beta testing almost done

Problem 7 : Capstone Project 2

Problem statement:
Design a CGAN generative model that can generate the few fake images from the
provided data. Use the CIFAR-10 dataset to train a Conditional GAN. Take user input for
class type and generate images of the specified class type using the Conditional GAN.
5/9/25, 2:56 PM Generating SQL commands.ipynb - Colab

import openai
openai.api_key="sk-proj-v4qLcBBF-JUPQoka7unid0Rn_NkHCoCqsKv2hYlq_QVF1smzgiwDQg8Iba3vIJv6qJu3B8mdLCT3BlbkFJJ4oy5W7q82jbnpz_huk7wCdJdgNB8OO
examples= [
("Find unique values of DEPARTMENT from EMPLOYEE table.",
"SELECT DISTINCT DEPARTMENT FROM EMPLOYEE;"),
("Find 3 highest salaries from EMPLOYEE table.",
"SELECT TOP 3 SALARY FROM EMPLOYEE ORDER BY SALARY DESC;")
]
def create_prompt(user_query):
prompt="Convert the following English statements into SQL queries;\n\n"
for example in examples:
prompt +=f"English:{example[0]}\nSQL:{example[1]}\n\n"

prompt += f"English: {user_query}\nSQL:"


return prompt

def generate_sql(english_text):
prompt=create_prompt(english_text)
response = openai.Completion.create(
engine="davinci-002",
prompt=prompt,
temperature=0.5,
max_tokens=100,
stop=["\n"]
)
return response["choices"][0]["text"].strip()

test_query = "List all employees hired after 2020."


generated_query = generate_sql(test_query)

print(f"English query: {test_query}")


print(f"generated SQL Query:{generated_query}")

English query: List all employees hired after 2020.


generated SQL Query:SELECT E1.EMPLOYEE_NUMBER,E1.EMPLOYEE_NAME FROM EMPLOYEE E1 WHERE E1.HIRE_DATE > TO_DATE('2020-01-01','YYYY-MM-D
 

https://colab.research.google.com/drive/1tqqXjfw6xbdJaUEYndRGUSLwkTgmj0hu?authuser=1#printMode=true 1/1
5/9/25, 2:57 PM Information extraction.ipynb - Colab

import openai
openai.api_key="sk-proj-v4qLcBBF-JUPQoka7unid0Rn_NkHCoCqsKv2hYlq_QVF1smzgiwDQg8Iba3vIJv6qJu3B8mdLCT3BlbkFJJ4oy5W7q82jbnpz_huk7wCdJdgNB8OO

def extract_information(passage):
prompt = f"""Extraxt key information from the following passage:\n\n\"\"\"n{passage}\n\"\"\"n|nsummary of key points:\n"""
response = openai.Completion.create(
engine = "davinci-002",
prompt=prompt,
temperature=0.3,
max_tokens=200,
top_p=1.0,
frequency_penalty=0,
presence_penalty=0,
stop=["\"\"\""]
)
return response["choices"][0]["text"].strip()

passage= """Generative Pre-trained Transformer 3 (GPT-3) is an autoregressive language model that uses deep learning to produce human-lik

extracted_info = extract_information(passage)

print("Extracted Informations:\n")
print(extracted_info)

Extracted Informations:

1. GPT-3 is an autoregressive language model that uses deep learning to produce human-like text
2. It is the third-generation language prediction model in the GPT-n series (and the successor to GPT-2)
3. GPT-3's full version has a capacity of 175 billion machine learning parameters
4. GPT-3, which was introduced in May 2020, and was in beta testing as of July 2020
5. GPT-3 is part of a trend in natural language processing (NLP) systems of pre-trained language representations
6. Before the release of GPT-3, the largest language model was Microsoft's Turing NLG, introduced in February 2020, with a capacity
 

https://colab.research.google.com/drive/1NTYviof2Y7PmmBANXC2bYvfgSO0Z0kfe?authuser=1#printMode=true 1/1
5/9/25, 2:57 PM Recipe Generator.ipynb - Colab

import openai
openai.api_key="sk-proj-v4qLcBBF-JUPQoka7unid0Rn_NkHCoCqsKv2hYlq_QVF1smzgiwDQg8Iba3vIJv6qJu3B8mdLCT3BlbkFJJ4oy5W7q82jbnpz_huk7wCdJdgNB8OO

def generate_recipe(dish_name,ingredients):
ingredients_text = "\n".join(ingredients)
prompt = f"""Write a recipe based on these ingredients and instructions:
{dish_name}
ingredients:
{ingredients_text}

step to be followed
"""
response = openai.Completion.create(
engine = "davinci-002",
prompt=prompt,
temperature=0.5,
max_tokens=100,
top_p=1.0,
frequency_penalty=0.5,
presence_penalty=0.5,
)
return response["choices"][0]["text"].strip()

dish_name="Caramel Custard"
ingredients=["Milk","Custard Power","Sugar","Milkmaid"]

recipe = generate_recipe(dish_name,ingredients)

print(f"Recipe for {dish_name}:\n")


print(recipe)

Recipe for Caramel Custard:

1. Take milk in a pan.


2. Add custard powder to it.
3. Mix well and heat the milk on slow flame.
4. When the milk starts boiling, add sugar to it and stir well.
5. Now add milkmaid to it and mix well.
6. Pour this mixture into small bowls or glasses.
7. Put these bowls in a baking dish and pour hot water in it till it comes up

https://colab.research.google.com/drive/1H4KjHbOR_5etB617B5bMzabeVnCYosdG?authuser=1#printMode=true 1/1
5/9/25, 2:58 PM Image Reshape.ipynb - Colab

# example of training a gan on mnist


from numpy import expand_dims
from numpy import zeros
from numpy import ones
from numpy import vstack
from numpy.random import randn
from numpy.random import randint
from keras.datasets import mnist
from matplotlib import pyplot
import matplotlib.pyplot as plt

(train_X, train_y), (test_X, test_y) = mnist.load_data()


print('X_train: ' + str(train_X.shape))

X_train: (60000, 28, 28)

train_X = train_X/255

train_y

array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

from sklearn.naive_bayes import BernoulliNB


clf = BernoulliNB()
model=clf.fit(train_X, train_y)

import numpy as np
prob= (np.exp(model.feature_log_prob_)[0])

prob


array([1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 3.37552743e-04,
3.37552743e-04, 3.37552743e-04, 1.68776371e-04, 3.37552743e-04,
3.37552743e-04, 3.37552743e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 3.37552743e-04,
3.37552743e-04, 1.68776371e-04, 3.37552743e-04, 5.06329114e-04,
3.37552743e-04, 1.68776371e-04, 1.68776371e-04, 3.37552743e-04,
5.06329114e-04, 8.43881857e-04, 5.06329114e-04, 5.06329114e-04,
8.43881857e-04, 6.75105485e-04, 3.37552743e-04, 3.37552743e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 3.37552743e-04,
5.06329114e-04, 5.06329114e-04, 5.06329114e-04, 6.75105485e-04,
1.35021097e-03, 3.03797468e-03, 5.56962025e-03, 9.78902954e-03,
1.60337553e-02, 2.29535865e-02, 2.80168776e-02, 3.07172996e-02,
3.10548523e-02, 2.64978903e-02, 2.22784810e-02, 1.65400844e-02,
1.19831224e-02, 7.42616034e-03, 2.86919831e-03, 1.68776371e-03,
3.37552743e-04, 3.37552743e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 5.06329114e-04,
8.43881857e-04, 1.18143460e-03, 3.37552743e-03, 6.58227848e-03,
1.56962025e-02, 3.35864979e-02, 6.83544304e-02, 1.23544304e-01,
2.02531646e-01, 2.80337553e-01, 3.56624473e-01, 4.08607595e-01,
4.27172996e-01, 4.02194093e-01, 3.40084388e-01, 2.57552743e-01,
1.68438819e-01, 9.75527426e-02, 4.75949367e-02, 1.70464135e-02,
3.20675105e-03, 5.06329114e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 6.75105485e-04,
1.35021097e-03, 3.20675105e-03, 7.93248945e-03, 2.09282700e-02,
4.97890295e-02, 1.02447257e-01, 1.95611814e-01, 3.15949367e-01,
4.51983122e-01, 5.90717300e-01, 6.92658228e-01, 7.61856540e-01,
7.78396624e-01, 7.51392405e-01, 6.71561181e-01, 5.39240506e-01,
3.91729958e-01, 2.47257384e-01, 1.25907173e-01, 4.69198312e-02,
7.76371308e-03, 5.06329114e-04, 1.68776371e-04, 1.68776371e-04,
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 1.68776371e-04,
2.70042194e-03, 7.42616034e-03, 1.68776371e-02, 4.91139241e-02,
1.01772152e-01, 2.01181435e-01, 3.47510549e-01, 5.07510549e-01,
6.56540084e-01, 7.66413502e-01, 8.44894515e-01, 8.90126582e-01,
9.01603376e-01, 8.90801688e-01, 8.45569620e-01, 7.42953586e-01,
5.81097046e-01, 4.05569620e-01, 2.36118143e-01, 1.04978903e-01,

https://colab.research.google.com/drive/1GI5vjJKefJkG4YXPZ1WqvQcNKeegJ3-9?authuser=1#printMode=true 1/3
5/9/25, 2:58 PM Image Reshape.ipynb - Colab
1.90717300e-02, 1.35021097e-03, 5.06329114e-04, 3.37552743e-04, 
1.68776371e-04, 1.68776371e-04, 1.68776371e-04, 3.37552743e-04,
2.70042194e-03, 1.18143460e-02, 3.40928270e-02, 9.13080169e-02,
1.93248945e-01, 3.39915612e-01, 5.11054852e-01, 6.74092827e-01,
7.93417722e-01, 8.56371308e-01, 8.85063291e-01, 8.95527426e-01,
9.00759494e-01, 9.11223629e-01, 9.05485232e-01, 8.53670886e-01,
7.33333333e-01, 5.56793249e-01, 3.60168776e-01, 1.76708861e-01,
4.23628692e-02, 2.19409283e-03, 3.37552743e-04, 3.37552743e-04,
3.37552743e-04, 1.68776371e-04, 5.06329114e-04, 6.75105485e-04,
3 20675105e-03 1 80590717e-02 6 61603376e-02 1 62194093e-01

gen_image = [0 if i < 0.5 else 1 for i in prob]


gen_image =np.array(gen_image)

import matplotlib.pyplot as plt


# set up the canvas for the subplots
plt.figure(figsize=(10,10))
plt.axis('Off')
# image
plt.subplot(1,2,1)
plt.imshow(train_X[1].reshape(28,28),cmap='gray')
plt.title('Actual Image', fontsize=14)
plt.axis('off')
# image
plt.subplot(1,2,2)
plt.imshow(gen_image.reshape(28,28),cmap='gray')
plt.title('Generated Image', fontsize=14)
plt.axis('off')

(-0.5, 27.5, 27.5, -0.5)

https://colab.research.google.com/drive/1GI5vjJKefJkG4YXPZ1WqvQcNKeegJ3-9?authuser=1#printMode=true 2/3
5/9/25, 2:58 PM Image Reshape.ipynb - Colab

https://colab.research.google.com/drive/1GI5vjJKefJkG4YXPZ1WqvQcNKeegJ3-9?authuser=1#printMode=true 3/3
5/9/25, 2:56 PM Guided Project - Viewer Page.ipynb - Colab

import tensorflow as tf
import numpy as np
import math
import matplotlib.pyplot as plt
import os
import glob
import imageio
from IPython.display import Image, display

output_dir = 'output_images'
if not os.path.exists(output_dir):
os.makedirs(output_dir)

# -----------------------------
# Data Loading and Preprocessing
# -----------------------------
(x_train, y_train), (_, _) = tf.keras.datasets.fashion_mnist.load_data()
print("Training data shape:", x_train.shape, y_train.shape)

Training data shape: (60000, 28, 28) (60000,)

# Plot sample images


plt.figure(figsize=(10,3))
for i in range(3):
plt.subplot(1, 3, i+1)
plt.imshow(x_train[i*5], cmap='gray')
plt.title(f"Label: {y_train[i*5]}")
plt.axis('off')
plt.show()

 

#Convert the train dataset into a 3D DATASET of stacked 3D images


image_width = x_train.shape[1];
x_train = np.reshape(x_train, [-1, image_width ,image_width , 1]).astype('float32')

#Check the shape of the data now


x_train.shape

(60000, 28, 28, 1)

# Normalize the images to [-1, 1] [Normalized Value = (value - mean)/mean]


x_train = (x_train - 127.5) / 127.5

#Generator Initializations
noise_length = 100

def create_generator_model(image_size=28, noise_input=100):


input_layer = tf.keras.layers.Input(shape=(noise_input,))
x = tf.keras.layers.Dense(7 * 7 * 256)(input_layer)
x = tf.keras.layers.Reshape((7, 7, 256))(x)

# Upscaling 1: 7x7x256 -> 14x14x128


x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation('leaky_relu')(x)
x = tf.keras.layers.Conv2DTranspose(128, (5,5), strides=2, padding='same')(x)

# Upscaling 2: 14x14x128 -> 28x28x64


x = tf.keras.layers.BatchNormalization()(x)

https://colab.research.google.com/drive/1_kVBs9j-zwHhN-MRscKqfGSoebeX4AQQ?authuser=1#printMode=true 1/6
5/9/25, 2:56 PM Guided Project - Viewer Page.ipynb - Colab
x = tf.keras.layers.Activation('leaky_relu')(x)
x = tf.keras.layers.Conv2DTranspose(64, (5,5), strides=2, padding='same')(x)

# Upscaling 3: 28x28x64 -> 28x28x32 (spatial dims remain same)


x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation('leaky_relu')(x)
x = tf.keras.layers.Conv2DTranspose(32, (5,5), strides=1, padding='same')(x)

# Final layer: 28x28x32 -> 28x28x1 with sigmoid activation


x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation('leaky_relu')(x)
x = tf.keras.layers.Conv2DTranspose(1, (5,5), strides=1, padding='same')(x)
x = tf.keras.layers.Activation('sigmoid')(x)

gen_network = tf.keras.models.Model(input_layer, x, name='gen_network')


return gen_network

def create_discriminator_model(input_shape=[28,28,1]):
disc_input = tf.keras.layers.Input(shape=input_shape)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(disc_input)
x = tf.keras.layers.Conv2D(32, (5,5), strides=2, padding='same')(x)

x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(64, (5,5), strides=2, padding='same')(x)

x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(128, (5,5), strides=2, padding='same')(x)

x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = tf.keras.layers.Conv2D(256, (5,5), strides=1, padding='same')(x)

x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(1, activation='sigmoid')(x)

disc_network = tf.keras.models.Model(disc_input, x, name='disc_network')


return disc_network

generator = create_generator_model()
noise = tf.random.normal([1, 100])
generated_image = generator(noise, training=False)
plt.imshow(generated_image[0, :, :, 0], cmap='gray')

<matplotlib.image.AxesImage at 0x7cfbb8472a50>

#This code will check the functioning of the descriminator against a sample input. As of this point,
#the descriminator is untrained.
discriminator = create_discriminator_model()
decision = discriminator(generated_image)
print (decision)

tf.Tensor([[0.49749836]], shape=(1, 1), dtype=float32)


/usr/local/lib/python3.11/dist-packages/keras/src/layers/activations/leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated.
warnings.warn(
 

https://colab.research.google.com/drive/1_kVBs9j-zwHhN-MRscKqfGSoebeX4AQQ?authuser=1#printMode=true 2/6
5/9/25, 2:56 PM Guided Project - Viewer Page.ipynb - Colab
def build_models():
generator = create_generator_model()
discriminator = create_discriminator_model()

# Freeze discriminator weights for the adversarial model


discriminator.trainable = False
noise_input = tf.keras.layers.Input(shape=(noise_length,))
generated_image = generator(noise_input)
adversarial_output = discriminator(generated_image)
adversarial = tf.keras.models.Model(noise_input, adversarial_output, name='adversarial')

optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)


discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
adversarial.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

return generator, discriminator, adversarial

def plot_images(fake_images, step):


num_images = fake_images.shape[0]
image_size = fake_images.shape[1]
rows = int(math.sqrt(num_images))

plt.figure(figsize=(rows, rows))
for i in range(num_images):
plt.subplot(rows, rows, i + 1)
image = np.reshape(fake_images[i], [image_size, image_size])
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.suptitle(f"Step {step}")

# Save the figure into the output folder


filename = os.path.join(output_dir, f'fashion_mnist_{step}.png')
plt.savefig(filename)
plt.show()
plt.close()

def train_gan(generator, discriminator, adversarial, noise_size=100):


batch_size = 64
train_steps = 500 # Increase this for longer training if desired
image_size = 28

# Load and preprocess Fashion MNIST for the discriminator (normalized to [0,1])
(x_train, _), (_, _) = tf.keras.datasets.fashion_mnist.load_data()
x_train = np.reshape(x_train, [-1, image_size, image_size, 1]).astype('float32')
x_train = x_train / 255.0

# Fixed noise for visualizing progress


test_noise_input = np.random.uniform(-1.0, 1.0, size=[16, noise_size])

for i in range(train_steps):
# Train Discriminator
noise_input = np.random.uniform(-1.0, 1.0, size=[batch_size, noise_size])
fake_images = generator.predict(noise_input)

img_indexes = np.random.randint(0, x_train.shape[0], size=batch_size)


real_images = x_train[img_indexes]

X = np.concatenate((real_images, fake_images))
y_real = np.ones((batch_size, 1))
y_fake = np.zeros((batch_size, 1))
y = np.concatenate((y_real, y_fake))

d_loss, d_acc = discriminator.train_on_batch(X, y)

# Train Generator via the Adversarial Model


noise_input = np.random.uniform(-1.0, 1.0, size=[batch_size, noise_size])
y_gen = np.ones((batch_size, 1)) # Generator tries to fool the discriminator
a_loss, a_acc = adversarial.train_on_batch(noise_input, y_gen)

if i % 100 == 0:
print(f"Step {i}: [D loss: {d_loss:.4f}, acc: {d_acc:.4f}], [A loss: {a_loss:.4f}, acc: {a_acc:.4f}]")

# Save images periodically


if (i + 1) % 100 == 0:
fake_images = generator.predict(test_noise_input)
plot_images(fake_images, i + 1)

# Save the trained generator model


generator.save('fashionmnist_generator_dcgan.h5')

https://colab.research.google.com/drive/1_kVBs9j-zwHhN-MRscKqfGSoebeX4AQQ?authuser=1#printMode=true 3/6
5/9/25, 2:56 PM Guided Project - Viewer Page.ipynb - Colab
def plot_images(fake_images, step):

plt.figure(figsize=(2.5,2.5))
num_images = fake_images.shape[0]

image_size = fake_images.shape[1]
rows = int(math.sqrt(fake_images.shape[0]))

for i in range(num_images):
plt.subplot(rows, rows, i + 1)
image = np.reshape(fake_images[i], [image_size, image_size])
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.show()

G, D, A = build_models()

G.summary()

Model: "gen_network"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ input_layer_2 (InputLayer) │ (None, 100) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_2 (Dense) │ (None, 12544) │ 1,266,944 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ reshape_1 (Reshape) │ (None, 7, 7, 256) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_4 │ (None, 7, 7, 256) │ 1,024 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_5 (Activation) │ (None, 7, 7, 256) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_transpose_4 (Conv2DTranspose) │ (None, 14, 14, 128) │ 819,328 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_5 │ (None, 14, 14, 128) │ 512 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_6 (Activation) │ (None, 14, 14, 128) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_transpose_5 (Conv2DTranspose) │ (None, 28, 28, 64) │ 204,864 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_6 │ (None, 28, 28, 64) │ 256 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_7 (Activation) │ (None, 28, 28, 64) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_transpose_6 (Conv2DTranspose) │ (None, 28, 28, 32) │ 51,232 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_7 │ (None, 28, 28, 32) │ 128 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_8 (Activation) │ (None, 28, 28, 32) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_transpose_7 (Conv2DTranspose) │ (None, 28, 28, 1) │ 801 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_9 (Activation) │ (None, 28, 28, 1) │ 0 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 2,345,089 (8.95 MB)
Trainable params: 2,344,129 (8.94 MB)
Non-trainable params: 960 (3.75 KB)
D.summary()
 

https://colab.research.google.com/drive/1_kVBs9j-zwHhN-MRscKqfGSoebeX4AQQ?authuser=1#printMode=true 4/6
5/9/25, 2:56 PM Guided Project - Viewer Page.ipynb - Colab

Model: "disc_network"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ input_layer_3 (InputLayer) │ (None, 28, 28, 1) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ leaky_re_lu_4 (LeakyReLU) │ (None, 28, 28, 1) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_4 (Conv2D) │ (None, 14, 14, 32) │ 832 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ leaky_re_lu_5 (LeakyReLU) │ (None, 14, 14, 32) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_5 (Conv2D) │ (None, 7, 7, 64) │ 51,264 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ leaky_re_lu_6 (LeakyReLU) │ (None, 7, 7, 64) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_6 (Conv2D) │ (None, 4, 4, 128) │ 204,928 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ leaky_re_lu_7 (LeakyReLU) │ (None, 4, 4, 128) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_7 (Conv2D) │ (None, 4, 4, 256) │ 819,456 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ flatten_1 (Flatten) │ (None, 4096) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_3 (Dense) │ (None, 1) │ 4,097 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 1,080,577 (4.12 MB)
Trainable params: 0 (0.00 B)
Non-trainable params: 1,080,577 (4.12 MB)
 
A.summary()

Model: "adversarial"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ input_layer_4 (InputLayer) │ (None, 100) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ gen_network (Functional) │ (None, 28, 28, 1) │ 2,345,089 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ disc_network (Functional) │ (None, 1) │ 1,080,577 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 3,425,666 (13.07 MB)
Trainable params: 2,344,129 (8.94 MB)
Non-trainable params: 1,081,537 (4.13 MB)
 

train_gan(G, D, A)

https://colab.research.google.com/drive/1_kVBs9j-zwHhN-MRscKqfGSoebeX4AQQ?authuser=1#printMode=true 5/6
5/9/25, 2:56 PM Guided Project - Viewer Page.ipynb - Colab

2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 224ms/step


/usr/local/lib/python3.11/dist-packages/keras/src/backend/tensorflow/trainer.py:82: UserWarning: The model does not have any trainab
warnings.warn("The model does not have any trainable weights.")
Step 0: [D loss: 0.6921, acc: 0.5156], [A loss: 0.7091, acc: 0.0000]
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 214ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 210ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 531ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 240ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 250ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 371ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 339ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
import glob
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 211ms/step
import imageio
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 326ms/step
from IPython.display import Image,
2/2 ━━━━━━━━━━━━━━━━━━━━ display
1s 267ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━
anim_file = 'dcfashiongan.gif' 0s 220ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━
filenames 0s 215ms/step
= sorted(glob.glob(os.path.join(output_dir, 'fashion_mnist_*.png')))
2/2 ━━━━━━━━━━━━━━━━━━━━
if len(filenames) == 0: 1s 255ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━
print("No image files were 1s 358ms/step
found. Please check that the training loop saved images "
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 216ms/step
"to the correct directory and that training ran for enough steps.")
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
else:
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
with
2/2 imageio.get_writer(anim_file, mode='I', duration=0.5) as writer:
━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
2/2for filename in filenames:
━━━━━━━━━━━━━━━━━━━━ 0s 222ms/step
image = imageio.imread(filename)
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 344ms/step
writer.append_data(image)
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 219ms/step
# 2/2
Display the animated GIF 0s
━━━━━━━━━━━━━━━━━━━━ inline in Colab
210ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 221ms/step
display(Image(filename=anim_file))
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 222ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 256ms/step
<ipython-input-22-fb538a7d5f9c>:13: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that o
2/2 ━━━━━━━━━━━━━━━━━━━━
image 1s 297ms/step
= imageio.imread(filename)
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 210ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 212ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 213ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 344ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 214ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 216ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 214ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 233ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 223ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 338ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 230ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 213ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 248ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 1s 335ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 213ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 215ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 219ms/step
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 219ms/step

https://colab.research.google.com/drive/1_kVBs9j-zwHhN-MRscKqfGSoebeX4AQQ?authuser=1#printMode=true 6/6
5/9/25, 2:55 PM Capstone Project 1.ipynb - Colab

pip install openai==0.28

Collecting openai==0.28
Downloading openai-0.28.0-py3-none-any.whl.metadata (13 kB)
Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.11/dist-packages (from openai==0.28) (2.32.3)
Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from openai==0.28) (4.67.1)
Requirement already satisfied: aiohttp in /usr/local/lib/python3.11/dist-packages (from openai==0.28) (3.11.12)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests>=2.20->openai==0.2
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests>=2.20->openai==0.28) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests>=2.20->openai==0.28) (2
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests>=2.20->openai==0.28) (20
Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (2.4
Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (1.3.2)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (25.1.0)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (1.5.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (6.1.0)
Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (0.2.1)
Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->openai==0.28) (1.18.3)
Downloading openai-0.28.0-py3-none-any.whl (76 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.5/76.5 kB 5.8 MB/s eta 0:00:00
Installing collected packages: openai
Attempting uninstall: openai
Found existing installation: openai 1.61.1
Uninstalling openai-1.61.1:
Successfully uninstalled openai-1.61.1
Successfully installed openai-0.28.0
 

import openai
openai.api_key = "sk-proj-v4qLcBBF-JUPQoka7unid0Rn_NkHCoCqsKv2hYlq_QVF1smzgiwDQg8Iba3vIJv6qJu3B8mdLCT3BlbkFJJ4oy5W7q82jbnpz_huk7wCdJdgNB8

prompt_text="""
Generate Python code for the following tasks using Pandas,Matplotlib and seaborn:

1. Perform exploratory data analysis(EDA) on a dataset


using Pandas.Include basic satistical summaries, missing value analysis and data types.

2. Visualize the dataset using Matplotlib and seaborn.


Include histogram,barplots, boxplots and correlation heatmaps.
"""

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt_text}
],
temperature=0.7,
max_tokens=500,
top_p=1.0,
frequency_penalty=0,
presence_penalty=0,
)
print(response['choices'][0]["message"]['content'])

Sure! Here's an example Python code to perform exploratory data analysis (EDA) and visualize a dataset using Pandas, Matplotlib, and

```python
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Load the dataset


data = pd.read_csv('your_dataset.csv')

# Display basic statistical summaries


print("Basic Statistical Summary:")
print(data.describe())

# Missing value analysis


print("\nMissing Values Analysis:")
print(data.isnull().sum())

# Data types analysis


print("\nData Types:")
print(data.dtypes)

# Visualizing the dataset


plt.figure(figsize=(15, 10))

# Histogram
plt.subplot(2, 2, 1)
data.hist()
plt.title('Histogram')

https://colab.research.google.com/drive/1oiZ88ewu4oY3muX8-9WQI262phzO-8XD?authuser=1 1/4
5/9/25, 2:55 PM Capstone Project 1.ipynb - Colab

# Barplot
plt.subplot(2, 2, 2)
sns.countplot(data['column_name'])
plt.title('Barplot')

# Boxplot
plt.subplot(2, 2, 3)
sns.boxplot(data=data)
plt.title('Boxplot')

# Correlation Heatmap
plt.subplot(2, 2, 4)
corr = data.corr()
sns.heatmap(corr, annot=True)
plt.title('Correlation Heatmap')

plt.tight_layout()
plt.show()
```

In the code above:


- Replace 'your_dataset.csv' with the path to your dataset file.
- Replace 'column_name' with the column name you want to create a countplot for.
- Make sure to install the required libraries using `pip install pandas matplotlib seaborn`.

This code will help you perform EDA and visualize the dataset using Pandas, Matplotlib, and Seaborn in Python.

keyboard_arrow_down Resume Generation


def generate_resume(experience_paragraph):
prompt=f"""
Convert the following experience and skillset paragraph into a structured
professional resume:
{experience_paragraph}
The resume should be formatted with the following sections:
-Name
-Contact Information (Placeholder)
-Summary
-Skills
-EExperience
-Education
-Certification(If applicable)
-Projects (If applicable)
-References

Ensure the resume is professional, concise and formatted correctly.


"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
 
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=0.7,
top_p=1.0,
frequency_penalty=0,
presence_penalty=0,
)
return response['choices'][0]["message"]['content']

experience_paragraph="""
I am Ravikumar and I have accumulated five years of experience at Steel Strips Wheels Limited, where I contributed to manufacturing and

resume_text=generate_resume(experience_paragraph)
print(resume_text)

**Resume**

**Name:** Ravikumar

**Contact Information:** [Placeholder]

**Summary:**
Dedicated professional with five years of experience at Steel Strips Wheels Limited in manufacturing and quality control. Skilled in

**Skills:**
- Production Line Management
- Operations Optimization
- Quality Control
- Technical Proficiency
- Problem-Solving

https://colab.research.google.com/drive/1oiZ88ewu4oY3muX8-9WQI262phzO-8XD?authuser=1 2/4
5/9/25, 2:55 PM Capstone Project 1.ipynb - Colab
- Automotive Industry Knowledge

**Experience:**
Steel Strips Wheels Limited
- Contributed to manufacturing and quality control processes
- Oversaw production lines, optimized operations, and ensured product quality
- Developed technical skills and problem-solving abilities
- Enhanced understanding of the automotive industry

**Education:**
[Your Education Background]

**Certifications:**
[If Applicable]

**Projects:**
[If Applicable]

**References:**
Available upon request.

keyboard_arrow_down INTERVIEW QUESTION GENERATOR


def generate_interview_questions(programming_language, num_questions = 10):
prompt=f"""
Generate {num_questions} technical interview questions for the programming
language:
{programming_language}.
Include question on fundamental concepts, best particles, advance topics and problem solving."""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
 

{"role": "system", "content": "You are an AI that generates programming interview questions."},
{"role": "user", "content": prompt}],
temperature=0.7,
)
return response['choices'][0]["message"]['content']

programming_language="Javascript"
interview_questions=generate_interview_questions(programming_language)
print(interview_questions)

1. Explain the difference between 'undefined' and 'null' in JavaScript. Provide examples where each would be used.

2. What are closures in JavaScript? How are they useful and can you provide an example of how to create a closure?

3. Describe the concept of prototypal inheritance in JavaScript. How does it differ from classical inheritance in other programming

4. How does JavaScript handle asynchronous programming? Explain the event loop and how it facilitates non-blocking operations.

5. What are the differences between 'var', 'let', and 'const' for variable declaration in JavaScript? When would you use each one?

6. How can you optimize the performance of a JavaScript application? Discuss best practices for improving speed and efficiency.

7. What is the role of promises and async/await in modern JavaScript development? How do they help manage asynchronous code executio

8. Discuss the concept of hoisting in JavaScript. How does it impact variable and function declarations in the code?

9. What are some common design patterns used in JavaScript? Provide examples of when and how these patterns can be applied in real-w

10. Write a function in JavaScript that takes an array of numbers and returns the sum of all the even numbers in the array. Optimize
 

keyboard_arrow_down MEETING SUMMARY GENERATION


def summarize_meetingnotes(notes):
prompt=f"""
Summarize the following meeting notes into a concise, structured summary:
{notes}
Ensure the summary is professional and highlights key points effectively.
"""

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an AI generates professional meeting summaries."},
{"role": "user", "content": prompt}],
temperature=0.5,

https://colab.research.google.com/drive/1oiZ88ewu4oY3muX8-9WQI262phzO-8XD?authuser=1 3/4
5/9/25, 2:55 PM Capstone Project 1.ipynb - Colab
)
return response['choices'][0]["message"]['content']

notes="""
Ruby: New servers are online

Kyle: Need more time to fix software

Walker: Happy to help

Parkman: beta testing alomost done


"""

meeting_summary=summarize_meetingnotes(notes)
print(meeting_summary)

During the meeting, Ruby reported that new servers are now online. Kyle requested more time to fix software issues. Walker offered a
 

https://colab.research.google.com/drive/1oiZ88ewu4oY3muX8-9WQI262phzO-8XD?authuser=1 4/4
5/9/25, 2:54 PM Capstone project - fake image 2.ipynb - Colab

import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt

if tf.config.list_physical_devices('GPU'):
print("GPU is available. Running on GPU.")
else:
print("GPU is not available. Running on CPU.")

GPU is available. Running on GPU.

(x_train, y_train), (_, _) = tf.keras.datasets.cifar10.load_data()


x_train = x_train.astype('float32')
x_train = (x_train - 127.5) / 127.5 # Normalize to [-1, 1]
y_train = y_train.astype('int32')

BUFFER_SIZE = x_train.shape[0]
BATCH_SIZE = 32
LATENT_DIM = 100 # Dimension of the noise vector
NUM_CLASSES = 10
IMG_SHAPE = (32, 32, 3)

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz


170498071/170498071 ━━━━━━━━━━━━━━━━━━━━ 14s 0us/step

train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

def build_generator():
noise_input = layers.Input(shape=(LATENT_DIM,))
label_input = layers.Input(shape=(1,), dtype='int32')

# Embed the label into a vector of size equal to the noise dimension
label_embedding = layers.Embedding(NUM_CLASSES, LATENT_DIM)(label_input)
label_embedding = layers.Flatten()(label_embedding)

# Instead of concatenation, we perform element-wise multiplication for conditioning.


conditioned_input = layers.multiply([noise_input, label_embedding])

# Project to a low-resolution feature map


x = layers.Dense(8 * 8 * 128, use_bias=False)(conditioned_input)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Reshape((8, 8, 128))(x)

# Upsampling: 8x8 -> 16x16


x = layers.Conv2DTranspose(128, kernel_size=4, strides=2, padding='same', use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

# Upsampling: 16x16 -> 32x32


x = layers.Conv2DTranspose(64, kernel_size=4, strides=2, padding='same', use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)

# Final layer: produce a 32x32 RGB image


output_img = layers.Conv2D(3, kernel_size=3, activation='tanh', padding='same')(x)

return tf.keras.models.Model([noise_input, label_input], output_img, name="Generator")

# -------------------------------
# 3. Build the Simplified Discriminator
# -------------------------------
def build_discriminator():
image_input = layers.Input(shape=IMG_SHAPE)
label_input = layers.Input(shape=(1,), dtype='int32')

# Embed the label and reshape to the image dimensions


label_embedding = layers.Embedding(NUM_CLASSES, np.prod(IMG_SHAPE))(label_input)
label_embedding = layers.Flatten()(label_embedding)
label_embedding = layers.Reshape(IMG_SHAPE)(label_embedding)

# Concatenate the image and label embedding along the channels


merged_input = layers.Concatenate(axis=-1)([image_input, label_embedding])

x = layers.Conv2D(64, kernel_size=3, strides=2, padding='same')(merged_input)


x = layers.LeakyReLU()(x)
x = layers.Dropout(0.3)(x)

https://colab.research.google.com/drive/1N3Z3VgTPEnr3IQ0N6Wnj9AvNiZykAukS?authuser=1#printMode=true 1/4
5/9/25, 2:54 PM Capstone project - fake image 2.ipynb - Colab

x = layers.Conv2D(128, kernel_size=3, strides=2, padding='same')(x)


x = layers.LeakyReLU()(x)
x = layers.Dropout(0.3)(x)

x = layers.Flatten()(x)
# Output logits (no activation) for use with from_logits=True in loss
output = layers.Dense(1)(x)

return tf.keras.models.Model([image_input, label_input], output, name="Discriminator")

# -------------------------------
# Instantiate Models on GPU
# -------------------------------
with tf.device('/GPU:0'):
generator = build_generator()
discriminator = build_discriminator()

# -------------------------------
# 4. Define Losses and Optimizers
# -------------------------------
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def discriminator_loss(real_output, fake_output):


real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
return real_loss + fake_loss

def generator_loss(fake_output):
return cross_entropy(tf.ones_like(fake_output), fake_output)

generator_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)


discriminator_optimizer = tf.keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.5)

# -------------------------------
# 5. Training Step and Loop on GPU
# -------------------------------
@tf.function
def train_step(images, labels):
noise = tf.random.normal([BATCH_SIZE, LATENT_DIM])
random_labels = tf.random.uniform([BATCH_SIZE, 1], minval=0, maxval=NUM_CLASSES, dtype=tf.int32)

with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:


generated_images = generator([noise, random_labels], training=True)
real_output = discriminator([images, labels], training=True)
fake_output = discriminator([generated_images, random_labels], training=True)

g_loss = generator_loss(fake_output)
d_loss = discriminator_loss(real_output, fake_output)

gradients_of_generator = gen_tape.gradient(g_loss, generator.trainable_variables)


gradients_of_discriminator = disc_tape.gradient(d_loss, discriminator.trainable_variables)

generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))

return g_loss, d_loss

def train(dataset, epochs):


for epoch in range(epochs):
print(f"Epoch {epoch+1}/{epochs}")
for image_batch, label_batch in dataset:
g_loss, d_loss = train_step(image_batch, label_batch)
print(f"Generator Loss: {g_loss.numpy():.4f}, Discriminator Loss: {d_loss.numpy():.4f}")

EPOCHS = 250
with tf.device('/GPU:0'):
train(train_dataset, EPOCHS)


Epoch 1/250
Generator Loss: 0.6839, Discriminator Loss: 1.3814
Epoch 2/250
Generator Loss: 0.6770, Discriminator Loss: 1.4080
Epoch 3/250
Generator Loss: 0.7029, Discriminator Loss: 1.4113
Epoch 4/250
Generator Loss: 0.6723, Discriminator Loss: 1.4256
Epoch 5/250
Generator Loss: 0.6918, Discriminator Loss: 1.4012
Epoch 6/250
Generator Loss: 0.6755, Discriminator Loss: 1.3699

https://colab.research.google.com/drive/1N3Z3VgTPEnr3IQ0N6Wnj9AvNiZykAukS?authuser=1#printMode=true 2/4
5/9/25, 2:54 PM Capstone project - fake image 2.ipynb - Colab
Epoch 7/250 
Generator Loss: 0.7395, Discriminator Loss: 1.4185
Epoch 8/250
Generator Loss: 0.7247, Discriminator Loss: 1.4029
Epoch 9/250
Generator Loss: 0.7553, Discriminator Loss: 1.3193
Epoch 10/250
Generator Loss: 0.6400, Discriminator Loss: 1.5028
Epoch 11/250
Generator Loss: 0.6821, Discriminator Loss: 1.3818
Epoch 12/250
Generator Loss: 0.6937, Discriminator Loss: 1.3994
Epoch 13/250
Generator Loss: 0.6841, Discriminator Loss: 1.4496
Epoch 14/250
Generator Loss: 0.7226, Discriminator Loss: 1.4325
Epoch 15/250
Generator Loss: 0.7455, Discriminator Loss: 1.3671
Epoch 16/250
Generator Loss: 0.6962, Discriminator Loss: 1.3433
Epoch 17/250
Generator Loss: 0.7197, Discriminator Loss: 1.3869
Epoch 18/250
Generator Loss: 0.7162, Discriminator Loss: 1.4074
Epoch 19/250
Generator Loss: 0.7342, Discriminator Loss: 1.3944
Epoch 20/250
Generator Loss: 0.7146, Discriminator Loss: 1.4116
Epoch 21/250
Generator Loss: 0.7800, Discriminator Loss: 1.3810
Epoch 22/250
Generator Loss: 0.6912, Discriminator Loss: 1.4149
Epoch 23/250
Generator Loss: 0.7153, Discriminator Loss: 1.3769
Epoch 24/250
Generator Loss: 0.7144, Discriminator Loss: 1.3733
Epoch 25/250
Generator Loss: 0.6757, Discriminator Loss: 1.4576
Epoch 26/250
Generator Loss: 0.7196, Discriminator Loss: 1.3844
Epoch 27/250
Generator Loss: 0.6581, Discriminator Loss: 1.4439
Epoch 28/250
Generator Loss: 0.7165, Discriminator Loss: 1.3002
Epoch 29/250
Generator Loss: 0.6969, Discriminator Loss: 1.4068

# -------------------------------
# 6. Generate Images by Class
# -------------------------------
# CIFAR-10 class names
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']

def generate_images_by_class(class_name, num_images=5):


# Validate the class name
if class_name.lower() not in class_names:

print("Invalid class name. Valid options are:", class_names)
return
label_index = class_names.index(class_name.lower())

# Sample random noise and set the label for all generated images
noise = tf.random.normal([num_images, LATENT_DIM])
labels = tf.constant([[label_index]] * num_images, dtype=tf.int32)

# Generate images (set training=False)


generated_images = generator([noise, labels], training=False)

# Rescale images from [-1, 1] to [0, 1] for visualization


generated_images = (generated_images + 1) / 2.0

# Plot generated images


plt.figure(figsize=(num_images * 2, 2))
for i in range(num_images):
plt.subplot(1, num_images, i + 1)
plt.imshow(generated_images[i])
plt.title(class_name)
plt.axis('off')
plt.show()

https://colab.research.google.com/drive/1N3Z3VgTPEnr3IQ0N6Wnj9AvNiZykAukS?authuser=1#printMode=true 3/4
5/9/25, 2:54 PM Capstone project - fake image 2.ipynb - Colab
# -------------------------------
# 7. User Input and Image Generation
# -------------------------------
# Prompt the user for a class type and generate images from that class
user_class = input("Enter the class type to generate images (e.g., airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck)
generate_images_by_class(user_class)

Enter the class type to generate images (e.g., airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck): bird

# -------------------------------
# 7. User Input and Image Generation
# -------------------------------
# Prompt the user for a class type and generate images from that class
user_class = input("Enter the class type to generate images (e.g., airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck)
generate_images_by_class(user_class)

Enter the class type to generate images (e.g., airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck): dog

https://colab.research.google.com/drive/1N3Z3VgTPEnr3IQ0N6Wnj9AvNiZykAukS?authuser=1#printMode=true 4/4

You might also like