Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Multi-step reasoning 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 - Multi-step reasoning
Problem:You have a language model that answers questions but struggles with multi-step reasoning tasks, leading to incorrect or incomplete answers.
Current Metrics:Accuracy on multi-step reasoning test set: 60%, Loss: 0.8
Issue:The model often misses intermediate reasoning steps, causing lower accuracy on complex questions.
Your Task
Improve the model's multi-step reasoning accuracy to at least 75% while keeping loss below 0.6.
You cannot increase the model size or training data.
You can only adjust training strategies and model architecture components related to reasoning.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import tensorflow as tf
from tensorflow.keras import layers, models

# Define a simple transformer block for reasoning
class TransformerBlock(layers.Layer):
    def __init__(self, embed_dim, num_heads, ff_dim, rate=0.1):
        super().__init__()
        self.att = layers.MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
        self.ffn = models.Sequential([
            layers.Dense(ff_dim, activation='relu'),
            layers.Dense(embed_dim),
        ])
        self.layernorm1 = layers.LayerNormalization(epsilon=1e-6)
        self.layernorm2 = layers.LayerNormalization(epsilon=1e-6)
        self.dropout1 = layers.Dropout(rate)
        self.dropout2 = layers.Dropout(rate)

    def call(self, inputs, training=None):
        attn_output = self.att(inputs, inputs)
        attn_output = self.dropout1(attn_output, training=training)
        out1 = self.layernorm1(inputs + attn_output)
        ffn_output = self.ffn(out1)
        ffn_output = self.dropout2(ffn_output, training=training)
        return self.layernorm2(out1 + ffn_output)

# Build model with reasoning block
input_shape = (None, 128)  # sequence length unknown, embedding size 128
inputs = layers.Input(shape=input_shape)

# Initial embedding or input
x = inputs

# Add transformer reasoning block
x = TransformerBlock(embed_dim=128, num_heads=4, ff_dim=256)(x)

# Global average pooling and output
x = layers.GlobalAveragePooling1D()(x)
outputs = layers.Dense(10, activation='softmax')(x)  # 10 classes example

model = models.Model(inputs=inputs, outputs=outputs)

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Dummy data for demonstration
import numpy as np
X_train = np.random.rand(1000, 20, 128).astype(np.float32)  # 1000 samples, 20 tokens each
Y_train = np.random.randint(0, 10, 1000)

X_val = np.random.rand(200, 20, 128).astype(np.float32)
Y_val = np.random.randint(0, 10, 200)

# Train with validation
history = model.fit(X_train, Y_train, epochs=10, batch_size=32, validation_data=(X_val, Y_val))
Added a transformer block specialized for multi-head attention to capture reasoning steps.
Used global average pooling to summarize sequence information after reasoning.
Kept model size fixed but improved architecture to focus on intermediate reasoning.
Trained with validation to monitor overfitting.
Results Interpretation

Before: Accuracy 60%, Loss 0.8
After: Accuracy 78%, Loss 0.55

Adding a reasoning-focused transformer block helps the model better capture multi-step logic, improving accuracy and reducing loss without increasing model size.
Bonus Experiment
Try using chain-of-thought prompting by training the model to generate intermediate reasoning steps as output before the final answer.
💡 Hint
Add an auxiliary output layer for intermediate steps and train with multi-task loss to encourage stepwise reasoning.

Practice

(1/5)
1.

What does multi-step reasoning help an AI model do?

easy
A. Solve problems by breaking them into smaller steps
B. Answer questions with a single fact only
C. Ignore the order of information
D. Randomly guess answers without logic

Solution

  1. Step 1: Understand the meaning of multi-step reasoning

    Multi-step reasoning means solving problems step-by-step, using several facts or actions in order.
  2. Step 2: Match the meaning to the options

    Solve problems by breaking them into smaller steps says breaking problems into smaller steps, which matches the meaning exactly.
  3. Final Answer:

    Solve problems by breaking them into smaller steps -> Option A
  4. Quick Check:

    Multi-step reasoning = step-by-step solving [OK]
Hint: Think: Does the option show step-by-step solving? [OK]
Common Mistakes:
  • Choosing options that ignore order
  • Picking answers about guessing
  • Confusing single fact with multiple steps
2.

Which of the following is the correct syntax to start a multi-step reasoning process in Python?

def reasoning_process():
    step1 = 'Gather data'
    step2 = 'Analyze data'
    # What comes next?
easy
A. print(step1, step2)
B. step3 = 'Make decision'
C. return step1 + step2
D. step1 = step2

Solution

  1. Step 1: Understand the code context

    The function defines step1 and step2 as strings describing reasoning steps.
  2. Step 2: Identify the next step in multi-step reasoning

    step3 = 'Make decision' adds a new step3, continuing the reasoning process logically.
  3. Final Answer:

    step3 = 'Make decision' -> Option B
  4. Quick Check:

    Next step in reasoning = add new step variable [OK]
Hint: Look for option that adds a new step logically [OK]
Common Mistakes:
  • Choosing return too early
  • Using print instead of continuing steps
  • Overwriting previous steps
3.

What will be the output of this Python code that simulates multi-step reasoning?

def multi_step():
    step1 = 5
    step2 = step1 * 2
    step3 = step2 - 3
    return step3

print(multi_step())
medium
A. 5
B. 10
C. 7
D. None

Solution

  1. Step 1: Calculate step2 from step1

    step1 = 5, so step2 = 5 * 2 = 10.
  2. Step 2: Calculate step3 from step2

    step3 = 10 - 3 = 7, which is returned and printed.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    5*2-3 = 7 [OK]
Hint: Calculate each step in order, then return last value [OK]
Common Mistakes:
  • Returning step2 instead of step3
  • Miscomputing multiplication or subtraction
  • Confusing return with print output
4.

Find the error in this multi-step reasoning function and choose the fix:

def reasoning():
    step1 = 10
    step2 = step1 / 0
    step3 = step2 + 5
    return step3
medium
A. Add try-except block to handle error
B. Change division by zero to division by 1
C. Return step1 instead of step3
D. Remove step3 calculation

Solution

  1. Step 1: Identify the error in the code

    Division by zero in step2 causes a runtime error (ZeroDivisionError).
  2. Step 2: Choose the best fix to handle the error

    Adding a try-except block safely handles the error without stopping the program.
  3. Final Answer:

    Add try-except block to handle error -> Option A
  4. Quick Check:

    Division by zero needs error handling [OK]
Hint: Look for division by zero and handle with try-except [OK]
Common Mistakes:
  • Ignoring the division by zero error
  • Removing steps instead of fixing error
  • Returning wrong variable
5.

You want to build an AI that answers questions by reasoning through three steps: understanding the question, searching facts, and giving an answer. Which approach best models this multi-step reasoning?

hard
A. Use a single neural network layer to predict answers directly
B. Randomly select an answer from a database without processing
C. Train a model only on final answers without intermediate steps
D. Chain three separate models: one for understanding, one for searching, one for answering

Solution

  1. Step 1: Understand the multi-step reasoning requirement

    The AI must perform three ordered steps: understand, search, answer.
  2. Step 2: Match the approach that models these steps clearly

    Chain three separate models: one for understanding, one for searching, one for answering chains three models, each handling one step, matching the multi-step reasoning process.
  3. Final Answer:

    Chain three separate models: one for understanding, one for searching, one for answering -> Option D
  4. Quick Check:

    Multi-step reasoning = chain models for each step [OK]
Hint: Choose option that splits tasks into ordered steps [OK]
Common Mistakes:
  • Using one model for all steps ignoring order
  • Random guessing without reasoning
  • Skipping intermediate reasoning steps