The shape of the mask [2205] at index 0 does not match the shape of the indexed tensor [2205, 7] at index 1

username: Ardeal
cooked: <p>Hi<br>
For the following code,<br>
shape of <code>outputs</code> is: <code>torch.Size([2205, 7])</code><br>
shape of <code>labels</code> is: <code>torch.Size([2205])</code></p>
<p>I am experiencing the error:</p>
<pre data-code-wrap="Python"><code class="lang-nohighlight">The shape of the mask [2205] at index 0 does not match the shape of the indexed tensor [2205, 7] at index 1
</code></pre>
<pre data-code-wrap="Python"><code class="lang-nohighlight">t0 = torch.zeros_like(outputs)
t0[range(outputs.size()[0]), labels] = 1
</code></pre>

username: Matias_Vasquez
cooked: <p>Can you post how labels is defined?</p>
<p>I tried to reproduce the error with this code, but it works.</p>
<pre><code class="lang-python">outputs = torch.rand(2205, 7)
labels = torch.randint(0, 7, (2205,))

t0 = torch.zeros_like(outputs)
t0[range(outputs.size(0)), labels] = 1
</code></pre>

username: Ardeal
cooked: <p>labels is a tensor,<br>
dtype = torch.uint8<br>
min value in labels is 0, max value in labels is 6</p>

username: Matias_Vasquez
cooked: <p>It seems to be the type that is causing the error.</p>
<p>When defining <code>labels = torch.randint(0, 7, (2205,), dtype=torch.uint8)</code> I get the same error as you.</p>
<p>A possible workaround would be to cast it as <code>long</code>.</p>
<p>This ↓ should work.</p>
<pre><code class="lang-python">outputs = torch.rand(2205, 7)
labels = torch.randint(0, 7, (2205,), dtype=torch.uint8)

t0 = torch.zeros_like(outputs)
t0[range(outputs.size(0)), labels.long()] = 1
</code></pre>

username: Ardeal
cooked: <aside class="quote no-group" data-username="Matias_Vasquez" data-post="4" data-topic="151153">
<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/matias_vasquez/48/47847_2.png" class="avatar"> Matias_Vasquez:</div>
<blockquote>
<p><code>labels.long()</code></p>
</blockquote>
</aside>
<p>Thank you!<br>
This error is very weird!<br>
the index torch.uint8 and torch.long are different???</p>

username: Matias_Vasquez
cooked: <p>Apparently <code>uint8</code> is used for masking and <code>int64</code> for indexing.</p>
<p>I believe this means that if you have <code>3</code> for example, which <code>int64</code> it would understand that you need position number <code>4</code> starting from <code>0</code>. But with <code>uint8</code> you would get a mask like this <code>0000 0011</code>. (But I might be wrong).</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/70916">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/70916" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/70916" target="_blank" rel="noopener nofollow ugc">uint8 scalar tensors cannot be used for integer indexing</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-01-06" data-time="13:54:27" data-timezone="UTC">01:54PM - 06 Jan 22 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/pmeier" target="_blank" rel="noopener nofollow ugc">
          <img alt="pmeier" src="https://avatars.githubusercontent.com/u/6849766?v=4" class="onebox-avatar-inline" width="20" height="20">
          pmeier
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: python array api
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Integer, scalar tensors should behave like integers when used as index. Tensors <span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">of dtype `torch.uint8` deviate from that:

```python
import torch

t_1d_single = torch.empty(1)
t_1d_multi = torch.empty(2)

for dtype in (torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64):
    print("single", dtype, t_1d_single[torch.tensor(0, dtype=dtype)].shape)
    print("multi1", dtype, t_1d_multi[torch.tensor(0, dtype=dtype)].shape)
    print("multi2", dtype, t_1d_multi[torch.tensor(1, dtype=dtype)].shape)
    print("#" * 50)
```

```
single torch.uint8 torch.Size([0, 1])
multi1 torch.uint8 torch.Size([0, 2])
multi2 torch.uint8 torch.Size([1, 2])
##################################################
single torch.int8 torch.Size([])
multi1 torch.int8 torch.Size([])
multi2 torch.int8 torch.Size([])
##################################################
single torch.int16 torch.Size([])
multi1 torch.int16 torch.Size([])
multi2 torch.int16 torch.Size([])
##################################################
single torch.int32 torch.Size([])
multi1 torch.int32 torch.Size([])
multi2 torch.int32 torch.Size([])
##################################################
single torch.int64 torch.Size([])
multi1 torch.int64 torch.Size([])
multi2 torch.int64 torch.Size([])
##################################################
```

cc @mruberry @rgommers @pmeier @asmeurer @leofang @AnirudhDagar @asi1024 @emcastillo @kmaehashi</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/17346">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/17346" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/17346" target="_blank" rel="noopener nofollow ugc">torch.Tensor(dtype = torch.uint8) is not OK for index_select?</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-02-21" data-time="07:41:54" data-timezone="UTC">07:41AM - 21 Feb 19 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2019-02-21" data-time="20:39:48" data-timezone="UTC">08:39PM - 21 Feb 19 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/huanglin6385" target="_blank" rel="noopener nofollow ugc">
          <img alt="huanglin6385" src="https://avatars.githubusercontent.com/u/29923614?v=4" class="onebox-avatar-inline" width="20" height="20">
          huanglin6385
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## 🐛 Bug



## To Reproduce

Steps to reproduce the behavior:

import to<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">rch
number = torch.randn(6,3,11)
p = torch.arange(10)
print(0,0,p[0],number[0,0,p[0]])
p = torch.arange(10,dtype = torch.uint8)
print(0,0,p[0],number[0,0,p[0]])
print(0,0,p[0],number[0,0,int(p[0])])

output:
0 0 tensor(0) **tensor(-0.2130)**
0 0 tensor(0, dtype=torch.uint8) **tensor([], size=(0, 11))**
0 0 tensor(0, dtype=torch.uint8) **tensor(-0.2130)**

torch.uint8 is not OK for index！


## Expected behavior



## Environment

Please copy and paste the output from our
[environment collection script](https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py)
(or fill out the checklist below manually).

You can get the script and run it with:
```
wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
```

 - PyTorch Version (e.g., 1.0):
 - OS (e.g., Linux):
 - How you installed PyTorch (`conda`, `pip`, source):
 - Build command you used (if compiling from source):
 - Python version:
 - CUDA/cuDNN version:
 - GPU models and configuration:
 - Any other relevant information:

## Additional context</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: Ardeal
cooked: <p>Thank you!<br>
<a class="mention" href="/u/matias_vasquez">@Matias_Vasquez</a></p>