W2 A2 | Ex 8 - Cannot reshape array of size 2 into shape (4,1)

@kenb
Hello,
Almost done, passed all tests till now, but I have no idea as to what to do with this error.
I am fairly new to programming (learning by doing), that’s why although I looked at other students’ questions with the same error, I didn’t understad the 'way to implement the answers. Hopefully you can help me nevertheless. Thanks!

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)
38
39 # YOUR CODE STARTS HERE
—> 40 Y_prediction_test = predict(w, b, X_test)
41 Y_prediction_train = predict(w, b, X_train)
42 # YOUR CODE ENDS HERE

in predict(w, b, X)
16 m = X.shape[1]
17 Y_prediction = np.zeros((1, m))
—> 18 w = w.reshape(X.shape[0], 1)
19
20 # Compute vector “A” predicting the probabilities of a cat being present in the picture

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

Hi @Doron_Modan ,

Have a look at this thread, you may find an answer to your problem.

Hi, I have read it already, but couldn’t apply this to my code. I’m doing print (w.shape) and get only (1,2) all the way back from predict, back to the first time w is mentioned, in the initialization.
I did w.shape both before each ‘def’ and after each ‘return’.
Is there any other specific place where I would have to print w.shape?

Is this is the clue that you are refering to in the thread you told me to look?
“The type of error you show typically means that you are referencing global variables someplace in your model code instead of the parameters that are actually passed in.”
I coudln’t find any global variable w, at least not one that I myself inserted. I wrote my code every time only inside the boundaries: 'Your code starts here" and “Your code ends here”. Maybe because I am new to programming, I don’t know exactly what a global variable looks like. As I read, this is a variable that is not part of a function, but I never wrote the variabe w unless asked to.
Still at a loss. :frowning:

I don’t know, maybe I found the culprit?
in model I wrote:
w, b = initialize_with_zeros(dim) ?
and we know that dim was set to 2
Did I make a mistake putting “dim” inside the brackets of initialize_with_zeros in fuction model? If So, what did I have to write there instead?

Hi @Doron_Modan ,

The dim that you used in the model() is incorrect, because this dim should be extracted from the training set passed as input parameter to the function model(). Bearing in mind that the model() function is a generic function, able to work with whatever values passed in as input parameters.

To get the dim from the X_train:
dim = X_train.shape[0], which is num_px * num_px * 3, the size of an image.

The model() function is used to classify cat or non-cat images. During training and testing, the model is fed with images, some are cats, some are not cat. So the dim is different from those used for the unit testing of initialize_with_zeros().

Thank you, this is useful for me to understand, but silll, writing
w, b = initialize_with_zeros(X_train.shape[0])
I get this error:

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)
22 w, b = initialize_with_zeros(X_train.shape[0])
23 # Gradient descent
—> 24 propagate(w, b, X, Y)
25 # params, grads, costs = …
26 params, grads, costs = optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=True)

in propagate(w, b, X, Y)
28 # cost = …
29 # YOUR CODE STARTS HERE
—> 30 A = sigmoid(np.dot(w.T, X) + b)
31 cost = - ((np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)))/m)
32 # YOUR CODE ENDS HERE

<array_function internals> in dot(*args, **kwargs)

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

Hi @Doron_Modan ,

It looks like the execution environment is picking up some values from previous tests. Could you refresh the kernel and rerun all the code cells:
from the Kernel tab, click restart and clear all outputs
from the Cell tab, click run all

Thank you, so I did that, and now I get:

Hi @Doron_Modan ,

I couldn’t see what you got, I hope everything is working well now.

Unfortunatey, no. After refreshing and rerunning all the code cells, I get this error: ValueError: shapes (1,4) and (2,3) not aligned: 4 (dim 1) != 2 (dim 0)
as follows:

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)
22 w, b = initialize_with_zeros(X_train.shape[0])
23 # Gradient descent
—> 24 propagate(w, b, X, Y)
25 # params, grads, costs = …
26 params, grads, costs = optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=True)

in propagate(w, b, X, Y)
28 # cost = …
29 # YOUR CODE STARTS HERE
—> 30 A = sigmoid(np.dot(w.T, X) + b)
31 cost = - ((np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)))/m)
32 # YOUR CODE ENDS HERE

<array_function internals> in dot(*args, **kwargs)

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

[quote=“Doron_Modan, post:6, topic:207732”]

Hi @Doron_Modan ,

Why are you passing X, & Y to optimize? Where do they come from?
Please check the input parameters of the model() function, only use the input parameters and passing them to the calling function. In this case, it should be X_train, and Y_train.

Thank you, indeed I’m learning now very basic things about programming, and this is one of them.
Still, I am getting the same error message:
model_test(model)

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)
22 w, b = initialize_with_zeros(X_train.shape[0])
23 # Gradient descent
—> 24 propagate(w, b, X, Y)
25 # params, grads, costs = …
26 params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost=True)

in propagate(w, b, X, Y)
28 # cost = …
29 # YOUR CODE STARTS HERE
—> 30 A = sigmoid(np.dot(w.T, X) + b)
31 cost = - ((np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)))/m)
32 # YOUR CODE ENDS HERE

<array_function internals> in dot(*args, **kwargs)

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

Hi @Doron_Modan ,

Here is the tips from the implementation instruction for the optimize():
Tips:
You basically need to write down two steps and iterate through them:
1) Calculate the cost and the gradient for the current parameters. Use propagate().
2) Update the parameters using gradient descent rule for w and b.
So why is your code calling propagate() before optimize()?
If you follow the implementation instructions for optimize(), propagate(), and passing the input parameters to the calling functions, then the correct parameters will get passed down. Remember, this assignment is dealing with images, so, the functions are there to support the processing of these images.
There are a lot of online resources on programming, you might find it useful to get some help from those resources.

1 Like

Thank you. It now works. I changed propagate(w, b, X, Y) that I blindly copied from propagate function above, into propagate(w, b, X_train, Y_train)
Still, I don’t understand your question: “So why is your code calling propagate() before optimize()?” What did you mean by that? After all, after the change that I reported everything works.

Hi @Doron_Modan ,

I was referring to the model() function you wrote. In that function, there is this line of code:
propagate(w, b, X, Y),
and then followed by :
params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost=True)

Unless you have a different version of the assignment, the cost and gradients are calculated within the optimize() function.