C2W1_Assignment_2 - Solution to Forward Propagation with Dropout Confusing Grading

The Week 1 Programming Assignment 2 implementation for Forward Propagation with Dropout despite All tests pass and following the instructions, e.g.

D2 = (D2 < keep_prob).astype(int)

Fails when graded (see the screenshot below for the grading error), despite the instruction specifically saying:

Note that the X = (X < keep_prob).astype(int) works with multi-dimensional arrays, and the resulting output preserves the dimensions of the input array.

Also note that without using .astype(int), the result is an array of booleans True and False, which Python automatically converts to 1 and 0 if we multiply it with numbers. (However, it’s better practice to convert data into the data type that we intend, so try using .astype(int).)

However, the following passes the grading:

D2 = (D2 < keep_prob)

Therefore, either update the instructions or fix the grading.

Sorry, but I think you are misinterpreting the symptoms. E.g. perhaps you changed two things at the same time to see these results. I have written the code using both this:

D1 = (D1 < keep_prob).astype(float)

and this:

D1 = (D1 < keep_prob).astype(int)

and I get 100% from the grader both ways. It’s unclear to me why they would recommend casting to int, when we are dealing with floats here. How is int better than bool if the other operand is float? With int, you’re still leaning on numpy’s type coercion rules and I thought the whole point of the cast was not to do that.

I’ll file an enhancement request suggesting that they change the instructions to recommend casting to float, but I think there is still another mystery here to be solved. Notice that the error message from the grader discusses bool types, but you’ve removed the Booleans from that expression in the first instance (the one that fails the grader).

Sorry, I just realized that you posted this for the DLAI learning platform. My experiments above were conducted on Coursera. Perhaps the graders work differently between the two environments.

It will take me some time, but I will try the same experiment on the DLAI LP and report back. Stay tuned!

My apologies! You are right: running it on the DLAI LP, I see the exact same error from the grader that you show with either casting to int or float.

So the graders work differently between the two platforms. Oh, joy.

More investigation required and obviously something will need to get fixed. I’m still not understanding where the bool is coming from in the grader message. More thought required!

1 Like

Some analysis of possible causes of the problem. I am going to call DLAI LP, or DeepLearning.ai learning platform, as DLAI, just like we call Coursera Coursera.

This error is triggered in np.multiply when the output has to be placed in an existing boolean array. Normally, np.multiply creates a new array to store the output and such error won’t happen, but there are two exceptions: (1) in-place multiplication, (2) out argument stated. To reproduce the error, uncomment any of the commented lines below and run it.

import numpy as np
f = np.array([1., 2.]) # float
b = np.array([True, False]) # boolean
# b *= f
# np.multiply(f, b, out=b)

The assignment didn’t ask us to perform the like of any of the commented operations, and I believe none of us had, but I wondered if the DLAI’s grader did, so with D2 = D2.astype(int), I added a raise right before return and see whether np.multiply or my raise would trigger an error first, and it turned out to be the latter. Therefore, I conclude that D2 = D2.astype(int) did no harm to the function itself, as the instruction explained, and after several more tests, I think the story is that, the function will run and return successfully with astype(int) present, but then the grader will additionally take the value of D2 and perform some operations with it. My theory is that the DLAI’s grader expected D2 to be a boolean (which it wasn’t with .astype(int) present) and then one of the operations used “multiply” like the commented lines.

Cheers,
Raymond

2 Likes

For the time being, @alinposho, I think we can add .astype(int) not in the line that calculates D2 but in the line that calculates A2, preserving D2 as boolean array. We can do the same for D1 and A1.

Alternatively, we may skip .astype(int) as well because np.multiply handles type casting (see doc).

2 Likes

Thanks all for your explanations. Given that the issue reported in this topic seems to be related to the assignment grading logic, can you, please raise a ticket for that to be fixed?

1 Like

Yes, I have filed a git issue on this and we have an active discussion about it with the course staff. Thank you for reporting this!

2 Likes