Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 5 Instructions: You will maked a masked version of the accuracy function. You will need to perform an argmax to get the predicted label
Exercise
Instructions: You will maked a masked version of the accuracy function. You will need to perform an argmax to get the predicted label for each element in the batch. Remember to provide the appropriate axis in the argmax function. Furthermore, remember to use only tensorflow operations. Even though numpy has every function you will need, to pass it as a loss function andor metric function, you must use tensorflow operations, due to internal optimizations that Tensorflow performs for reliable fitting. The following tensorflow functions are already loaded in memory, so you can directly call them.
tfequal, equivalent to npequal
tfcast, equivalent to npastype
tfreducesum, equiavalent to npsum
tfmath.argmax, equivalent to npargmax
You may need t# 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 masktruelabels
predictedlogits
printmaskedaccuracytruelabels, predictedlogitsedaccf.float while casting
Expected output:
tfTensor shape dtypefloat
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started