How to mask out (with -1) all values between two specific values

username: fbrah
cooked: <p>Hi,<br>
I have a tensor of dimension (batch_size, Seq_len)<br>
I want to mask out all values between two specific values of seq_len, for example 100, and 125. (including 100 and 125). There might be multiple such occurrences of them too.<br>
I want to mask out with ‘-1’<br>
how can I do it in a fast way in pytorch?</p>

username: KFrank
cooked: <p>Hi fbrah!</p>
<aside class="quote no-group" data-username="fbrah" data-post="1" data-topic="59335" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fbrah/48/12933_2.png" class="avatar"> fbrah:</div>
<blockquote>
<p>I have a tensor of dimension (batch_size, Seq_len)<br>
I want to mask out all values between two specific values of seq_len,</p>
</blockquote>
</aside>
<p>I’m confused about what you are asking.  “Seq_len” is the second<br>
dimension of your tensor, but then you have “two specific values of<br>
seq_len.”  I don’t understand what you mean.  Is “seq_len” an index<br>
into your tensor, or is it the value of an element of your tensor?</p>
<aside class="quote no-group">
<blockquote>
<p>for example 100, and 125. (including 100 and 125). There might be multiple such occurrences of them too.<br>
I want to mask out with ‘-1’</p>
</blockquote>
</aside>
<p>If “seq_len” is an index, then you can use <em>slice notation</em>.  That is,<br>
if <code>t</code> is your tensor, then:</p>
<pre data-code-wrap="python"><code class="lang-python">t[:, 100:126] = -1.0
</code></pre>
<p>will overwrite the elements of your tensor whose second index is in<br>
the range [100, 125] (inclusive) with -1.0.</p>
<p>If “seq_len” is a value, then if you want to replace with -1 the elements<br>
of your tensor whose values fall in a given range, <code>masked_fill_()</code><br>
should do what you want.  That is, if <code>t</code> is your tensor, then:</p>
<pre data-code-wrap="python"><code class="lang-python">t.masked_fill_ (t.ge (100.0) * t.le (125.0), -1.0)
</code></pre>
<p>will overwrite the elements of your tensor whose value fall in the<br>
range [100.0, 125.0] (inclusive) with -1.0.</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: fbrah
cooked: <p>Hi <a class="mention" href="/u/kfrank">@KFrank</a><br>
Thanks for your reply and thorough explanation.<br>
However, values in the seq_len dimension are some integers which are basically word_indices.<br>
let me clarify what I want to do:<br>
Lets say seq_len = 128<br>
consider X = myTensor[0,:].  # this would be of size [1, 128].</p>
<p>for example: X = [4,121,4,501,100, …,125, 90,92, 100, …,125]</p>
<p>I want all values between (including) 100 and 125 which I showed with …, set to -1. And I want to do that for all rows of myTensor (<span class="hashtag">#row</span> = batch_size)</p>
<p>I hope I could clarify this. Thanks again<br>
Is there anyway I can use <code>masked_fill_()</code> for this but not with respect to actual values?</p>

username: KFrank
cooked: <p>Hello fbrah!</p>
<aside class="quote no-group" data-username="fbrah" data-post="3" data-topic="59335" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fbrah/48/12933_2.png" class="avatar"> fbrah:</div>
<blockquote>
<p>Lets say seq_len = 128<br>
consider X = myTensor[0,:].  # this would be of size [1, 128].</p>
<p>for example: X = [4,121,4,501,100, …,125, 90,92, 100, …,125]</p>
<p>I want all values between (including) 100 and 125 which I showed with …, set to -1. And I want to do that for all rows of myTensor (<span class="hashtag-raw">#row</span> = batch_size)</p>
<p>I hope I could clarify this. Thanks again<br>
Is there anyway I can use <code>masked_fill_()</code> for this but not with respect to actual values?</p>
</blockquote>
</aside>
<p>Based on what I think you want, the <code>masked_fill_()</code> code I posted<br>
should work for you.</p>
<p>Could you post a short, self-contained script that uses <code>masked_fill_()</code><br>
as I described, together with its output (including errors, if any), and<br>
compare, concretely, what <code>masked_fill_()</code> gives you with what you<br>
actually want?</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: fbrah
cooked: <aside class="quote no-group" data-username="KFrank" data-post="2" data-topic="59335">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/48.png" class="avatar"> KFrank:</div>
<blockquote>
<p>t.ge (100.0) * t.le (125.0), -1.0</p>
</blockquote>
</aside>
<p><a class="mention" href="/u/kfrank">@KFrank</a><br>
The script you described set all elements whose values lies in between 100 and 125 to -1.<br>
However, my elements are just some indices and I want the elements who are between specific start and end to be -1.<br>
Just to clarify the confusion let me change the start and end to 41 and 7 respectively.<br>
I want to change <code>x = torch.tensor([1,3,4,41,10,7,5,1,8,3,41,90,78,7,41])</code> to<br>
<code>torch.tensor([1,3,4,-1,-1,-1,5,1,8,3,-1,-1,-1,-1,41])</code>.</p>
<p>With the code you sent <code> x.masked_fill_(x.ge(41) * x.le(4), -1.0)</code> the output wouldn’t change.</p>

username: KFrank
cooked: <p>Hello fbrah!</p>
<aside class="quote no-group" data-username="fbrah" data-post="5" data-topic="59335" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fbrah/48/12933_2.png" class="avatar"> fbrah:</div>
<blockquote>
<p>Just to clarify the confusion let me change the start and end to 41 and 7 respectively.<br>
I want to change <code>x = torch.tensor([1,3,4,41,10,7,5,1,8,3,41,90,78,7,41])</code> to<br>
<code>torch.tensor([1,3,4,-1,-1,-1,5,1,8,3,-1,-1,-1,-1,41])</code>.</p>
</blockquote>
</aside>
<p>I understand what you are asking now.</p>
<p>I do not see a way to do it using straightforward tensor operations;<br>
I think you will have to loop over the elements in each row.</p>
<p>Best.</p>
<p>K. Frank</p>