Course 1 Week 3 Exercise 5 the cost function

[code removed - moderator]

How did you guys implement the cost function? See my attempt and help

The last term is incorrect. Please fix it keeping in mind that A2 is in probability scale.
Don’t forget to remove code from your post.

Thanks! I made the correction as follows:

[code removed - moderator]

it worked.

I have a type error. Please advise!

Term 1 logprobs: [[-0.69268589 -0. -0. ]] Term 2: [[0. 0.50014169 0.49976037]]

TypeError Traceback (most recent call last)
1 A2, t_Y = compute_cost_test_case()
----> 2 cost = compute_cost(A2, t_Y)
3 print("cost = " + str(compute_cost(A2, t_Y)))
5 compute_cost_test(compute_cost)

in compute_cost(A2, Y)
27 print(‘\n\n’)
—> 29 cost = (-1/m) * np.sum((logprobs),((1-Y)*np.log(1-A2)))
31 # debug

<array_function internals> in sum(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/ in sum(a, axis, dtype, out, keepdims, initial, where)
2228 return _wrapreduction(a, np.add, ‘sum’, axis, dtype, out, keepdims=keepdims,
→ 2229 initial=initial, where=where)

/opt/conda/lib/python3.7/site-packages/numpy/core/ in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
88 return reduction(axis=axis, out=out, **passkwargs)
—> 90 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

TypeError: only integer scalar arrays can be converted to a scalar index

Please read the markdown for the exercise. Your computation of logprobs and eventually the cost is incorrect.
If it helps, see the definition of J just above the writeup for Exercise 5 - compute_cost section.

I’m sure you’re right. I just didn’t come right looking at the code at the top of the exercise.

I ended up going back to the line I used in the backpropagation assignment and it worked immediately. Is there a place to look at model answers when the assignment is closed?

No, sorry, there are no “official solutions” that you can use as a reference. My guess is that this is because of concerns about cheating. Where would they put those in such a way that you can only see them after you’ve completed the exercise and how do they prevent them from being shared?

Onwards and upwards then… :grinning: