@johnkyuan123 could you please check if you are hardcoding num_iterations
anywhere in your code? The costs.append(cost)
depends on the num_iterations
parameter.
Thanks for the reply. I just checked again and make sure I did not hard code num-ierations.
The costs
list filled in the optimize
function depends on two things:
- The number of iterations, you could add a print statement before the
for
loop:
costs = []
print('Iterations', num_iterations)
for i in range(num_iterations):
- The conditional which allows adding the cost to the list, there you could also add a print statement:
# Record the costs
if i % 100 == 0:
print('append cost', i)
Could you check the printout after adding those debug statements?
Iterations 100
append cost 0
append cost 1
append cost 2
append cost 3
append cost 4
append cost 5
append cost 6
append cost 7
append cost 8
append cost 9
append cost 10
append cost 11
append cost 12
append cost 13
append cost 14
append cost 15
append cost 16
append cost 17
append cost 18
append cost 19
append cost 20
append cost 21
append cost 22
append cost 23
append cost 24
append cost 25
append cost 26
append cost 27
append cost 28
append cost 29
append cost 30
append cost 31
append cost 32
append cost 33
append cost 34
append cost 35
append cost 36
append cost 37
append cost 38
append cost 39
append cost 40
append cost 41
append cost 42
append cost 43
append cost 44
append cost 45
append cost 46
append cost 47
append cost 48
append cost 49
append cost 50
append cost 51
append cost 52
append cost 53
append cost 54
append cost 55
append cost 56
append cost 57
append cost 58
append cost 59
append cost 60
append cost 61
append cost 62
append cost 63
append cost 64
append cost 65
append cost 66
append cost 67
append cost 68
append cost 69
append cost 70
append cost 71
append cost 72
append cost 73
append cost 74
append cost 75
append cost 76
append cost 77
append cost 78
append cost 79
append cost 80
append cost 81
append cost 82
append cost 83
append cost 84
append cost 85
append cost 86
append cost 87
append cost 88
append cost 89
append cost 90
append cost 91
append cost 92
append cost 93
append cost 94
append cost 95
append cost 96
append cost 97
append cost 98
append cost 99
w = [[0.19033591]
[0.12259159]]
b = 1.9253598300845747
dw = [[0.67752042]
[1.41625495]]
db = 0.21919450454067652
I think that you may have something wrong with the indentation of the code, I mean 1 % 100
is not equal to 0 so the statement append cost 1 should not appear above.
Is your code exactly like the one below? (the tabs are important)
# Record the costs
if i % 100 == 0:
print('append cost', i)
costs.append(cost)
# Print the cost every 100 training iterations
if print_cost:
print ("Cost after iteration %i: %f" %(i, cost))
As this part of the code was provided by Coursera you can copy / paste your code here to check it. Only this part, not the one you have completed.
Thanks. I got it working as you suggested by fixing indentation.Iterations 100
append cost 0
w = [[0.19033591]
[0.12259159]]
b = 1.9253598300845747
dw = [[0.67752042]
[1.41625495]]
db = 0.21919450454067652
Costs = [array(5.80154532)]
Iterations 101
append cost 0
append cost 100
All tests passed!
Hi I am stuck at the exercise 8 - model. I had a similar costs problem that I thought I fixed it before. The output Costs became an array of numbers instead of 1 as I call the function optimize within the model function definition, i. e.
params, grads, costs = optimize(w, b, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False)
Error Message = grads = {‘dw’: array([[-0.00184457],
[ 0.00074881],
[-0.00153935],
[-0.00062098]]), ‘db’: 0.0006242417520455953}
Costs = [array(0.69314718), array(0.1368631), array(0.07239687), array(0.04843874), array(0.03619413), array(0.02881856), array(0.02390811), array(0.02041124), array(0.01779774), array(0.01577211), array(0.01415702), array(0.01283968), array(0.01174505), array(0.01082127), array(0.01003139), array(0.00934836), array(0.00875194), array(0.0082267), array(0.00776065), array(0.00734434)]
AssertionError Traceback (most recent call last)
in
----> 1 model_test(model)
~/work/release/W2A2/public_tests.py in model_test(target)
112
113 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
→ 114 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
115 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for pred. {d[‘costs’]} != {expected_output[‘costs’]}"
116
AssertionError: Wrong length for d[‘costs’]. 20 != 1
I thought I fixed the Cost size problem with the optimize function last time:w = [[0.19033591]
[0.12259159]]
b = 1.9253598300845747
dw = [[0.67752042]
[1.41625495]]
db = 0.21919450454067652
Costs = [array(5.80154532)]
All tests passed!
Hi All,
I am doing the last part of the assignment, 5 - Merge all functions into a mode. All my previous codes are correct and the ‘dw’ I am calculating is correct too, but when I run the cell with ‘model_test(model)’ code in it, I get this error:
AssertionError: Wrong values for d[‘w’]. [[ 0.28154433]
[-0.11519574]
[ 0.13142694]
[ 0.20526551]] != [[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
I have check all my codes many times and I am not sure where is the problem, or how I can find where I am doing wrong.
Hi @johnkyuan123 , you are hardcoding the number of iterations and learning rate, you should be taking the inputs to the model
function.
@ali could you please check if you are doing something similar? The model
function should not use any hardcoded values.
Hi I’m stuck at the exercise-5, propagate() code, could you please help me out?
A=sigmoid(np.dot(w.T,X)+b)
cost=(1/m)*np.sum((-np.dot(Y,np.log(A).T)-np.dot(1-Y,np.log(1-A).T))
dw = 1/m*(np.dot(X,(A-Y).T))
db = (1/m)*np.sum(A-Y)
what am I doing wrong here?
Hello! Same problem that ali has. Same error message (with the same values) for Exercise 8 - model. For all previous exercises, got “All tests passed!”
I checked the for loops, they are okay. Also, I checked that I don’t have hard-coded values, I don’t.
Practically, for Exercise 8, there are 5 lines to enter. Based on the error message, these lines should be the error, but I believe they are correct
w = params[“w”]
b = params[“b”]
Any suggestion on how to find the problem that causes this
AssertionError: Wrong values for d[‘w’]. [[ 0.28154433]
[-0.11519574]
[ 0.13142694]
[ 0.20526551]] != [[ 0.00194946]
[-0.0005046 ]
[ 0.00083111]
[ 0.00143207]]
Thanks!
@Aryaman_rao what is the error code you are getting?
Hi @pneri, are you using all the parameters when calling the optimize
function? You should call that function with the model
parameters (no hardcoded values), all required inputs to the optimize
function are inputs to model
.
If after checking the above it is still failing you should trace back the problem: d["w"] -> params["w"] -> w -> optimize
I am facing the same problem. If you have solved it, then can you please help me.
Hi @Mohit21091, I don’t know if @johnkyuan123 solved the issue but there was an error in his code as he was hardcoding some of the inputs when calling the optimize
function. If you have the same problem then please don’t freeze the inputs in the call as he was doing, e.g. num_iterations = 2000 is wrong.
AssertionError Traceback (most recent call last)
in
----> 1 model_test(model)
~/work/release/W2A2/public_tests.py in model_test(target)
112
113 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
→ 114 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
115 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for pred. {d[‘costs’]} != {expected_output[‘costs’]}"
116
AssertionError: Wrong length for d[‘costs’]. 20 != 1
Please help me here.
Thank you!
Hi @Mohit21091, are you sure you are not hardcoding the number of iterations in the optimize
function?
The function model_test
is called with 50 iterations, therefore it is not possible that the list costs
has 20 items unless somewhere in your code you are hardcoding the number of iterations.
self correction, see post below. How to delete?
Had same error but found syntax error was result of error in cost function
Now I get to the end of exercise 4.6 but there is a syntax error in np.squeeze:
File “”, line 53
cost = np.squeeze(np.array(cost))
Maybe still a problem with code for cost? Here:
cost=(-np.dot(Y,np.log(A))-np.dot((1-Y),np.log(A)))/m
^
SyntaxError: invalid syntax
m = X.shape[1]
# rows 1, cols 0
# FORWARD PROPAGATION (FROM X TO COST)
#(≈ 2 lines of code)
# compute activation
# A = ...
# compute cost using np.dot. Don't use loops for the sum.
# cost = ...
# YOUR CODE STARTS HERE
A=sigmoid(np.dot(w.T,X))
ma= A.shape[1]
#grads,
cost=(-np.dot(Y,np.log(A))-np.dot((1-Y),np.log(1-A)))/m
mb=Y.shape[1]
# YOUR CODE ENDS HERE
# BACKWARD PROPAGATION (TO FIND GRAD)
#(≈ 2 lines of code)
# dw = ...
# db = ...
# YOUR CODE STARTS HERE
dw=(np.dot(X,(A-Y).T))/m
db=(np.sum((A-Y))/m
# YOUR CODE ENDS HERE
cost = np.squeeze(np.array(cost))
grads = {"dw": dw,
"db": db}
return grads, cost
Hi
This is what is in optimize function which is in Exercise 6
def optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False):
and this is what is in model function where i am calling the optimize function.
parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations=2000, learning_rate=0.5, print_cost=False)
Thank you for helping me.