Answered step by step
Verified Expert Solution
Link Copied!

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 5
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 and/or 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.
tf.equal, equivalent to np.equal
tf.cast, equivalent to np.astype
tf.reduce_sum, equiavalent to np.sum
tf.math.argmax, equivalent to np.argmax
You may need t# GRADED FUNCTION: masked_accuracy
def masked_accuracy(y_true, y_pred):
"""
Calculate masked accuracy for predicted labels.
Parameters:
y_true (tensor): True labels.
y_pred (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 tf.float32 data type.
y_true = tf.cast(y_true, tf.float32)
# Create the mask, i.e., the values that will be ignored
mask = None
mask = tf.cast(mask, tf.float32)
# Perform argmax to get the predicted values
y_pred_class = None
y_pred_class = tf.cast(y_pred_class, tf.float32)
# Compare the true values with the predicted ones
matches_true_pred = tf.equal(None, None)
matches_true_pred = tf.cast(matches_true_pred , tf.float32)
# Multiply the acc tensor with the masks
matches_true_pred *= None
# Compute masked accuracy (quotient between the total matches and the total valid values, i.e., the amount of non-masked values)
masked_acc = None/None
### END CODE HERE ###
return masktrue_labels =[0,1,2,0]
predicted_logits =[[0.1,0.6,0.3],[0.2,0.7,0.1],[0.1,0.5,0.4],[0.4,0.4,0.2]]
print(masked_accuracy(true_labels, predicted_logits))ed_accf.float32 while casting
Expected output:
tf.Tensor(0.5, shape=(), dtype=float32)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions