Voted C3_W2_Collaborative_RecSys_Assignment - Regularization?

Hello Learners,

Public tests

from public_tests import *
test_cofi_cost_func(cofi_cost_func(X_r,W_r,b_r,Y_r,R_r,lambda_=0.15))

Result

TypeError Traceback (most recent call last)
in
1 # Public tests
2 from public_tests import *
----> 3 test_cofi_cost_func(cofi_cost_func(X_r,W_r,b_r,Y_r,R_r,lambda_=0.15))

~/work/public_tests.py in test_cofi_cost_func(target)
12 R_r = np.zeros((num_movies_r, num_users_r))
13
—> 14 J = target(X_r, W_r, b_r, Y_r, R_r, 2);
15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiply the regularization term by lambda_?"
16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"

TypeError: ‘numpy.float64’ object is not callable

How Should I do?

Did you restart the kernel and then run all of the cells in the notebook starting from the first cell?

I did it. but same result


TypeError Traceback (most recent call last)
in
1 # Public tests
2 from public_tests import *
----> 3 test_cofi_cost_func(cofi_cost_func(X_r,W_r,b_r,Y_r,R_r,lambda_=0.15))

~/work/public_tests.py in test_cofi_cost_func(target)
12 R_r = np.zeros((num_movies_r, num_users_r))
13
—> 14 J = target(X_r, W_r, b_r, Y_r, R_r, 2);
15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiply the regularization term by lambda_?"
16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"

TypeError: ‘numpy.float64’ object is not callable

I used lambed. Is it a proble?

Hello @Makoto_Onodera,

It seems that you have changed code that is not supposed to be changed. Please keep to the original version.

螢幕擷取畫面_20221127_180025

Raymond

PS: The original version passes a function to test_cofi_cost_func. Your version passes a number to it.

This is not solved. There is an error.
INPUT
#Public tests
from public_tests import *
test_cofi_cost_func(cofi_cost_func)

OUTPUT

AssertionError Traceback (most recent call last)
in
1 #Public tests
2 from public_tests import *
----> 3 test_cofi_cost_func(cofi_cost_func)

~/work/public_tests.py in test_cofi_cost_func(target)
14 J = target(X_r, W_r, b_r, Y_r, R_r, 2);
15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiply the regularization term by lambda_?"
—> 16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"
17
18

AssertionError: Wrong value. Expected 27, got 270.0. Check the regularization term

The original problem is gone and this is a new problem.

This new problem says that your function does not compute the answer correctly. Therefore something is wrong in your code. If you can’t figure out what’s wrong by reading your own code, then I suggest you to check out the test itself.

In your function, insert these lines right after the line ### START CODE HERE ###:

print('X is', X)
print('W is', W)
print('b is', b)
print('Y is', Y)
print('R is', R)
print('lambda_ is', lambda_)

Then run the test again to check out what inputs are supplied before the error is triggered. With the printed inputs, go to the equation to verify yourself it is indeed 27, and then, without running the test again, go through your code step-by-step by replacing variables with those inputs, do the computation yourself, and find out which line does not work as expected.

When writing a program, you are just asking the computer to do the calculation for you, but in principle, all the calculation can be done by you. So if you don’t instruct Python correctly, you can check the maths yourself to find out where the instruction is wrong.

Good luck,
Raymond

Public tests

from public_tests import *

print(‘X is’, X)

print(‘W is’, W)

print(‘b is’, b)

print(‘Y is’, Y)

print(‘R is’, R)

print(‘lambda_’, lambda_)

test_cofi_cost_func(cofi_cost_func)

Result

Hello please move the printing code to inside your cofi_cost_func, right after ### START CODE HERE ###, and then run the test?

Please hlep me

Public tests

from public_tests import *
test_cofi_cost_func(print(cofi_cost_func))

<function cofi_cost_func at 0x7fd41afed950>

TypeError Traceback (most recent call last)
in
1 # Public tests
2 from public_tests import *
----> 3 test_cofi_cost_func(print(cofi_cost_func))
4
5

~/work/public_tests.py in test_cofi_cost_func(target)
12 R_r = np.zeros((num_movies_r, num_users_r))
13
—> 14 J = target(X_r, W_r, b_r, Y_r, R_r, 2);
15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiply the regularization term by lambda_?"
16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"

TypeError: ‘NoneType’ object is not callable

Public tests

from public_tests import *
print(‘X is’, X)
print(‘W is’, W)
print(‘b is’, b)
print(‘Y is’, Y)
print(‘R is’, R)
print(‘lambda_ is’, lambda_)
test_cofi_cost_func(cofi_cost_func)

X is [[-0.03328682 1.1667464 -0.5064895 … -0.349303 0.75385165
-0.20810875]
[ 0.40405777 2.2664917 -0.7428241 … -1.0381029 -2.3322656
-2.343191 ]
[ 0.02347802 0.42810223 -0.4263811 … -0.29378366 0.43972966
1.823422 ]

[-0.43808308 0.71181 -0.29844946 … 0.14841312 -0.439178
-0.06239507]
[ 1.1371753 0.8651963 1.0665106 … 0.61943984 0.37744495
-3.4115074 ]
[ 1.4329183 -1.5280124 1.0317537 … 0.8750148 0.28633314
0.46209347]]
W is [[ 0.47935075 0.4766997 0.48794055 … -0.2383076 -0.36121953
-0.18090296]
[ 0.03604829 -0.38190877 0.2958575 … 0.4640792 0.45832908
-0.23635024]
[ 0.05561632 -0.05899394 -0.2452817 … 0.36375767 0.01846844
0.17488128]

[ 0.4368593 0.23995417 0.4454432 … -0.39979035 0.0808987
-0.03249109]
[-0.26202303 -0.3454402 0.17738229 … 0.37314343 -0.41614646
0.03673166]
[ 0.37955832 0.09163761 -0.334498 … -0.14511484 0.12085658
0.09062105]]
b is [[ 0.23609531 0.35653394 0.12819964 0.3155024 0.23636407 -0.22917777
-0.14401537 0.07572716 -0.37003124 -0.02135438 -0.01885897 -0.01649374
0.069341 -0.4371599 -0.24480343 0.29720962 -0.3823989 0.37802482
0.44692165 -0.02464914 -0.499193 -0.4379481 -0.27089447 -0.23521835
0.35863072 -0.33172983 -0.319295 0.33025116 0.06182003 0.1360473
-0.16637295 -0.07102251 0.05386877 -0.34516948 -0.07712108 -0.43622762
0.34539694 -0.00874168 0.3759488 0.2825104 -0.14146823 -0.4804005
-0.40585357 0.2456305 -0.49492687 0.10903448 0.1771251 0.05603629
0.1348601 -0.25175112 0.11369991 0.48754352 0.24302799 0.20360255
0.0660556 0.13253677 0.09305513 0.2777846 0.02371693 0.10299754
-0.43873072 0.3180564 0.11554575 -0.45647788 -0.1157915 -0.20613712
-0.3222394 0.0670712 0.1316781 -0.04797685 0.16459042 0.265431
-0.44394213 -0.11467117 0.42816055 0.11962229 -0.18784195 0.23876792
0.14463365 -0.05417395 -0.08465075 -0.02212149 -0.01699716 -0.478563
0.19013703 0.14574134 -0.36160725 0.12130946 0.28853047 -0.02977687
-0.06522834 -0.4273659 -0.0752824 -0.15572357 0.01109242 0.3204555
0.24933082 0.11092675 0.09916294 0.06672055 -0.24027056 -0.15710282
-0.17818177 -0.36245525 -0.11369526 0.24532175 0.15722138 0.03113776
-0.27763367 0.16367197 -0.28439033 0.46277177 0.29739505 0.27828717
0.01912695 0.11723554 -0.22481555 -0.30280328 0.34909534 -0.24122453
-0.16898203 -0.14903909 0.26694208 0.42356133 0.34913808 -0.03734243
0.10106343 -0.17388654 -0.49831563 0.07820094 -0.15342546 -0.36590004
0.1406846 -0.47188687 0.17444587 -0.09562033 -0.35815257 0.35699356
-0.2095989 0.02738106 -0.38910145 0.11574459 -0.14782763 -0.39063984
0.4066319 -0.32036752 -0.1109702 0.39456856 0.37689888 0.39321297
0.3459376 0.00656825 -0.11964077 0.49896598 -0.24059612 0.10720742
0.22555989 -0.28566742 -0.3495056 -0.4036088 -0.14572406 0.36242604
-0.27781355 -0.35440212 0.02017504 -0.01487249 -0.45937228 -0.46679926
-0.24539918 0.250834 0.48240495 -0.12271291 -0.18530989 0.4877388
-0.42409772 -0.17774671 -0.23274487 -0.20638466 0.10630572 -0.23506796
0.41716546 -0.48773384 0.34115142 -0.19853532 0.13483697 0.48814183
0.365838 0.167292 0.26261747 0.04888517 -0.05316687 -0.09033108
0.00432897 -0.07519597 -0.00745195 -0.18805629 0.08991516 -0.07414108
0.01185024 0.00321782 -0.13788843 -0.15182257 0.23794007 -0.44536138
-0.4325565 0.04964775 -0.39575815 0.13207555 0.46450144 0.48699808
0.0982306 -0.24818414 -0.43989497 -0.17733443 0.44720662 -0.4877941
0.32816106 0.1520955 -0.36473483 0.41389132 0.38561857 0.3391484
0.24316305 -0.34090698 0.18459034 0.22107542 0.46749824 0.24527848
-0.01508802 -0.19061911 0.39714372 0.17129338 -0.4565462 0.29424638
0.11227339 0.02905083 0.24344242 0.07746142 0.09415382 0.32288378
-0.12956685 -0.4886583 -0.28763622 -0.13127446 0.06799567 0.19962704
0.30296177 0.36935085 -0.09190476 0.26250762 0.40036052 -0.2743777
-0.49375767 -0.06962425 -0.29795706 0.05135191 0.02573103 0.3185677
0.1474306 0.29947352 0.07307166 0.19167346 -0.10398293 0.18090385
0.30783385 0.13168758 -0.28889263 -0.2867632 -0.14057803 -0.4823867
0.03124899 -0.3843631 -0.15378278 -0.2224276 0.11713392 -0.17432636
-0.4389227 -0.1263656 -0.19403148 0.07353908 -0.4833243 0.4543454
0.32979268 0.02035397 0.43283933 0.32527155 0.12116545 -0.19067067
-0.31272715 0.2764269 -0.1997847 -0.49005002 -0.13874072 -0.24505162
0.4553625 0.4179442 -0.04273564 -0.08790296 0.04113734 0.41393095
0.15213323 -0.09905428 0.13102758 0.12146121 0.1455949 -0.4549449
0.3422228 0.19875216 0.33838552 -0.31526864 0.15791327 -0.04885948
0.38249975 -0.2259838 0.4335224 0.09115928 0.1873228 -0.4408388
0.13642609 0.2911085 -0.4142604 0.36956507 0.17642796 -0.05092746
0.4400813 0.2210195 0.19256228 0.40831757 0.2089485 -0.2952068
-0.46408284 -0.02400661 0.09706116 0.30689007 -0.20083755 -0.06569707
-0.00790119 0.33029783 -0.04697472 -0.301004 -0.03436923 -0.18052393
0.205028 0.17713094 -0.2915638 0.13628459 0.18889505 -0.10857934
-0.10574538 -0.30238467 0.27625644 0.36994237 0.3764261 0.2511189
-0.30040634 0.17628974 -0.41685903 -0.10970008 -0.18728197 0.28934503
0.49773997 0.08808029 -0.11087114 0.03448701 -0.27818608 0.34542423
-0.37529272 -0.2853775 -0.26411527 0.41159993 -0.1375072 0.4988646
0.19680327 0.4818679 0.15024763 0.12502313 0.25538224 0.3805648
0.27491546 0.15169483 -0.30213797 0.11057246 0.37918144 -0.09437716
0.13442796 -0.01982748 -0.43407947 0.26273787 0.22879243 0.26459223
0.4303277 0.25444216 0.4908933 0.41282868 -0.0973509 -0.39770204
-0.2710728 0.3187216 0.49455476 -0.33418065 -0.22715044 -0.11189991
-0.37946326 0.17442083 0.1970753 0.3409006 0.27558124 -0.20686197
0.3802954 0.01373512 -0.1563006 0.48745012 -0.03233349 0.11390352
-0.40698427 -0.21574324 0.27934504 0.05980569 0.3466866 0.07286084
0.35323143 -0.2696011 0.29613405 0.09769893 0.24693 0.26872206
-0.41006708 -0.04926592 -0.44144148 -0.41583318 0.15290171 0.06859624
0.10710317 -0.17657506 -0.47332853 -0.36171782 0.08478701 0.42955917
-0.40508574 0.45839047 0.4126075 -0.36223966 -0.3201397 ]]
Y is [[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]

[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]]
R is [[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]

[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]]

NameError Traceback (most recent call last)
in
12 print(‘R is’, R)
13
—> 14 print(‘lambda_’, lambda_)
15
16 test_cofi_cost_func(cofi_cost_func)

NameError: name ‘lambda_’ is not defined

I do not know Why Y and R are 0??

First, change

Public tests
from public_tests import *

print(‘X is’, X)

print(‘W is’, W)

print(‘b is’, b)

print(‘Y is’, Y)

print(‘R is’, R)

print(‘lambda_’, lambda_)

test_cofi_cost_func(cofi_cost_func)

to

Public tests
from public_tests import *

test_cofi_cost_func(cofi_cost_func)

≈=================================

Then go to your exercise code, find ### START CODE HERE ###. Please let me know if you can find it.

File “”, line 22
print(‘X is’, X)
^
SyntaxError: invalid character in identifier

File “”, line 22
print(‘X is’, X)
^
SyntaxError: invalid character in identifier

I have no idea why it doesn’t work for me.
Is everyone else able to do it?

Is this no proble?Correct?
#Public tests
from public_tests import *
test_cofi_cost_func(cofi_cost_func)

It is perfect!

Raymond

Could you please give me some more specific advice?
I don’t know what is wrong.

Please add only


print('X is', X)

print('W is', W)

print('b is', b)

print('Y is', Y)

print('R is', R)

print('lambda_', lambda_)

after the line ### START CODE HERE ###

There is no need to add the following.

from public_tests import *

test_cofi_cost_func(cofi_cost_func)

Result