Compute the cost

GRADED FUNCTION: compute_cost

@tf.function
def compute_cost(logits, labels):

"""
Computes the cost

Arguments:
logits -- output of forward propagation (output of the last LINEAR unit), of shape (6, number of examples)
labels -- "true" labels vector, same shape as Z3

Returns:
cost - Tensor of the cost function
"""

## YOUR CODE STARTS HERE(1 line of code)
cost = tf.reduce_mean(tf.keras.losses.binary_crossentropy(y_true = labels, y_pred = logits, from_logits=True))
# YOUR CODE ENDS HERE

return cost

Output

NameError Traceback (most recent call last)
in
1 # GRADED FUNCTION: compute_cost
----> 2 @tf.function
3 def compute_cost(logits, labels):
4
5 “”"

NameError: name ‘tf’ is not defined

I am getting this error.

Hi, @rdrajdipdas1.

You probably forgot to run the first cell:

import h5py
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.python.framework.ops import EagerTensor
from tensorflow.python.ops.resource_variable_ops import ResourceVariable
import time

Let me know if you need more help :slight_smile: