Challenge - 5 Problems
REST API Prediction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Flask API prediction code?
Given the following Flask REST API code snippet that loads a TensorFlow model and predicts from JSON input, what will be the JSON response if the input is {"data": [[1.0, 2.0, 3.0]]}?
TensorFlow
from flask import Flask, request, jsonify import tensorflow as tf import numpy as np app = Flask(__name__) model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(3,))]) model.compile(optimizer='adam', loss='mse') model.set_weights([np.zeros((3,1)), np.zeros((1,))]) @app.route('/predict', methods=['POST']) def predict(): input_json = request.get_json() data = np.array(input_json['data']) preds = model.predict(data) return jsonify({'predictions': preds.flatten().tolist()}) # Assume model weights are all zeros for simplicity
Attempts:
2 left
💡 Hint
The model is untrained and initialized with zeros, so output will be zero regardless of input.
✗ Incorrect
The model has one Dense layer with weights initialized to zero by default. So any input multiplied by zero weights plus zero bias results in zero output.
❓ Model Choice
intermediateWhich TensorFlow model is best for a REST API predicting image classes?
You want to build a REST API that receives images and returns predicted classes. Which TensorFlow model architecture is most suitable for this task?
Attempts:
2 left
💡 Hint
Think about which model type is best for image data.
✗ Incorrect
CNNs are designed to extract spatial features from images, making them ideal for image classification tasks in REST APIs.
❓ Hyperparameter
advancedWhich batch size is best for fast REST API predictions with TensorFlow?
You deploy a TensorFlow model in a REST API for real-time predictions. Which batch size should you choose to minimize latency?
Attempts:
2 left
💡 Hint
Smaller batch sizes reduce waiting time for predictions.
✗ Incorrect
Batch size 1 means the model predicts immediately on each request, minimizing latency for real-time REST API calls.
❓ Metrics
advancedWhich metric is best to monitor REST API model classification accuracy?
Your REST API returns predicted classes for input data. Which metric should you monitor to evaluate model accuracy during training?
Attempts:
2 left
💡 Hint
Think about classification tasks and common metrics.
✗ Incorrect
Accuracy measures the fraction of correct predictions, which is suitable for classification tasks served by REST APIs.
🔧 Debug
expertWhy does this TensorFlow REST API prediction code raise an error?
Examine the code below. When sending a POST request with JSON {"data": [[1,2,3]]}, the API raises a ValueError. What is the cause?
TensorFlow
from flask import Flask, request, jsonify import tensorflow as tf import numpy as np app = Flask(__name__) model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(4,))]) model.compile(optimizer='adam', loss='mse') @app.route('/predict', methods=['POST']) def predict(): input_json = request.get_json() data = np.array(input_json['data']) preds = model.predict(data) return jsonify({'predictions': preds.flatten().tolist()})
Attempts:
2 left
💡 Hint
Check the input shape expected by the model versus the input provided.
✗ Incorrect
The model expects input vectors of length 4, but the input JSON provides vectors of length 3, causing a shape mismatch error.
