# Wk 2, PA Log. Regr. w/ NN logistic_regression_model not working

Hi I’ve completed up and through the model definition in Exercise 8, creating and passing the model() definition exercise, inclurding the model_test(model). However when I try to run the next line, “logistic_regression_model = model(…” I get an mismatch error on operands, captured below. I didn’t add any new lines and I guess a previous function was created incorrectly and somehow still passed the check. Any advice?

ValueError Traceback (most recent call last)
in
----> 1 logistic_regression_model = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005, print_cost=True)

in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
26 # Gradient descent
27 # parameters, grads, costs = …
—> 28 params, grads, costs = optimize(w, b, X_train, Y_test, num_iterations, learning_rate, print_cost)
29 # Retrieve parameters w and b from dictionary “parameters”
30 # w = …

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)
31 # print("w is shape " + str(w.shape))
32 A = sigmoid(np.dot(w.T, X) + b)
—> 33 cost = -1 / m * np.sum( Y * np.log(A) + (1-Y) * np.log(1-A))
34 # YOUR CODE ENDS HERE
35

ValueError: operands could not be broadcast together with shapes (1,50) (1,209)

Hello @mast6580,

You can find your error by examining the shapes of A and Y inside the function propagate. Both should be of shape (1, 209) when running that test.

You can always restart the kernel and run all cells above again to see if it was just a matter of synchronization between your notebook and the kernel (code change not reflected in kernel yet).

Hello!

I have the same problem. Both have the form (1, 209) when running the test.

Can you post the error stack?

I guess you have an error in your `model` function, where you calculate `Y_prediction_train`. You are most likely using `X_test`, which contains 50 examples, instead of `X_train`, which has 209 examples.

``````Number of training examples: m_train = 209
Number of testing examples: m_test = 50
``````

If I swap the datasets, I get the same error:

``````Cost after iteration 1800: 0.146542
Cost after iteration 1900: 0.140872
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-b8051f6b0651> in <module>
----> 1 logistic_regression_model = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005, print_cost=True)

<ipython-input-17-4d64b275fb28> in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
45     # Print train/test Errors
46     if print_cost:
---> 47         print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
48         print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
49

ValueError: operands could not be broadcast together with shapes (1,50) (1,209)
``````

No, it isn’t, or I don’t understand your advice.

Check your code for

``````    # Predict test/train set examples (≈ 2 lines of code)
# Y_prediction_test = ...
# Y_prediction_train = ..
``````
1 Like

Thank you very much! I didn’t know what to do anymore )
Inattention (((

Did my advice make sense now? Did you solve the problem?

That’s great. Problem solved! Thank you

1 Like