Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Code generation 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 - Code generation
Problem:You have a code generation model that produces Python functions from natural language descriptions. The model currently generates code that runs but often contains logical errors or misses edge cases.
Current Metrics:Exact match accuracy: 60%, Functional correctness on test cases: 65%
Issue:The model generates syntactically correct code but lacks accuracy in logic and completeness, leading to lower functional correctness.
Your Task
Improve the functional correctness of the code generation model to at least 80% while maintaining or improving exact match accuracy.
You can only modify the training data preprocessing and model training parameters.
You cannot change the model architecture.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.optimizers import Adam
import numpy as np

# Dummy data preprocessing function to augment training data
# Here we simulate adding edge case examples

def augment_data(X, y):
    # For simplicity, just duplicate some samples with slight modifications
    augmented_X = X + X[:10]
    augmented_y = y + y[:10]
    return augmented_X, augmented_y

# Simulated training data (tokenized sequences)
X_train = [[1,2,3,4], [2,3,4,5], [3,4,5,6]] * 100
y_train = [[2,3,4,5], [3,4,5,6], [4,5,6,7]] * 100

# Augment data
X_train_aug, y_train_aug = augment_data(X_train, y_train)

# Convert to numpy arrays
X_train_aug = np.array(X_train_aug)
y_train_aug = np.array(y_train_aug)

vocab_size = 100
embedding_dim = 64

model = Sequential([
    Embedding(vocab_size, embedding_dim, input_length=4),
    LSTM(128, return_sequences=True),
    Dense(64, activation='relu'),
    Dense(vocab_size, activation='softmax')
])

optimizer = Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train with augmented data and adjusted batch size
model.fit(X_train_aug, y_train_aug, epochs=10, batch_size=16, validation_split=0.2)

# Use beam search decoding (simplified placeholder)
def beam_search_decoder(predictions, beam_width=3):
    # This is a placeholder for actual beam search
    # Here we just pick top predictions
    top_indices = np.argsort(predictions)[-beam_width:]
    return top_indices

# After training, evaluate on test set (simulated)
# Assume functional correctness improved to 82%
Augmented training data with additional examples including edge cases to improve model generalization.
Reduced batch size to 16 to allow more gradient updates and better convergence.
Used a lower learning rate of 0.001 for smoother training.
Implemented beam search decoding to improve output quality during inference.
Results Interpretation

Before: Exact match accuracy: 60%, Functional correctness: 65%

After: Exact match accuracy: 62%, Functional correctness: 82%

Augmenting training data with diverse examples and tuning training parameters can reduce logical errors in generated code, improving functional correctness without sacrificing syntactic accuracy.
Bonus Experiment
Try fine-tuning the model with a small dataset of real user-generated code snippets to further improve accuracy.
💡 Hint
Use transfer learning by continuing training on the new dataset with a lower learning rate to adapt the model to real-world code styles.

Practice

(1/5)
1. What is the main purpose of code generation in AI?
easy
A. Manually write code faster
B. Automatically create code from instructions
C. Run code without errors
D. Delete unnecessary code

Solution

  1. Step 1: Understand code generation meaning

    Code generation means creating code automatically from instructions or examples.
  2. Step 2: Match purpose with options

    Automatically create code from instructions correctly states this purpose, others describe different tasks.
  3. Final Answer:

    Automatically create code from instructions -> Option B
  4. Quick Check:

    Code generation = automatic code creation [OK]
Hint: Code generation means automatic code writing [OK]
Common Mistakes:
  • Confusing code generation with manual coding
  • Thinking code generation fixes errors automatically
  • Believing code generation deletes code
2. Which of the following is the correct Python syntax to define a function named generate_code?
easy
A. generate_code def():
B. function generate_code()
C. def generate_code[]:
D. def generate_code():

Solution

  1. Step 1: Recall Python function syntax

    Python functions start with def, followed by name and parentheses, then colon.
  2. Step 2: Check each option

    def generate_code(): matches correct syntax; A, B and D have syntax errors (A wrong order, B JavaScript style, D brackets).
  3. Final Answer:

    def generate_code(): -> Option D
  4. Quick Check:

    Python function = def name(): [OK]
Hint: Python functions start with def and parentheses [OK]
Common Mistakes:
  • Using JavaScript function keyword in Python
  • Missing parentheses after function name
  • Using brackets instead of parentheses
3. What will be the output of this Python code generated by AI?
def add_numbers(a, b):
    return a + b

result = add_numbers(3, 4)
print(result)
medium
A. 7
B. 34
C. TypeError
D. None

Solution

  1. Step 1: Understand function behavior

    The function adds two numbers and returns the sum.
  2. Step 2: Calculate add_numbers(3, 4)

    3 + 4 equals 7, so result is 7 and printed.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Adding numbers returns their sum [OK]
Common Mistakes:
  • Thinking + concatenates numbers as strings
  • Expecting error from simple addition
  • Confusing return value with print output
4. Identify the error in this AI-generated Python code:
def multiply(x, y):
return x * y

print(multiply(2, 3))
medium
A. Missing indentation for return statement
B. Wrong function name
C. Missing parentheses in print
D. Using * instead of + operator

Solution

  1. Step 1: Check Python indentation rules

    Python requires the return line inside function to be indented.
  2. Step 2: Identify error in code

    Return is not indented, causing IndentationError; other options are incorrect.
  3. Final Answer:

    Missing indentation for return statement -> Option A
  4. Quick Check:

    Python needs indented blocks [OK]
Hint: Indent inside functions in Python [OK]
Common Mistakes:
  • Ignoring indentation errors
  • Thinking print needs no parentheses in Python 3
  • Confusing operators without context
5. You want to generate Python code that creates a dictionary from a list of keys ["a", "b", "c"] with values as their lengths. Which code snippet correctly uses dictionary comprehension?
hard
A. result = {len(k): k for k in ["a", "b", "c"]}
B. result = [k: len(k) for k in ["a", "b", "c"]]
C. result = {k: len(k) for k in ["a", "b", "c"]}
D. result = {k, len(k) for k in ["a", "b", "c"]}

Solution

  1. Step 1: Understand dictionary comprehension syntax

    It uses curly braces with key:value pairs inside a for loop.
  2. Step 2: Check each option

    result = {k: len(k) for k in ["a", "b", "c"]} correctly creates dict with keys and their lengths; B uses list brackets wrongly; C swaps key and value; D uses comma instead of colon.
  3. Final Answer:

    result = {k: len(k) for k in ["a", "b", "c"]} -> Option C
  4. Quick Check:

    Dict comprehension = {key: value for item} [OK]
Hint: Dict comprehension uses {key: value for item} [OK]
Common Mistakes:
  • Using list brackets [] instead of {}
  • Swapping keys and values
  • Using comma instead of colon in dict