Pytorch: Assign values from one mask to another, masked by itself
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping
--
Chapters
00:00 Question
01:21 Accepted answer (Score 4)
02:10 Thank you
--
Full question
https://stackoverflow.com/questions/7174...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pytorch
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping
--
Chapters
00:00 Question
01:21 Accepted answer (Score 4)
02:10 Thank you
--
Full question
https://stackoverflow.com/questions/7174...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pytorch
#avk47
ACCEPTED ANSWER
Score 5
There are a few solutions, I will also give their speed as measured by timeit, 10k repetitions, on 2021 macbook pro.
The simplest solution, taking 0.260s:
active[active.clone()] = ~terminated
We can use masked_scatter_ inplace operation for abt. 2x speedup (0.136s):
active.masked_scatter_(
active,
~terminated,
)
Out of place operation, taking 0.161s, would be:
active = torch.masked_scatter(
active,
active,
~terminated,
)