W2 A2 - Logistic Regression Assignment - Exercise 8 - Model

Hello!
I am getting this error:

I understood that the previously defined propagate function only returns grads and costs. I believe that I shouldn’t touch this function already approved in exercise 5.
So how to apply the gradient descent in a single line, as suggested in the exercise (“params, grads, costs = …”)?
What I’m not understanding?

You are correct, propagate() should only return grads and cost.

The propagate() function doesn’t do gradient descent. So it doesn’t need to return the parameters.

Gradient descent is the job of the optimize() function.

1 Like

I managed to assign the optimize function to the variables params, grads and costs, but I was unsure:

  • whether the function parameters should be related to the training set or the test set. I think it’s training, right?
  • the other parameters, I was unsure whether they should follow those of the original function (num_iterations=100, learning_rate=0.009) or the “model” function we are working on (num_iterations=2000, learning_rate=0.5).

I tried both sets with both learning rates but I always get the error: “AssertionError: Wrong values for d[‘w’].”

What am I doing wrong?

Two points:

  1. You have to use the initialize_with_zeros function to initialize the parameters.
  2. You don’t need to set the num_iterations and learning_rate to any value. It should be general, so, any value could work with them.

Please share your full error if you still need any assistance…

This is given to you, self explanatory.

# Predict test/train set examples (≈ 2 lines of code)
Y_prediction_test = ...
Y_prediction_train = ...

I was rewriting the initialize_with_zeros function again (“w, b = np.zeros((X_train.shape[0],1)), 0”, but thanks for the tip! Now I parameterized the function with X_train.shape[0].

My optimize function now is defined as “(w, b, X_train, Y_train)”.

And now I get this:

I believe you quote another part of the exercise:
" # Predict test/train set examples (≈ 2 lines of code)
# Y_prediction_test = …
# Y_prediction_train = …"

My difficulty is in this section:
" #(≈ 1 line of code)
# Gradient descent
# params, grads, costs = …"
where, as we are seeing, I need the optimize function, which asks for data (X) and labels (Y) sets, right?

This code is incomplete. You have to pass the num_iterations and learning_rate as well but don’t set them to any numeric value.

Said another way, pass the variable names, but don’t assign any values to them. They already hold the correct values.

Got it! Added “num_iterations, learning_rate” to the function.

Now I’m getting:

I set Y_prediction_test = np.empty((1, m_test)) and printed the value of m_test (50).

Why does the exercise expect a shape (1,3)?

In Exercise 7 - predict, m is indeed 3. But here, shouldn’t it takes into account the number of examples in these sets (Y_test and Y_train)?

I don’t think so. And where did you put this code?

Firstly, please note that I corrected the post stating the correct code and error.

The assignment states:
“# Predict test/train set examples (≈ 2 lines of code)
# Y_prediction_test = …
# Y_prediction_train = …”

So I wrote what I did in the Exercise 7 - predict in the “YOUR CODE HERE” block in the Exercise 8 - model.

Exercise 7 creates the predict() function. Your code there should compute the A value, and then test it to set the Y_prediction value.

The code there that uses np.zeros(…) is only to create the initial Y_prediction variable with the appropriate size. That isn’t how you compute the predictions.

In Exercise 8 (the model() function), you should call the predict() function.

1 Like