What is a REST API in the context of machine learning predictions?
A REST API is a way to let other programs ask your machine learning model for predictions over the internet using simple web requests.
Click to reveal answer
beginner
Why do we use JSON format in REST APIs for ML predictions?
JSON is easy to read and write for both humans and machines, making it a popular format to send input data and receive prediction results in REST APIs.
Click to reveal answer
beginner
What Python library is commonly used to create REST APIs for TensorFlow models?
Flask is a simple and popular Python library used to build REST APIs that serve TensorFlow model predictions.
Click to reveal answer
beginner
What is the typical HTTP method used to send data for prediction in a REST API?
POST method is typically used because it allows sending data in the request body to the server for prediction.
Click to reveal answer
intermediate
How do you ensure your REST API returns predictions quickly and reliably?
By loading the model once when the API starts, handling requests efficiently, and returning only necessary data in a simple format like JSON.
Click to reveal answer
Which HTTP method is best suited for sending input data to a REST API for predictions?
APOST
BGET
CDELETE
DPUT
✗ Incorrect
POST is used to send data in the request body, which is ideal for prediction inputs.
What format is commonly used to send input data and receive predictions in REST APIs?
ACSV
BJSON
CXML
DYAML
✗ Incorrect
JSON is lightweight and easy to use for data exchange in REST APIs.
Which Python library is often used to build a REST API for TensorFlow models?
ADjango
BNumPy
CPandas
DFlask
✗ Incorrect
Flask is a lightweight web framework commonly used for REST APIs.
What is the main role of a REST API in machine learning?
ATrain models
BStore data
CServe predictions
DVisualize data
✗ Incorrect
REST APIs allow other programs to get predictions from a trained model.
Why should the model be loaded once when starting the REST API server?
ATo speed up prediction requests
BTo log requests
CTo retrain the model
DTo save memory
✗ Incorrect
Loading the model once avoids reloading it for every request, making predictions faster.
Explain how you would create a simple REST API to serve TensorFlow model predictions.
Think about the steps from loading the model to sending back the prediction.
You got /6 concepts.
Describe why REST APIs are useful for sharing machine learning predictions with other applications.
Consider how apps or websites might get predictions from your model.
You got /5 concepts.
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
Step 1: Understand REST API role
A REST API acts as a bridge to send data and receive results from a model.
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.
Final Answer:
To allow other programs to send data and get predictions easily -> Option D
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
Step 1: Identify correct Flask route syntax for POST
Flask uses @app.route with methods=['POST'] to define POST endpoints.
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.
Final Answer:
app.route('/predict', methods=['POST'])\ndef predict():\n data = request.get_json() -> Option C
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?
B. Missing methods=['POST'] in @app.route decorator
C. request.get_json() only works with GET requests
D. jsonify() cannot convert lists
Solution
Step 1: Check route decorator for HTTP methods
The route lacks methods=['POST'], so it defaults to GET only, causing errors on POST requests.
Step 2: Confirm other parts are correct
model.predict() can accept arrays, get_json() works with POST, and jsonify converts lists fine.
Final Answer:
Missing methods=['POST'] in @app.route decorator -> Option B
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
Step 1: Understand batch prediction needs
Batch prediction means processing many inputs at once for efficiency.
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.
Final Answer:
Accept a JSON list of inputs, convert to numpy array, predict batch, return list of predictions -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.