tensor.stride()¶
Stride is the jump necessary to go from one element to the next one in the specified dimension dim.
一个元素到另一个元素,元素粒度
任意维度上的步长,是其低维度乘积。
shape: (12, 512, 768) stride: (512x768x1, 768x1, 1x1)
tensor.as_strided()¶
input (Tensor) – the input tensor.
size (tuple or ints) – the shape of the output tensor
stride (tuple or ints) – the stride of the output tensor
storage_offset (int, optional) – the offset in the underlying storage of the output tensor
由input张量,以指定的size, stride, storage_offset创建新的view。
In [2]:
import torch
tensor.stride¶
In [46]:
x = torch.randn(3, 2, 4)
x
Out[46]:
In [77]:
print(f"0维度上的步长:{len(x[0]) * len(x[0][0] * 1)}\n"
f"1维度上的步长:{len(x[0][0]) * 1}\n"
f"2维度上的步长:{1 * 1}")
x.stride()
Out[77]:
In [78]:
x[0]
Out[78]:
In [67]:
x[0][0]
Out[67]:
In [59]:
x[0][0][0]
Out[59]:
In [61]:
x.shape
Out[61]:
In [84]:
x1 = torch.randn(2, 3, 5) # stride: (3 * 5 * 1, 5 * 1, 1 * 1)
print(x1)
x1.stride()
Out[84]:
In [75]:
x2 = torch.randn(10, 15, 256, 64) # stride: (15 * 256 * 64 * 1, 256 * 64 * 1, 64 * 1, 1 *1)
print((15 * 256 * 64 * 1, 256 * 64 * 1, 64 * 1, 1 *1))
x2.stride()
Out[75]:
tensor.as_strided¶
In [104]:
x1.as_strided((2,2,2), (16,10,4))
In [105]:
h = torch.randn(12,8,512,64)
In [106]:
h.stride()
Out[106]:
In [ ]:
Comments