Question: # GRADED FUNCTION: masked _ accuracy def masked _ accuracy ( y _ true, y _ pred ) : Calculate masked accuracy
# GRADED FUNCTION: maskedaccuracy
def maskedaccuracyytrue, ypred:
Calculate masked accuracy for predicted labels.
Parameters:
ytrue tensor: True labels.
ypred tensor: Predicted logits
Returns:
accuracy tensor: Masked accuracy.
### START CODE HERE ###
# Calculate the loss for each item in the batch.
# You must always cast the tensors to the same type in order to use them in training. Since you will make divisions, it is safe to use tffloat data type.
ytrue tfcastytrue, tffloat
# Create the mask, ie the values that will be ignored
mask None
mask tfcastmask tffloat
# Perform argmax to get the predicted values
ypredclass None
ypredclass tfcastypredclass, tffloat
# Compare the true values with the predicted ones
matchestruepred tfequalNone None
matchestruepred tfcastmatchestruepred tffloat
# Multiply the acc tensor with the masks
matchestruepred None
# Compute masked accuracy quotient between the total matches and the total valid values, ie the amount of nonmasked values
maskedacc NoneNone
### END CODE HERE ###
return maskedacc
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
