Bird
Raised Fist0
TensorFlowml~20 mins

REST API for predictions in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
REST API Prediction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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
A{"predictions": [3.0]}
B{"predictions": [0.0]}
C{"predictions": [1.0]}
D{"error": "Invalid input shape"}
Attempts:
2 left
💡 Hint
The model is untrained and initialized with zeros, so output will be zero regardless of input.
❓ Model Choice
intermediate
1:30remaining
Which 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?
AA Convolutional Neural Network (CNN) model
BA Sequential model with Dense layers only
CA Recurrent Neural Network (RNN) model
DA Transformer model designed for text sequences
Attempts:
2 left
💡 Hint
Think about which model type is best for image data.
❓ Hyperparameter
advanced
1:30remaining
Which 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?
ABatch size of 512
BBatch size of 32
CBatch size of 128
DBatch size of 1
Attempts:
2 left
💡 Hint
Smaller batch sizes reduce waiting time for predictions.
❓ Metrics
advanced
1:30remaining
Which 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?
AMean Absolute Error (MAE)
BMean Squared Error (MSE)
CAccuracy
DPerplexity
Attempts:
2 left
💡 Hint
Think about classification tasks and common metrics.
🔧 Debug
expert
2:30remaining
Why 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()})
AInput data shape (1,3) does not match model input shape (4,)
BJSON input key should be 'inputs' not 'data'
CFlask app is missing @app.route decorator
DModel is not compiled before prediction
Attempts:
2 left
💡 Hint
Check the input shape expected by the model versus the input provided.

Practice

(1/5)
1. What is the main purpose of creating a REST API for a TensorFlow model?
easy
A. To save the model in a different file format
B. To train the model faster on a server
C. To visualize the model's architecture
D. To allow other programs to send data and get predictions easily

Solution

  1. Step 1: Understand REST API role

    A REST API acts as a bridge to send data and receive results from a model.
  2. Step 2: Match purpose with options

    Only To allow other programs to send data and get predictions easily describes this role correctly as it enables easy data exchange for predictions.
  3. Final Answer:

    To allow other programs to send data and get predictions easily -> Option D
  4. Quick Check:

    REST API purpose = Easy data exchange for predictions [OK]
Hint: REST API = send input, get output from model [OK]
Common Mistakes:
  • Confusing API with training process
  • Thinking API is for visualization
  • Assuming API changes model files
2. Which Flask code snippet correctly defines a POST endpoint to receive JSON input for predictions?
easy
A. app.post('/predict')\ndef predict():\n data = request.get_json()
B. app.route('/predict')\ndef predict():\n data = request.get_json()
C. app.route('/predict', methods=['POST'])\ndef predict():\n data = request.get_json()
D. app.get('/predict')\ndef predict():\n data = request.get_json()

Solution

  1. Step 1: Identify correct Flask route syntax for POST

    Flask uses @app.route with methods=['POST'] to define POST endpoints.
  2. Step 2: Check code snippets

    Only app.route('/predict', methods=['POST'])\ndef predict():\n data = request.get_json() uses @app.route with methods=['POST'] and calls request.get_json() correctly.
  3. Final Answer:

    app.route('/predict', methods=['POST'])\ndef predict():\n data = request.get_json() -> Option C
  4. Quick Check:

    Flask POST route = @app.route with methods=['POST'] [OK]
Hint: Flask POST = @app.route with methods=['POST'] [OK]
Common Mistakes:
  • Using app.post decorator (not standard Flask)
  • Missing methods=['POST'] in route
  • Using app.get for POST requests
3. Given this Flask code snippet, what will be the output if the input JSON is {'input': [1, 2, 3]} and the model predicts sum of inputs?
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    inputs = data['input']
    prediction = sum(inputs)
    return jsonify({'prediction': prediction})
medium
A. {"prediction": 6}
B. {"prediction": [1, 2, 3]}
C. {"prediction": 123}
D. Error: KeyError

Solution

  1. Step 1: Extract input and compute prediction

    The input JSON has 'input': [1, 2, 3]. The code sums these values: 1+2+3=6.
  2. Step 2: Return prediction as JSON

    The code returns jsonify({'prediction': 6}), so output is {"prediction": 6}.
  3. Final Answer:

    {"prediction": 6} -> Option A
  4. Quick Check:

    Sum([1,2,3]) = 6 [OK]
Hint: Sum input list to get prediction output [OK]
Common Mistakes:
  • Returning input list instead of sum
  • Concatenating numbers as string
  • Misreading JSON keys causing KeyError
4. You wrote this Flask API code but get an error when sending POST requests. What is the likely cause?
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/predict')
def predict():
    data = request.get_json()
    prediction = model.predict(data['input'])
    return jsonify({'prediction': prediction.tolist()})
medium
A. model.predict() cannot accept JSON data
B. Missing methods=['POST'] in @app.route decorator
C. request.get_json() only works with GET requests
D. jsonify() cannot convert lists

Solution

  1. Step 1: Check route decorator for HTTP methods

    The route lacks methods=['POST'], so it defaults to GET only, causing errors on POST requests.
  2. Step 2: Confirm other parts are correct

    model.predict() can accept arrays, get_json() works with POST, and jsonify converts lists fine.
  3. Final Answer:

    Missing methods=['POST'] in @app.route decorator -> Option B
  4. Quick Check:

    POST route needs methods=['POST'] [OK]
Hint: Add methods=['POST'] to route for POST requests [OK]
Common Mistakes:
  • Forgetting to specify POST method in route
  • Assuming get_json works only with GET
  • Thinking jsonify can't handle lists
5. You want to deploy a TensorFlow model with a REST API that accepts multiple inputs and returns predictions for each. Which approach is best to handle batch inputs in your Flask API?
hard
A. Accept a JSON list of inputs, convert to numpy array, predict batch, return list of predictions
B. Accept one input at a time, call model.predict repeatedly for each
C. Save each input to a file, then load and predict later
D. Use GET requests with inputs in URL parameters

Solution

  1. Step 1: Understand batch prediction needs

    Batch prediction means processing many inputs at once for efficiency.
  2. Step 2: Choose method to handle batch inputs

    Accept a JSON list of inputs, convert to numpy array, predict batch, return list of predictions accepts JSON list, converts to array, predicts all at once, then returns all predictions, which is efficient and standard.
  3. Final Answer:

    Accept a JSON list of inputs, convert to numpy array, predict batch, return list of predictions -> Option A
  4. Quick Check:

    Batch input = JSON list + batch predict [OK]
Hint: Batch predict by sending list and returning list [OK]
Common Mistakes:
  • Predicting one input at a time (slow)
  • Using files instead of direct JSON
  • Using GET for large input data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard