Tensor index question

Hello!
I have a tensor of size ([128,3,64,64])
lets say this tensor is called testBatch

testBatch[127:].size()
–> torch.Size([1, 3, 64, 64])

testBatch[:1].size()
–> torch.Size([1, 3, 64, 64])

Could someone explain the differences of these two index methods

1 Like

Indexing uses the pattern [start:stop:step].
In the first approach you will get all samples from the start index 127 to the end (so just the last sample), while the second approach will give you all samples from the beginning to index 1, excluding index 1 (so just the first sample):

x = torch.randn(128, 3, 64, 64)

a = x[127:]
b = x[:1]

print((a == x[-1]).all())
> tensor(True)
print((b == x[0]).all())
> tensor(True)
1 Like

Very clear and concise explanation. Thanks so much for taking the time to answer my question!

1 Like