Hello, I am reaching almost the end of my W2 programming assignment. I got stuck on section of “Merge all functions into a model” in which I got an error of shape misalignment. It would be great if someone could help me take a look into my codes. Thanks in advance!!
We can’t directly see your code. The thing to do would be to post the error output that you are getting. Also note that you filed this question in the generic forum AI Questions, so we can’t really tell which course you are talking about. Please use the “edit pencil” on the title to move it to the Course Q&A category for the course you are taking.
That error says that your w value is the wrong shape. So where is that shape determined in the model
function? It is by the argument you pass to the initialize function, right?
Notice that once you fix that bug, you are not done. The way you are calling optimize
is “hard-coding” the values of the number of iterations and the learning rate. That means you are literally ignoring the values that are passed in to the model
function at the top level: no matter what value is requested, you will always run 100 iterations. That will not end well.
Thank you so much! The errors are resolved now. Have a great weekend!!
Can you please tell me what you changed in your code i can’t resolve it.
Please show us the error trace that you are getting.
Are you sure that your propagate
and optimize
functions pass their test cases? It looks like there are several problems somewhere in the chain. I added a print statement in my propagate
code to show the shapes and here’s what I see when I run that test cell for the model
function:
model with num_iterations 50 learning_rate 0.01
before optimize w.shape (4, 1)
optimize with num_iterations 50 learning_rate 0.01
propagate: X.shape (4, 7) A.shape (1, 7)
propagate: X.shape (4, 7) A.shape (1, 7)
....
propagate: X.shape (4, 7) A.shape (1, 7)
in model before predict call w.shape (4, 1)
predictions = [[1. 1. 0. 1. 0. 0. 1.]]
predictions = [[1. 1. 0.]]
All tests passed!
So both the shapes of your X and A values are wrong:
Your X is 4 x 1, which is the shape of w.
Your A is 7 x 7, but it should be a row vector.
Now your job is to figure out how that happened. My guess is there must be something wrong in your optimize
logic, which is why I asked whether you pass those tests.
{moderator edit - solution code removed}
These are the codes that I wrote under propagate and optimize functions and all test cases under them are passed but model function is showing error.
Ok, then that must mean that the error is in your model
logic. What is the shape of w before you call optimize
?
it is X_train.shape[0]
That is part of the shape. Print w.shape
before the call to optimize
like I did.
Oh, sorry, you can see the bug in your first trace: look at the function definition of optimize
again. A perfectly correct function will throw errors if you call it with incorrect arguments.
Yeah,it was the problem i didn’t notice it . Thank you so much for your time.
Hi,
I’m getting the same error, but all of my helper functions are still being passed. I think there is an issue within my model() function but I’m not sure how to resolve it:
In propagate(), do not reshape Y.
Instead, you should transpose A when you compute the cost. Use the .T operator.
Send me your code of this function ( Exercise 5 - propagate) in a private message. Click my name and message.
The problem is that your w is the wrong shape. The mistake is in how you are calling initialize_with_zeros
from model
. The bug is not in propagate
: a perfectly correct function can still throw errors if you call it with incorrect arguments.
Once you solve the problem with initialize, you will get a different error. Your call from model
to optimize
will result in using the default values for the learning rate and number of iterations that are included in the definition of the optimize
function. That means you are ignoring the values of those parameters that are passed in to the model
function by the test cases.
Also note that there is no need to call propagate
directly from model
: it is called from optimize
, right?
Note that I did not have some magic way of seeing your code: the bugs that I just mentioned are visible in the exception traces that you show above.