Assignement week 2 Logistic Regression

coursera # Neural Networks and deep learning week 2

Hey there I am doing Neural Networks and deep learning week 2 assignment.
I get most of tasks completed however when I run the final model my cost function becames quickly nan. It turns that my activations are all ones at the second iteration.
The part I am less sure are the dot product and the cost however they seem to make sense on informal testing:

A = sigmoid(np.dot(w.T, X) + b)   
cost = np.sum(((- np.log(A))*Y + (-np.log(1-A))*(1-Y)))/m


This is the error I get:
AssertionError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
131 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"
132 assert d[‘w’].shape == (X.shape[0], 1), f"Wrong shape for d[‘w’]. {d[‘w’].shape} != {(X.shape[0], 1)}"
→ 133 assert np.allclose(d[‘w’], expected_output[‘w’]), f"Wrong values for d[‘w’]. {d[‘w’]} != {expected_output[‘w’]}"
134
135 assert np.allclose(d[‘b’], expected_output[‘b’]), f"Wrong values for d[‘b’]. {d[‘b’]} != {expected_output[‘b’]}"

AssertionError: Wrong values for d[‘w’]. [[ 1.48583267]
[-1.37574823]
[-1.42633946]
[ 0.85382995]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]

{‘costs’: [array(0.69314718),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),
array(nan),

Any Help is appreciated, Marco

Hi @marcogiord ,

Your cost calculation statement is not correct. In python, you need to specify the use of a multiplication operator. Try this:

cost = - 1/m *(np.sum(Y * np.log(A)+(1-Y)*np.log(1-A)))

Please note that you need to use the “{}” formatting operator to get python code to render properly with “markdown”. The “*” symbol gets interpreted as markdown. I edited your post to add that formatting.

Are you sure that your earlier functions like optimize pass their test cases? If so, then you must be passing incorrect arguments when you call optimize from model.

If the values are becoming NaN, then one thing to check is that the learning rate is passed correctly.