Week 2, Assignment 2, dropout unvectorized code

Kindly explain this line of unvectorized code of dropout regularization given in the assignment.

for i,v in enumerate(x):

The full code is:

for i,v in enumerate(x):
    if v < keep_prob:
        x[i] = 1
    else: # v >= keep_prob
        x[i] = 0

They are just trying to give an alternate explanation for what this line of code does:

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

I think that line is way more clear and why they bother with showing you the loop is really the question. As they explain in the text, that loop code doesn’t even work in our real case here: it would only work if x is a 1D object. Everything we’re doing here is 2D. The loop logic should be pretty clear in its intent. The only thing about the code that requires any explanation is the enumerate() piece, which is just the usual hogwash of our pal Guido getting a sugar high from his “syntactic sugar”. If you wanted to be more clear about it, what would be so wrong with this:

for ii in range(size(x)):
    if x[ii] < keep_prob:
        x[ii] = 1
    else:
        x[ii] = 0

What we are doing here is computing a “mask” that we will elementwise multiply against the A^{[l]} activation output to perform the dropout operation.

Of course we assume that X is already a matrix of the same shape as A^{[l]} and contains values from the Uniform Distribution on (0,1).

One other fine point is that I would write the line this way:

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

but the type coercion rules make it all work even if you leave the values as Booleans.

Are you asking about the for loop itself?
The “enumerate” function is pretty useful. In this loop, it loops over the elements of x sequentially. At each iteration, ‘v’ is an entry from ‘x’, and ‘i’ is the index of that entry. Very useful.