C2_W3_Lab_1 error

When running 2.44 3.4, the lab did not work in the right way. It overflowed. I am confused…

Code:

parameters_multi = nn_model(X_multi_norm, Y_multi_norm, num_iterations=100, print_cost=True)

print("W = " + str(parameters_multi[“W”]))
print("b = " + str(parameters_multi[“b”]))

W_multi = parameters_multi[“W”]
b_multi = parameters_multi[“b”]

Result:

Cost after iteration 0: 52.266244
Cost after iteration 1: 6618981604291.579102
Cost after iteration 2: 846311471320471930142720.000000
Cost after iteration 3: 108210469420893557562554204586770432.000000
Cost after iteration 4: 13835929311013789285734458757963686991802400768.000000
Cost after iteration 5: 1769079654897126770857256087341856345639617085546711482368.000000
Cost after iteration 6: 226196792063663968610530602691698484905007750099769924455996984393728.000000
Cost after iteration 7: 28921811744461971074263647029441669961057256873010218919233989563529600799080448.000000
Cost after iteration 8: 3697979918064753468611767061903613564854447867660278247773831145040239396492641364185448448.000000
Cost after iteration 9: 472828451939174241101160167524985525479677017422424281790690174628712046876677479756380366418217009152.000000
Cost after iteration 10: 60456451878244530430029171929007188343442993551648718108866132864433181150751073059623292032111550651175446511616.000000
Cost after iteration 11: 7730039422789805977543265648077239584304527807815652296397959581332504545474895051760172271236921257352072426273444806524928.000000
Cost after iteration 12: 988372748010822478507875547425075018179284770365458988985053588101430122555981400630442134759981168824130112868327892648533488201367552.000000
Cost after iteration 13: 126374606335177537346952003658909815802536310014330915178668880155622784852585336110695202906773248695662336387883048351735439918655576213016281088.000000
Cost after iteration 14: 16158419137428732557071000653563650415590689939276597030057474945531670419828762281445554618294888907475873148687125294604992298113099715419645342244122132480.000000
Cost after iteration 15: 2066036180784088932196025397644575441577376996106007468270517286640252130824718336621627302854273541652199449793115218461150252628845761183441746719397833845740979355648.000000
Cost after iteration 16: 264166034065888594994920277470848027424433222333237079115142656859937933655772737629977884983798853979600290690745963001240262348922298672576361613412997566575179978368383360958464.000000
Cost after iteration 17: 33776607691166578260156978892362428730432623733695873662095147388880126841226686985856082968172350797195948150977602656471011893601225970492984823147792106995880039840383265820638396360425472.000000
Cost after iteration 18: 4318720350090195007397869292663443417841863757956413633070920129669279127253670309255046811395317745601661521372590429033947121711994558964342935448252643620376984082906918890765715351353756504035426304.000000
Cost after iteration 19: 552197119166617028850131209212490444093105054755643021209521180569364014584883916970330077954404362306493696705664195679620776347778250461952377210559649878849461932276007861457626048523894693438641911680506789888.000000
Cost after iteration 20: 70604631394932144105822143178676692665470890816311924648653647041090519220645867506211199417932059332840987140501706109880335193740613515222609305528911316428376353486498080903763534169715068028865710213864540179863879811072.000000
Cost after iteration 21: 9027598662480696949791346234074689910217918656898007901767340465142563498978979872075747527490221771314542892597383773190924772640965134234731541576921036523041939283488631494732047689289065814216489241042450362705431794181789026615296.000000
Cost after iteration 22: 1154280335449395481067437683105948563797001418964732076327560912126540073829305817800488080959675675617504875358391955268529159374558627033823584923417578289140805501302290701208260652044467538082962603619273689835029631645767770208855801854951424.000000
Cost after iteration 23: 147587763104994789910827929846622944556688317698167280922796143807690307657626837573341062789309929452516452842045261885111475450824124391661241999487334529285182013460041416318747372374173425030077246278468425887808031052974204369428868183329886619392016384.000000
Cost after iteration 24: 18870760550430448032422151823432421034187229148334832210908129567559089569212959215897373298651027124534427451960260367428435156830652321854273385783079572678569835225541363602127040101614167841331660142172530082475360341806353036580160280061549308625518772129988870144.000000
Cost after iteration 25: 2412839630195806056086779727717868286202566319089730202051994440355653709722785033838408931386198874454777614749414534120780633665826925974391122547124641472125474550471115273757902239620878518259167792842121395213801366835927995292129781068945001974030670481163203200112882876416.000000
Cost after iteration 26: 308508767597638546987431066808771640538855783775107818633059004916463089227393414685663264111133720299752067319121686351861839254728439872730574809322337364163381163945687943778147841331009105531933192738157416928027465141051835709930310704314622784265849791764764885986421272104540517171200.000000
Cost after iteration 27: 39446326433593120950046469310987612741666700401366943235925957836628992364143012562959017745241431674821045489447943653937643500074979465691063014221000721230508072129669877103582591215393739234533793564156527250554049721377007996151617579978453306488876505946559964601420705452911222119100454312869888.000000
Cost after iteration 28: inf
Cost after iteration 29: inf
Cost after iteration 30: inf
Cost after iteration 31: inf
Cost after iteration 32: inf
Cost after iteration 33: inf
Cost after iteration 34: inf
Cost after iteration 35: inf
Cost after iteration 36: inf
Cost after iteration 37: inf
Cost after iteration 38: inf
Cost after iteration 39: inf
Cost after iteration 40: inf
Cost after iteration 41: inf
Cost after iteration 42: inf
Cost after iteration 43: inf
Cost after iteration 44: inf
Cost after iteration 45: inf
Cost after iteration 46: inf
Cost after iteration 47: inf
Cost after iteration 48: inf
Cost after iteration 49: inf
Cost after iteration 50: inf
Cost after iteration 51: inf
Cost after iteration 52: inf
Cost after iteration 53: inf
Cost after iteration 54: inf
Cost after iteration 55: inf
Cost after iteration 56: nan
Cost after iteration 57: nan
Cost after iteration 58: nan
Cost after iteration 59: nan
Cost after iteration 60: nan
Cost after iteration 61: nan
Cost after iteration 62: nan
Cost after iteration 63: nan
Cost after iteration 64: nan
Cost after iteration 65: nan
Cost after iteration 66: nan
Cost after iteration 67: nan
Cost after iteration 68: nan
Cost after iteration 69: nan
Cost after iteration 70: nan
Cost after iteration 71: nan
Cost after iteration 72: nan
Cost after iteration 73: nan
Cost after iteration 74: nan
Cost after iteration 75: nan
Cost after iteration 76: nan
Cost after iteration 77: nan
Cost after iteration 78: nan
Cost after iteration 79: nan
Cost after iteration 80: nan
Cost after iteration 81: nan
Cost after iteration 82: nan
Cost after iteration 83: nan
Cost after iteration 84: nan
Cost after iteration 85: nan
Cost after iteration 86: nan
Cost after iteration 87: nan
Cost after iteration 88: nan
Cost after iteration 89: nan
Cost after iteration 90: nan
Cost after iteration 91: nan
Cost after iteration 92: nan
Cost after iteration 93: nan
Cost after iteration 94: nan
Cost after iteration 95: nan
Cost after iteration 96: nan
Cost after iteration 97: nan
Cost after iteration 98: nan
Cost after iteration 99: nan
W = [[nan nan]]
b = [[nan]]

C:\Users\pangu\AppData\Local\Temp\ipykernel_22420\651274294.py:17: RuntimeWarning: overflow encountered in square
cost = np.sum((Y_hat - Y)**2)/(2*m)
C:\Users\pangu\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
C:\Users\pangu\AppData\Local\Temp\ipykernel_22420\583824931.py:14: RuntimeWarning: invalid value encountered in matmul
Z = np.matmul(W, X) + b

Code:

X_pred_multi = np.array([[1710, 7], [1200, 6], [2200, 8]]).T
Y_pred_multi = predict(X_multi, Y_multi, parameters_multi, X_pred_multi)

print(f"Ground living area, square feet:\n{X_pred_multi[0]}“)
print(f"Rates of the overall quality of material and finish, 1-10:\n{X_pred_multi[1]}”)
print(f"Predictions of sales price, $:\n{np.round(Y_pred_multi)}")

Result:


ValueError Traceback (most recent call last)
Cell In[50], line 2
1 X_pred_multi = np.array([[1710, 7], [1200, 6], [2200, 8]]).T
----> 2 Y_pred_multi = predict(X_multi, Y_multi, parameters_multi, X_pred_multi)
4 print(f"Ground living area, square feet:\n{X_pred_multi[0]}“)
5 print(f"Rates of the overall quality of material and finish, 1-10:\n{X_pred_multi[1]}”)

Cell In[42], line 13, in predict(X, Y, parameters, X_pred)
11 X_pred_norm = ((X_pred - X_mean)/X_std).reshape((1, len(X_pred)))
12 else:
—> 13 X_mean = np.array(np.mean(X)).reshape((len(X.axes[1]),1))
14 X_std = np.array(np.std(X)).reshape((len(X.axes[1]),1))
15 X_pred_norm = ((X_pred - X_mean)/X_std)

ValueError: cannot reshape array of size 1 into shape (2,1)

1 Like

Hello @prepan

Kindly remove codes which will grade your assignment. It is against community guidelines to post your codes on public post thread.

Also you have mentioned header as C3=== which means probability and statistics for machine learning and Data science, but you have selected Course 2 in the categories as Calculus for machine learning and data science. That is mentioning a proper header with the right category of course and week selection is a must for better response.

Kindly confirm the above, so we can address your issue in hand.

Regards
DP

Sorry for the typo. This should be C2.

These two are the first errors I met. They are the last two cells. Only them made mistakes.

Also, this is not an assignment. This is a lab session and all code are directly from the class, not me.

1 Like

Also, I am not really sure about what you mean by saying

"Can you break your post into step wise, seems like you got two errors leading to your error relations training model. Also confirm that you are working on a latest or fresh copy of assignment as the course was recently updated.

To confirm you can cross check with the exercises your did, parse grader cell headers."

I apologise for my inability of understanding you.

1 Like

Can you mention the lab name? Is it ungraded lab? Graded assignment lab? From the Coursera page.

The ungraded lab from course 2 week 1 mentions name as C2_W1_Lab_1_differentiation_in_python.ipynb

kindly confirm if you are referring to the same lab??

I am so so sorry… It should be C2 W3 Lab 1 Regression with Perceptron. I don’t know why would I typed it wrong… maybe I am not so sober now. I apologise.

2 Likes

I surely would advise against it to do any coding as you can see the results.

Ok now comes to your issue, are you running the lab in coursera environment or in your local Jupyter notebook??

If in you did assignment in Coursera environment, then can you try once restarting the kernel, then running each cell one by one.

Let me know if you still have the same error.

Regards
DP

I am running in my local Jupyter notebook. I will test the Coursera environment later, but I can confirm neither local Jupyter or colab worked. Thanks for you assistance.

1 Like

When we try to run labs that were coursera environment dependent, one needs to understand you need to download all the necessary files to run the assignments, and sometimes even downloading all files still can encounter error due to some of module or libraries missing in your local computer. So first make sure you have all the requirement to run the lab you are running in your local Jupiter notebook.

Regards
DP

1 Like

I recommend you not attempt to run the labs locally until after you have successfully completed the assignment (and received a grade of 100%) using the Coursera Labs platform.

There are lots of potential issues with downloading the assignments and installing compatible versions of all of the tools and packages.

The labs are not set up to use the latest versions of the Python packages and tools.

Best to tackle one problem at a time. Get the lab running on Coursera Labs first, then you can try moving it to other platforms.

1 Like

FYI

This is indeed due to a different behavior of numpy/pandas between the online versions and newer versions of the libraries that you might have locally. The easy fix is to stick to working with the online notebooks.

I noticed the cells responsible for normalization was not doing what was expected, y axis gives negative values.

Can be fixed by replacing

adv_norm = (adv - np.mean(adv))/np.std(adv)

with

adv_norm = (adv - adv.mean())/adv.std()

Works on both coursera and locally.

Cheers

2 Likes

@lucas.coutinho , could you look into this while @Mubsi is out?

1 Like

Thanks for flagging this, and thanks for tagging me @TMosh, I’m actually the LT for M4ML :grin:.

As far as I can see, the issue happens due to different pandas/numpy versions in local and coursera environment, right?

This may happen as the library versions in the Coursera environment are predefined so, as the time passes, it will get outdated and there may be some divergences in function outputs or function calls. This is why we recommend learners to use Coursera environment instead of local.

If you still want to run it in your local machine, I would recommend to run in a code block something like

!pip list | grep pandas
!pip list | grep numpy

and any other library used in the assignment, so you can just use the same version and avoid such issues.

Cheers,
Lucas

2 Likes