# Explain Pytorch Tensor.stride and Tensor.storage with code examples

`x = torch.arange(12).view((3,4))x`
`tensor([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]])`
`storage = x.storage()storage`
` 0 1 2 3 4 5 6 7 8 9 10 11[torch.LongStorage of size 12]`
`stride = x.stride()stride`
`idx = (2, 1)item = x[idx].item()item`
`loc = idx[0]*stride[0] + idx[1]*stride[1]`
`loc = sum([i*s for i,s in zip(idx, stride)])`
`x = torch.rand(1200).view((10, 30, 4))storage = x.storage()stride = x.stride()stride`
`idx = (4, 23, 2)item = x[idx].item()loc = sum([i*s for i,s in zip(idx, stride)])storage[loc]==item`
`x = torch.tensor([[0, 1, 2]])`
`y = x.expand(2, 3)z = x.repeat(2, 1)`
`tensor([[0, 1, 2],        [0, 1, 2]])`
`xstorage = x.storage()xstride  = x.stride()ystorage = y.storage()ystride  = y.stride()zstorage = z.storage()zstride  = z.stride()`
` 0 1 2[torch.LongStorage of size 3]`
` 0 1 2[torch.LongStorage of size 3]`
` 0 1 2 0 1 2[torch.LongStorage of size 6]`
`idx = (1,2)yloc = sum([i*s for i,s in zip(idx, ystride)])zloc = sum([i*s for i,s in zip(idx, zstride)])assert y[idx].item()==ystorage[yloc]assert z[idx].item()==zstorage[zloc]`

--

--

--

## More from Yang Zhang

Software Engineering SMTS at Salesforce Commerce Cloud Einstein

Love podcasts or audiobooks? Learn on the go with our new app.

## Yang Zhang

Software Engineering SMTS at Salesforce Commerce Cloud Einstein