One liner tensor with conditions
I have two tensors:
a = torch.nn.Parameter(torch.rand(7, requires_grad=True))
b = torch.randint(0,60, (20,))

Is there a one liner (or a quick &amp; short way) that can create a tensor (call it x) of size 20 (similar to &quot;b&quot;) with conditions?
i.e.
[b&lt;4 use a[0], 4 &lt;=b&lt;12 use a[1], 12&lt;=b&lt;22 use a[2], &lt;28, &lt;38, &lt;50, &gt;50] for every b

So if:
b = [12, 93, 54, 0...]

I want my new tensor &quot;x&quot; to be:
x = [a[2],a[6], a[6]...]

I'm going to use this &quot;x&quot; tensor to train and need the values the be backproped and learnable
i.e.
loss = torch.rand(20) * x

loss.backward() ...

So if one of the a's is not in x I want it to not change.

You can sum multiplicative masks of the conditions:
x = a[0]*(b&lt;4) + a[1]*((4&lt;=b)&amp;(b&lt;12)) + a[2]*((12&lt;=b)&amp;(b&lt;22)) + a[3]*((22&lt;=b)&amp;(b&lt;28)) + a[4]*((28&lt;=b)&amp;(b&lt;30)) + a[5]*((30&lt;=b)&amp;(b&lt;50)) + a[6]*(b&gt;=50)

