Week 2 Ex 8 Array shape issue

Can you advise what is wrong here please?
I am struggling with the error message that my “w” array shape is wrong. line 122
When I print out the w and X shapes at the point of failure they seem to match = to my untrained eye at least!

Sorry - missed off the error message on my snip.


I slipped a few extra print commands in at key points to investigate further
w.shape = (2, 1)
X.shape = (2, 3)
w.shape = (2, 1)
X.shape = (2, 3)
w.shape = (2, 1)
d(w).shape = (2, 1)
X.shape = (2, 3)

Hi, @Ian_Proffitt. If by “seem to match” you mean that the computation w^T X is valid for those dimensions, you are indeed correct. That multiplication will produce a 1 \times 3 array. Unfortunately, for this test case, you should be producing a w with shape (4, 1), not (2, 1).

I noticed that you added a couple of print statements to the model function to help you diagnose. That’s good. But where does the variable X come from? The input data argument to the model() function is X_train, not X.

The size of w is set under the “# initialize parameters with zeros” comment. You need to apply your initialize with zeros() function with the proper argument. Hint: it invokes X_train.

Thankyou Paul
I guess the X comes from me ‘over-thinking’ again. X_train failed in my first pass because it was the wrong shape - so I reassigned it and reshaped to an array called X and that seemed to work. at first glance.
However, back to the drawing board.

It was Ken who gave you the guidance there. It is possible to be consistently wrong: you probably used X instead of X_train in both places where you could have made that mistake. :nerd_face:

But rather than randomly trying different things, the better strategy is to understand the principles at play here. The idea is that all the functions we write need to be “self-contained”, which means they should never reference global variables. Within the body of the function, you should only reference variables defined in the “local scope” of the function. Where is the variable X defined? If the idea of “scope” is not familiar to you, you might want to put this course on pause and take an “intro to python” course. But note that python’s scoping model is absolutely flavor vanilla and would be recognizable if you’ve also learned any other of the common procedural languages (c, C++, Java, JavaScript, C# …).

Apology Ken, Paul. I didn’t look at the picture.
I am happy with python, what has confused me is the hidden layers of code we discussed in another thread.
Before the course started I had expected to be coding complete modules rather than inserting snippets - which make complete sense when you look at them as a whole but need some extra careful thought about their usage.
As you implied, there are many ways of skinning a cat in the software world and my thought processes, which lie in a hardware design background, tend to be fixed on absolutes rather than options.

The layers aren’t hidden: all the code is there for you to see. Anything you see called in the notebook that is not a numpy primitive is a subroutine imported from another local file. Just click “File → Open” and open the appropriate file. Start by reading the “import” cell which is the first executable cell in every notebook. All it takes is putting in the effort to read and understand what it is in front of you. The instructions are also quite complete and helpful.