Dba = np.sum(dtanh,axis=1 or keepdims = True)

Hi friend and mentor,

In the Exercise 5 - rnn_cell_backward of Building_a_Recurrent_Neural_Network_Step_by_Step, the hint said very clear that " To calculate dba , the ‘batch’ above is a sum across all ‘m’ examples (axis= 1). Note that you should use the keepdims = True option." However, I got an error if I do that, my output will be just one number, not (5,1).

Well, if I do dba = np.sum(dtanh,axis=1 ), then i think i got the right answer and right dim as in the pictures below. Btw, my dtanh was (5,10)

Can someone verify this with me? maybe I am just lucky in this case?


Your dba is the wrong shape: they are expecting (5,1), but yours is (5,). See the difference? That means yours is a 1D array and not a 2D array. That is exactly the point of keepdims = 1.

If you got one number, that means you did not include the axis parameter. You need both, right? It’s not an “either or”. If you don’t include axis, then you’re saying you want the sum of all the elements in the array. But what we want here is the “columnwise” sum, so we need the axis = 1 to say that. Then we also want it preserved as a 2D array. If you don’t give the keepdims, then it simply eliminates the column dimension leaving you with a 1D array.

1 Like

This is the way “keyword” parameters work in python: you can have many of them and they are all optional. The declaration of the function defines the default values of all those keyword parameters if they are not supplied. Read the docpage for numpy sum to learn more.

thanks Paul for your reply.
I changed to dba = np.sum(dtanh, axis=1,keepdims = True) since I need them “both”. Now I have the results below.