Question: # GRADED FUNCTION: predict def predict ( sentence , model, sentence _ vectorizer, tag _ map ) : Predict NER labels for
# GRADED FUNCTION: predict
def predictsentence model, sentencevectorizer, tagmap:
Predict NER labels for a given sentence using a trained model.
Parameters:
sentence str: Input sentence.
model tfkeras.Model: Trained NER model.
sentencevectorizer tfkeras.layers.TextVectorization: Sentence vectorization layer.
tagmap dict: Dictionary mapping tag IDs to labels.
Returns:
predictions list: Predicted NER labels for the sentence.
### START CODE HERE ###
# Convert the sentence into ids
sentencevectorized None
# Expand its dimension to make it appropriate to pass to the model
sentencevectorized tfexpanddimsNone None
# Get the model output
output None
# Get the predicted labels for each token, using argmax function and specifying the correct axis to perform the argmax
outputs npargmaxNone axis None
# Next line is just to adjust outputs dimension. Since this function expects only one input to get a prediction, outputs will be something like
# so to avoid heavy notation below, let's transform it into
outputs outputs
# Get a list of all keys, remember that the tagmap was built in a way that each label id matches its index in a list
labels listtagmap.keys
pred
# Iterating over every predicted token in outputs list
for tagidx in None
predlabel None
pred.appendNone
### END CODE HERE ###
return pred
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
