Course 1 Week 2: Pgm Assignment Exercise 8

I am getting the following error when testing the model for Graded function: model in Exercise 8

I completed all the prior exercises leading up to exercise 8 (graded function: model). My coding segment is below. Can someone tell me what I am doing wrong?

{moderator edit - solution code removed}

1 Like

Hi, James.

Welcome to the course! Let’s start with a few general points, before we get to the details of your question.

You’ve created this thread under General Discussion, but you’ll generally have better luck getting answers if you file them under the relevant specialization and course, which in this case looks like it’s Deep Learning Specialization Course 1 (DLS - Course 1). I’ve moved it for you by using the little “edit pencil” on the title.

Also note that the rules are that we aren’t supposed to publish our solution code in the forums. The forums last forever and it “spoils the fun” for folks who come after by revealing the solutions. Normally we can help you just based on the exception trace that you get, as we could in this case. It’s fine to just “copy/paste” the output you get from running the failing test. If it includes a stack trace, it will reveal a little bit of code, but not the whole function.

It looks like you have “hard-coded” the dimension you are passing to the initialize routine to be 2. Hard-coding things is never a good idea. The point here is that we are always trying to write code that is “general”, meaning that it will work with any dimensions of inputs. So you’re saying that you believe that all datasets that will ever be passed to the model function will always have 2 features, but we know that images we are using for the real application here have 12288 features, right? So the idea is that you use the dimensions of the actual objects that are passed to model to determined things like the number of features (n_x) or the number of samples (m). So the best solution is to use the “shape” attribute of the variable X_train to figure out the dimensions. They gave some examples in the template code in the notebook of how to use the “shape” attribute of a numpy array. Have a look and let us know if that’s not enough info to get you there.

Also note that once you fix that error, you’ll get a different “dimension mismatch” error. In addition to the rule “don’t hard-code things”, another important rule to remember is “don’t reference global variables within the body of your function”. Where is the variable X defined?

Also note that no matter what value is passed into model as the learning rate, you’re always going to use 0.009. That will not end well.

Another good general principle to keep in mind is that a perfectly correct subroutine can throw errors if you pass it incorrect arguments. So the bugs we are discussing here are all in the top layer function model. When debugging, you always have to start at the point of the error and understand what it is telling you. But frequently the bug is not at that point in the code: it’s somewhere earlier and even someplace higher up the call stack. So you work backwards one step at a time to find the actual error.

Hello Paul,

Thank you for the “how to engage” corrections and the troubleshooting suggestions. Much appreciated!

James