-1

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?

deceze
  • 510,633
  • 85
  • 743
  • 889
  • before this q is shut, it is assigning all values in the grad_h array which are less than zero to zero – chris Aug 21 '18 at 07:07
  • if ```grad_h``` is dict and ```h = 1``` is int then: ```grad_h[h < 0] = 0``` will result in ```grad_h = {False: 0}``` – dorintufar Aug 21 '18 at 07:07
  • This works because `grad_h` is a NumPy array. – fractals Aug 21 '18 at 07:08
  • 1
    This is almost certainly a duplicate of an existing question with a good answer; we just need to find it for you. – abarnert Aug 21 '18 at 07:09
  • Meanwhile, here's [the relevant section in the numpy user guide](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing), which explains things pretty nicely. – abarnert Aug 21 '18 at 07:12
  • There are probably closer duplicates (I'm still looking…), but jonrsharpe's answer to that one does a great job explaining the relevant features here. – abarnert Aug 21 '18 at 07:15
  • Meanwhile, this isn't "attribute syntax". Attributes are things you access with `.`, like `self.x` or `np.sum` or `grad_h.shape`. This is _indexing_ syntax. Indexes are things you access with brackets, like `grad_h[0]` or `grad_h[0, 2]` or `grad_h[0:2, ..., 3]` or `grad_h[h < 0]`. (Technically, it's _subscripting_ syntax, which is used both for indexing and for key lookup, but nobody calls it that unless they're talking about implementing `__getitem__` methods or how the parser works…) – abarnert Aug 21 '18 at 07:20

2 Answers2

0

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

Gabriel M
  • 1,486
  • 4
  • 17
  • 25
-2

It means that the variable grad_h is equal to 0 as long as h is less than 0.

Sleepless
  • 1
  • 1