Wk2 A2 Merge into Model Global Variable error

I have passed through wk2 programming exercises 1-7 but am stuck on 8. Like another student I keep getting error Data Type Mismatch and Wrong Shape and Wrong Output. The advice says Check your equations and avoid using global variables. I can’t see that I’m using global variables, and the previous tests passed for all the functions that I’m using (initialize, optimize, etc) but I am new to python - can you point me to guidance on global variables so that I can see what is wrong?

Hi @wayneh,

Which course is this in (Course 1-5)?


It sounds like this is the Logistic Regression assignment in C1 W2. If you are getting the shape mismatch error on model_test it most likely means you are “hard coding” or making fixed assumptions about the dimensions of the input. One way to do that is to reference the global variable num_px in the call to initialize_with_zeros from model. The point is that we are writing general code here which should work for any input dimensions. The model for variable scoping in python is pretty standard with the function being the unit of local scope, which is to say there are no “blocks” as in some other languages. For our purposes here, you should only reference the parameters of the function or other variables that are defined within the scope of the function itself.

Thanks for reply. re the ‘initialise’ function, I actually used num_px on first attempt, when I got the error. I thought maybe the value 3 was the problem (… * 3). Then I changed to hard code value - same error. Then I tried shape[0] on the training set - same error. The only hard-coded values in my code are either 3 if I use the num_px or 0 if I use the shape approach. I initialise two variables within the model function, and call functions with arguments that are specified in the model arguments/red text (or a NumPy operator on one of those arguments). All of the functions were ‘pass all tests’ in the prior exercises, so I presume the code within the functions is correct. Do you have further thoughts?

Thanks for your reply. It’s the Logistic Regression in C1 Wk2 - please see correspondence with paulinpaloalto below.

The correct approach is to use the “shape[0]” attribute of the input sample matrix as the dim argument to initialize_with_zeros. Are you sure you used X_train? That’s the actual parameter to model, right? If you reference e.g. train_set_x, that is a global variable, which is a mistake and will fail model_test because it has completely different dimensions.

1 Like

Thanks again - I am using X_train.shape[0] - so I think that’s correct. I just said the training set in my message, it’s not in my code. I’m stumped. Could it be because I am doing this in Safari?? Or could it be because I have tried so many modifications that something is stuck and I should clear all attempts and start again - I realise that is something I should have done and I am only avoiding it because it takes so much time …

I am tempted to submit the assignment, take the hit on the grade and then ask for someone to actually look at my code. I will try with a clear head next week and if I still can’t get through, that will be my plan

It’s a fine idea to submit to the grader just to see what it says. You can submit as many times as you like and the system will remember your best score on each section, so there is never any reason to avoid “submit”. But please realize that is just a piece of software. Doing that does not expose your code to any human beings. Your browser should not matter. If you want to start again from a clean slate, there is a topic about that on the FAQ Thread.

ok thanks, I have a week to submit in any case, so I will move on and then come back to it on a fresh notebook

The other thing to note is that the deadlines in these courses are fake: there is no penalty for missing them. So no need to stress about that. Although taking longer may end up costing you more months’ worth of the subscription fee. Hmmm, I wonder if the fake deadlines are actually a business strategy on Coursera’s part? :grin:

hahaha yes! Also, as I’m an old dog just learning new tricks, the grades are more for ego (and to check my understanding) than for career support

Hi, I am having a same kind of issue. Although I have used X_train.shape[0], I am getting an error stating that the length of the list cost ([‘cost’]) is 20 where it should be 1. Therefore, I updated the if condition in the optimize function from i%100 to i%2000. After updating that, the error was fixed but another error occurred stating that there is a value mismatch.

@Kavindu99, it is a mistake to modify the print logic in the optimize function. You are just compensating for a mistake that you made someplace else. Most likely you are “hard-coding” the number of iterations on the call from model to optimize, instead of respecting the parameters that were passed into model.

1 Like