C1 W2 A2: Error in python code

ValueError 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)
123 y_test = np.array([[0, 1, 0]])
124
→ 125 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
126
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"

in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
36 # YOUR CODE STARTS HERE
37 w, b = np.zeros((X_train.shape[0],1)) , float(0)
—> 38 params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)
39 Y_prediction_test = Y_test.shape[1]
40 Y_prediction_train = Y_train.shape[1]

in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
35 # grads, cost = …
36 # YOUR CODE STARTS HERE
—> 37 grads, cost = propagate(w, b, X, Y)
38
39 # YOUR CODE ENDS HERE

in propagate(w, b, X, Y)
32 # YOUR CODE STARTS HERE
33
—> 34 A = sigmoid(np.dot(w.T,X)+b)
35 # cost = -np.sum(np.dot(Y.T,np.log(A))+np.dot((1-Y).T,np.log(1-A)))/m
36 cost = -1/m * np.sum( np.dot(np.log(A), Y.T) + np.dot(np.log(1-A), (1-Y.T)))
<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (1,4) and (2,3) not aligned: 4 (dim 1) != 2 (dim 0)

how to correct it??

Somewhere in the calls leading up to using the sigmoid() function, either your w or X variables are not the correct shape.

The issue could be in the sigmoid() function itself, or any of these functions:

  • initialize_with_zeros()
  • propagate()
  • optimize()

In addition to Tom’s points, also notice that the way you are calling optimize from model, the number of iterations will always be 100, regardless of what value is requested in the call to model. That will not end well.

Yes, the optimize() function accepts num_iterations as a parameter, and the value is defined as a parameter to model().

Don’t hard-code values unless you’re specifically asked to.

In addition to Tom and Paul’s response, I notice several bugs in your code.

This line, in your model function, gives the current error. You need to call initialize_with_zeros function.

However, if you correct this, you will get another error as there are several bugs in your code.

In your model function, the arguments are X_train and Y_train. What is X and Y?

Again in the model function, these two lines are incorrect. You have to call a prediction function.

Furthermore, as Paul and Tom already highlighted, do not hard-code anything.

Best,
Saif.