Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

What Generative AI actually is in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - What Generative AI actually is
Problem:You want to understand how a generative AI model creates new content like text or images based on examples it learned from.
Current Metrics:N/A - This is a conceptual understanding experiment without numeric metrics.
Issue:Many beginners confuse generative AI with simple copying or rule-based systems. They don't see how the model learns patterns and creates new, original outputs.
Your Task
Build a simple generative AI model that learns from a small text dataset and generates new sentences. Understand how the model creates new content rather than copying.
Use a simple character-level RNN model in Python with TensorFlow or PyTorch.
Train on a small sample text (like a few sentences).
Generate new text after training.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import tensorflow as tf
import numpy as np

# Sample text data
text = "hello world. this is generative ai."

# Create character vocabulary
vocab = sorted(set(text))
char2idx = {u:i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)

# Convert text to integer indices
text_as_int = np.array([char2idx[c] for c in text])

# Create input-target sequences
seq_length = 10
examples_per_epoch = len(text)//seq_length

char_dataset = tf.data.Dataset.from_tensor_slices(text_as_int)
sequences = char_dataset.batch(seq_length+1, drop_remainder=True)

# Function to split input and target

def split_input_target(chunk):
    input_text = chunk[:-1]
    target_text = chunk[1:]
    return input_text, target_text

dataset = sequences.map(split_input_target)

# Batch size
BATCH_SIZE = 2
BUFFER_SIZE = 100

dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)

# Build the model
vocab_size = len(vocab)
embedding_dim = 8
rnn_units = 64

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, batch_input_shape=[BATCH_SIZE, None]),
    tf.keras.layers.SimpleRNN(rnn_units, return_sequences=True, stateful=True, recurrent_initializer='glorot_uniform'),
    tf.keras.layers.Dense(vocab_size)
])

# Loss function
loss = tf.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam', loss=loss)

# Train the model
EPOCHS = 20
model.fit(dataset, epochs=EPOCHS)

# Text generation function
def generate_text(model, start_string, num_chars=50):
    input_eval = [char2idx[s] for s in start_string]
    input_eval = tf.expand_dims(input_eval, 0)

    text_generated = []

    model.reset_states()
    for _ in range(num_chars):
        predictions = model(input_eval)
        predictions = tf.squeeze(predictions, 0)

        predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()

        input_eval = tf.expand_dims([predicted_id], 0)

        text_generated.append(idx2char[predicted_id])

    return start_string + ''.join(text_generated)

# Generate new text
print(generate_text(model, start_string="hello "))
Built a simple character-level RNN model to learn patterns in text.
Used a small dataset and limited epochs to avoid memorization.
Implemented text generation by predicting one character at a time.
Results Interpretation

Before: No model, no generated text.

After: Model generates new text sequences that resemble the style and characters of the training text but are not exact copies.

Generative AI learns patterns from data and creates new, original content by predicting what comes next, not by copying. This experiment shows how a simple model can generate text character by character.
Bonus Experiment
Try training the model on a larger text dataset and increase the number of epochs. Observe how the generated text improves or starts to copy exact phrases.
💡 Hint
Watch for overfitting: if the model memorizes, generated text will repeat training sentences exactly. Use dropout or reduce epochs to keep creativity.

Practice

(1/5)
1. What is the main purpose of Generative AI?
easy
A. To store large amounts of data efficiently
B. To delete irrelevant information from datasets
C. To only classify existing data into categories
D. To create new content by learning from examples

Solution

  1. Step 1: Understand the role of Generative AI

    Generative AI learns patterns from data and creates new content based on those patterns.
  2. Step 2: Compare options with the definition

    Only To create new content by learning from examples describes creating new content by learning from examples, which matches the main purpose.
  3. Final Answer:

    To create new content by learning from examples -> Option D
  4. Quick Check:

    Generative AI = create new content [OK]
Hint: Generative AI makes new stuff from learned data [OK]
Common Mistakes:
  • Confusing Generative AI with data storage
  • Thinking it only classifies data
  • Believing it deletes data
2. Which of the following is the correct way to describe Generative AI in simple code terms?
easy
A. Train a model, then generate new outputs
B. Only collect data without processing
C. Manually write all new content
D. Delete old models before training

Solution

  1. Step 1: Identify the typical workflow of Generative AI

    Generative AI involves training a model on data and then using it to create new outputs.
  2. Step 2: Match options to this workflow

    Train a model, then generate new outputs correctly states this process, while others describe unrelated or incorrect actions.
  3. Final Answer:

    Train a model, then generate new outputs -> Option A
  4. Quick Check:

    Train then generate = correct process [OK]
Hint: Generative AI = train model + create new data [OK]
Common Mistakes:
  • Thinking Generative AI only collects data
  • Assuming manual content creation is AI
  • Confusing training with deleting models
3. Consider this Python-like pseudocode for a simple Generative AI process:
model = train(data)
new_content = model.generate()

What will new_content most likely contain?
medium
A. A new example similar to the training data
B. The original training data unchanged
C. An error message because generate() is undefined
D. An empty output with no content

Solution

  1. Step 1: Understand the code steps

    The code trains a model on data, then calls generate() to create new content.
  2. Step 2: Predict the output of generate()

    Generate() produces new content similar to what the model learned, not the original data or errors.
  3. Final Answer:

    A new example similar to the training data -> Option A
  4. Quick Check:

    generate() = new similar content [OK]
Hint: generate() creates new data like training examples [OK]
Common Mistakes:
  • Thinking generate() returns original data
  • Assuming generate() causes an error
  • Expecting empty output
4. The following code is intended to train a Generative AI model and generate new content:
model = train(data)
new_content = model.generate(data)

What is the likely problem here?
medium
A. model should be a list, not a model object
B. train() should not take data as input
C. generate() should not take data as input after training
D. new_content should be assigned before training

Solution

  1. Step 1: Review typical usage of generate()

    After training, generate() usually creates new content without needing input data again.
  2. Step 2: Identify misuse in code

    Passing data to generate() is incorrect; it should generate based on learned patterns alone.
  3. Final Answer:

    generate() should not take data as input after training -> Option C
  4. Quick Check:

    generate() no input needed [OK]
Hint: generate() uses learned model, no extra data input [OK]
Common Mistakes:
  • Thinking train() shouldn't take data
  • Confusing model type
  • Assigning new_content before training
5. You want to create a Generative AI that writes short poems. Which steps best describe the process?
hard
A. Write poems manually, then use AI to classify them
B. Collect poem examples, train model on them, generate new poems
C. Train model on random text, then delete training data
D. Generate poems first, then collect examples to train

Solution

  1. Step 1: Understand the goal of Generative AI for poems

    The AI needs to learn from existing poems to create new ones.
  2. Step 2: Identify the correct sequence of actions

    Collecting examples, training the model, then generating new poems is the correct order.
  3. Final Answer:

    Collect poem examples, train model on them, generate new poems -> Option B
  4. Quick Check:

    Learn from examples, then create new [OK]
Hint: Train on examples first, then generate new content [OK]
Common Mistakes:
  • Trying to generate before training
  • Confusing classification with generation
  • Deleting training data too early