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

Hi
For the following code,
shape of outputs is: torch.Size([2205, 7])
shape of labels is: torch.Size([2205])

I am experiencing the error:

The shape of the mask [2205] at index 0 does not match the shape of the indexed tensor [2205, 7] at index 1
t0 = torch.zeros_like(outputs)
t0[range(outputs.size()[0]), labels] = 1

Can you post how labels is defined?

I tried to reproduce the error with this code, but it works.

outputs = torch.rand(2205, 7)
labels = torch.randint(0, 7, (2205,))

t0 = torch.zeros_like(outputs)
t0[range(outputs.size(0)), labels] = 1

labels is a tensor,
dtype = torch.uint8
min value in labels is 0, max value in labels is 6

It seems to be the type that is causing the error.

When defining labels = torch.randint(0, 7, (2205,), dtype=torch.uint8) I get the same error as you.

A possible workaround would be to cast it as long.

This ↓ should work.

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

Thank you!
This error is very weird!
the index torch.uint8 and torch.long are different???

Apparently uint8 is used for masking and int64 for indexing.

I believe this means that if you have 3 for example, which int64 it would understand that you need position number 4 starting from 0. But with uint8 you would get a mask like this 0000 0011. (But I might be wrong).

Thank you!
@Matias_Vasquez

1 Like