Week 2 Exercise 8 reshape array

Hi all,

I’m sure this is going to be another silly outlook on my part. But I really can’t figure out why I’m getting this error: ValueError: cannot reshape array of size 2 into shape (4,1)

<< code deleted >>

Hi @gweeish , you seem to have forgotten to implement the gradient descent (the optimize function that you implemented in exercise 6). Have a good look at that. Hope this helps.

Hi @sjfischer

Thank you for the hint. I’ve implemented it as so:


dim = X_train.shape[0]
w,b = initialize_with_zeros(dim)

params, grads, cost = optimize(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)

w = params["w"]
b = params["b"]

And got the other error of 'ValueError: operands could not be broadcast together with shapes (7,3) (1,7) ’

Hi @gweeish , you’re almost there. Have a good look at the input parameters of the optimize function (exercise 6)… Now what do you need to fill in for the first 4 parameters?

Hi @sjfischer

I’m so sorry for pestering you…

Have since changed it to “params, grads, cost = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)” but now getting the following error: AssertionError: Wrong values for d[‘costs’]. [array(0.15900538)] != [array(0.69314718)]

Oh dearie me…

Hi @gweeish , mmmh :thinking: just wondering whether it is better that you find out yourself or whether to give a hint… as it is important you understand the how the different initialization, optimize and propagation functions work.

But then again, I think you understand the functions right now looking at your posts. There is a typo somewhere that gives the wrong value. Now what typo would cause d[‘costs’] to go wrong?

@sjfischer no no… letting me know there’s a typo was all i needed. holy smokey doodles… THANK YOU!!!

Great, you found it! And no worries. This is a very common error made, myself included (I noticed it because I have seen it occurring many times in the course). It is something where you typically can look at for hours and don’t see it…

Good luck with the rest of the exercises, and enjoy the rest of the course!

1 Like

hi @sjfischer

I’m going through all of the steps in the assignment again to better understand the code. May I ask the silliest question? From the first 2 exercises, it looks at the sizes of the matrices, returning (209, 64, 64, 3) for the shape of train_set_x. Since 64 is the height and width of the image (number of pixels), 209 is the number of training examples and 3 is the RGB channels, how does the neural network distinguish whether its a cat or a non-cat? Does the neural network go through the colour of each pixel (represented in the 64*64 *3) and look for similar patterns in which a cat is shaped to determine is a new picture is a cat or non-cat?

Thank you! (so sorry if its a silly question)

Hi @gweeish , that’s not a silly question, actually it is a very good one. And the answer is: we do not know for sure. The neural network is a black box us. You give it a set of training data which you label as cat and non-cat, and based on the training data the neural network goes through the forward and backward propagation steps to create is prediction model. If it were a human being, it probably will look at things like its shape, whether it has four feet/paws, two ears, a certain color/color combination etc. But we cannot see from the hidden layers where it is looking at.

Compare that to logistic regression: there you would have some input parameters, and an output parameter. If the output parameter is a cat, you can revert back to the input variables and how much they “contribute” to the output variable. But for a neural network with layers, this almost impossible to determine.

Apologies for not giving a better answer.

Hi @sjfischer ,

Thank you for getting back to me. In this case, since we’re using logistic regression to determine if the image is cat and non-cat and there aren’t hidden layers, how does the colour of each pixel link to the output?

And no no. You’re helping me understand this so much better. Thank you for your time! =)

Hi @gweeish that again will depend on the training set. After the model is generated you need to analyze the input variables, which can be quite a lot of work.

Imagine a training set in which the cat pictures are all black or brown cats, and the non-cat items are of various colors, most likely color will play a role.

Now imagine a training set with all pictures (cats and non-cats) in black and white. The most likely, color will not play a large role and the important input variables might be things like shape, etc.

Hi @sjfischer ,

That is very fair indeed. I understand now.

Thank you! =)

Hi @gweeish , note that there is work/research being done in the field of interpreting deep learning models, as your question is very much relevant in business settings as well (e.g. imagine financial predictions within a bank that need to be explainable for regulatory reasons, or predictions that are relevant in medical/psychiatric environments). You can google and find a lot of information on this.

For example:
Interpretability of Deep Learning Models | by Eduardo Perez Denadai | Towards Data Science

Frontiers | Illuminating the Black Box: Interpreting Deep Neural Network Models for Psychiatric Research | Psychiatry (frontiersin.org)

1 Like

Dear Gweeish,

That’s very appropriate for a learner to ask this very question and the possibility of the answer is yes, no one really knows how and that’s why we use different sets of parameters and large datasets to train the machine to recognise the actual figure or face or the object. By providing the essential parameters, for instance, in the case of recognition of the picture of a cat, we use parameters like two raised ears near the frontal head, prominent moustache like hairs near the mouth, sharp small eyes, rounded face etc etc. Thus a machine learns with larger datasets through different parameters.


Hi Rashmi,

Thank you for your answer. I suppose my question was how the process goes about recognising the parameters (like how it recognises that the two pointy things are ears) with the input that we’ve given it.

Hi @sjfischer

This is really really neat stuff! Will have to research more about it.

Thank you! =)