I am current following a tutorial in Pytorch and there is this expression:
grad_h[h < 0] = 0
How does this syntax work and what does it do?
I am current following a tutorial in Pytorch and there is this expression:
grad_h[h < 0] = 0
How does this syntax work and what does it do?
It means replace with zeros all the values in grad_h where its corresponding h is negative.
So it is implementing some kind of mask, to keep the gradient values only when h is negative
suppose that grad_h and h have the same shape.
grad_h.shape == h.shape
when you do h < 0 you obtain an array of booleans of the same shape that is set to True if h[i] < 0 for each i.
So then you apply this mask to do slicing on grad_h and finally you set all the sliced elements to zero
It means that the variable grad_h is equal to 0 as long as h is less than 0.
grad_his dict andh = 1is int then:grad_h[h < 0] = 0will result ingrad_h = {False: 0}grad_his a NumPy array.