Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a TensorFlow SavedModel for serving.
TensorFlow
import tensorflow as tf model = tf.saved_model.load([1])
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the model path.
Using a variable name without defining it.
✗ Incorrect
The model path must be a string specifying the directory where the SavedModel is stored.
2fill in blank
mediumComplete the code to create a TensorFlow Serving signature for prediction.
TensorFlow
infer = model.signatures[[1]]Attempts:
3 left
💡 Hint
Common Mistakes
Using the signature name without quotes.
Using an incorrect signature key.
✗ Incorrect
The signature key must be a string matching the saved signature name, usually 'serving_default'.
3fill in blank
hardFix the error in the code to prepare input tensor for serving.
TensorFlow
import tensorflow as tf input_tensor = tf.constant([1])
Attempts:
3 left
💡 Hint
Common Mistakes
Passing multiple arguments instead of a list.
Using non-TensorFlow array types.
✗ Incorrect
tf.constant expects a list or nested list to create a tensor; a nested list represents batch input.
4fill in blank
hardFill both blanks to run inference and get the prediction output.
TensorFlow
output = infer([1]) prediction = output[[2]]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the input key string instead of the tensor variable.
Using variable names instead of output key strings.
✗ Incorrect
The input tensor variable is passed to the signature, and the output is accessed by the output key string.
5fill in blank
hardFill all three blanks to define a simple TF Serving REST API using Flask.
TensorFlow
from flask import Flask, request, jsonify import tensorflow as tf app = Flask(__name__) model = tf.saved_model.load([1]) infer = model.signatures[[2]] @app.route('/predict', methods=['POST']) def predict(): data = request.get_json() input_tensor = tf.constant(data[[3]]) output = infer(input_tensor) return jsonify({'prediction': output['output_1'].numpy().tolist()})
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names instead of strings for model path or signature.
Mismatching JSON input key.
✗ Incorrect
The model directory is a string path, the signature key is 'serving_default', and the JSON input key is 'input_data'.
