Week 3 practice lab: Logistic Regression: Exercise 2 issues

I am currently working on Exercise 2 of the Logistic Regression Lab. When I tried out my code on the first test, it worked perfectly well, but on the second test, it generated this output: (every number is the loss from that particular example)

“0.2068919948121915
2.4083107540663406e-05
2.538168593248454e-06
7.586343659223303
1.1101563638691179
0.011838375352335713
0.044360906440639794
0.03254272153899156
5.4890240538884116e-06
1.1157312326628153e-06
16.523366687016427
13.646752130455758
5.059646036756818e-10
4.107825191113164e-14
29.936630501374903
8.881784197001256e-16
6.661338147750941e-16
33.84642881178094
nan
nan
inf
nan
inf
36.04365338911715
nan
nan
nan
inf
inf
inf
nan
nan
inf
nan
inf
inf
inf
nan
inf
inf
nan
inf
nan
inf
inf
inf
nan
nan
nan
nan
nan
nan
nan
inf
inf
inf
nan
inf
nan
nan
nan
inf
inf
inf
inf
inf
nan
inf
nan
nan
inf
nan
nan
nan
nan
nan
nan
nan
inf
inf
nan
nan
nan
nan
nan
nan
inf
nan
nan
inf
nan
nan
inf
nan
nan
nan
nan
nan
nan
nan
nan
Cost at test w and b (non-zeros): nan
1.3132616875182228
2.1269280110429714
3.048587351573745
4.01814992791781
2.6267317445131875
1.7319810399426505
1.4153290864690846
5.566970697023261
0.007394181114983405
0.008860555472210385
1.7461071120044385”

I then got an assertion error because my total_cost was 1.7461071120044385 instead of the cost it wanted.

It seems there is an error in your code.

The first thing to check is that all of the blocks of your code are correctly indented. Python relies on indentation to define blocks of code.

This makes it very easy to write code that will compile but give incorrect results.

I do not see any Indentation Errors in my code, and I do not know what else it could be.

“nan” means “not a number”. This indicates an error in some of your math operations.

Make sure that the linear combination calculation

z_{\mathbf{w},b}(\mathbf{x}^{(i)}) = \mathbf{w} \cdot \mathbf{x^{(i)}} + b,

the sigmoid application

f_{\mathbf{w},b}(\mathbf{x}^{(i)}) = g(z_{\mathbf{w},b}(\mathbf{x}^{(i)})),

where the function g is the sigmoid function you implemented in Exercise 1, and the total cost

J(\mathbf{w}, b) = -\frac{1}{m}\sum_{i=0}^{m-1} [y^{(i)} \log\left(f_{\mathbf{w}, b}\left( \mathbf{x}^{(i)} \right) \right) + \left( 1 - y^{(i)}\right) \log \left( 1 - f_{\mathbf{w}, b}\left( \mathbf{x}^{(i)} \right) \right)]

are implemented correctly.

It turned out that I defined z_wb outside of the “for i in range(m):” loop. Thank you for helping me out!

1 Like

Glad you found the problem! Loop bugs can be tricky, but catching them is a great learning experience. Happy coding!