jade
jade

Reputation: 27

Getting an error like TypeError: cannot unpack non-iterable float object

Am computing the cost, but facing the error as TypeError: cannot unpack non-iterable float object

#Compute cost
def compute_cost(A2,y,parameters):
    m=y.shape[0]
    logprobs = y*np.log(A2) + (1-y)*np.log(1-A2)
    cost = -1/m*np.sum(logprobs)
    cost = float(np.squeeze(cost))  # makes sure cost is the dimension we expect.
    assert(isinstance(cost, float))
    return cost

#Calling the function
A2, y, parameters =compute_cost(A2,y,parameters)
print("cost = " + str(compute_cost(A2, y, parameters)))

TypeError                                 Traceback (most recent call last)
<ipython-input-533-089c3da0f833> in <module>
1 A2, y, parameters =compute_cost(A2,y,parameters)
2 print("cost = " + str(compute_cost(A2, y, parameters)))

TypeError: cannot unpack non-iterable float object

Upvotes: 1

Views: 5585

Answers (1)

aneroid
aneroid

Reputation: 15987

It's from the line A2, y, parameters =compute_cost(A2,y,parameters). You're calling compute_cost but assigning its return value (a single float) to three variables.

That line should be something like:

cost = compute_cost(A2,y,parameters)

Also, you don't seem to be using the parameters parameter anywhere. Nor have you set the initial values for A2, y and parameters.

Upvotes: 3

Related Questions