Discover how machines learn to both recognize and create, just like humans!
Generative vs discriminative models in Prompt Engineering / GenAI - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to sort thousands of photos into categories by hand, like "cats" or "dogs," and also create new photos that look like real animals. Doing this manually means looking at every photo carefully and trying to imagine new ones yourself.
Sorting photos by hand is slow and tiring. Trying to create new realistic photos manually is almost impossible and full of mistakes. You can't easily guess what new photos should look like or quickly decide which category a photo belongs to.
Generative and discriminative models help computers learn from examples to do these tasks automatically. Discriminative models learn to tell categories apart, like sorting photos quickly. Generative models learn how to create new examples that look real, like making new photos of animals.
if photo_has_cat(photo): label = 'cat' else: label = 'dog'
label = discriminative_model.predict(photo)
new_photo = generative_model.generate('cat')These models let machines understand and create data, making tasks like image recognition and content creation fast and accurate.
Apps that recognize your face to unlock your phone use discriminative models, while apps that create new art or deepfake videos use generative models.
Discriminative models focus on telling things apart.
Generative models focus on creating new, realistic data.
Together, they make powerful tools for understanding and generating information.
Practice
Solution
Step 1: Understand generative model purpose
Generative models learn the underlying data distribution to generate new data points similar to the training data.Step 2: Compare with discriminative models
Discriminative models focus on learning the decision boundary between classes, not on generating data.Final Answer:
It learns how data is generated and can create new examples. -> Option CQuick Check:
Generative = create data [OK]
- Confusing generative with discriminative models
- Thinking generative models only classify
- Assuming generative models ignore data distribution
Solution
Step 1: Define discriminative model behavior
Discriminative models learn the conditional probability P(output|input), focusing on predicting labels from data.Step 2: Contrast with generative models
Generative models model the joint probability P(input, output) to generate data, which is not the case here.Final Answer:
It models the conditional probability of outputs given inputs. -> Option AQuick Check:
Discriminative = P(output|input) [OK]
- Mixing joint and conditional probabilities
- Thinking discriminative models generate data
- Confusing labels with data points
from sklearn.naive_bayes import GaussianNB from sklearn.linear_model import LogisticRegression X_train = [[1, 2], [2, 3], [3, 4], [4, 5]] y_train = [0, 0, 1, 1] model = GaussianNB() model.fit(X_train, y_train) predictions = model.predict([[2, 3]]) print(predictions)
What will be the output of this code?
Solution
Step 1: Identify model type and training data
GaussianNB is a generative model that learns class distributions. Training data has two classes: 0 and 1.Step 2: Predict class for input [2, 3]
Input [2, 3] is closer to training points labeled 0 ([1,2],[2,3]) than to those labeled 1, so prediction is class 0.Final Answer:
[0] -> Option BQuick Check:
GaussianNB predicts class 0 for [2,3] [OK]
- Assuming LogisticRegression is used instead
- Expecting multiple classes in output
- Thinking prediction causes error
from sklearn.linear_model import LogisticRegression X_train = [[1, 2], [2, 3], [3, 4]] y_train = [0, 1] model = LogisticRegression() model.fit(X_train, y_train)
What is the error and how to fix it?
Solution
Step 1: Check training data shapes
X_train has 3 samples, but y_train has only 2 labels, causing mismatch error.Step 2: Fix label length
To fix, ensure y_train has 3 labels matching X_train samples, e.g., y_train = [0, 1, 0].Final Answer:
Mismatch in number of samples and labels; fix by matching lengths. -> Option AQuick Check:
Samples and labels count must match [OK]
- Ignoring label count mismatch
- Assuming LogisticRegression needs label conversion
- Thinking data type causes error
Solution
Step 1: Identify tasks and suitable models
Classification is best done by discriminative models that separate classes well. Image generation requires generative models that learn data distribution.Step 2: Combine models for both tasks
Use a discriminative model for classifying cats vs dogs, and a generative model like GAN to create new cat images.Final Answer:
Use a discriminative model for classification and a generative model for image creation. -> Option DQuick Check:
Classification + generation = discriminative + generative [OK]
- Using one model type for both tasks
- Confusing clustering with generation
- Ignoring model strengths for each task
