torch.arange(start=0, end, step=1,……)
创建一维张量
import torch
x = torch.arange(12)
a = torch.arange(1,12)
b = torch.arange(1,12,2)
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
tensor([ 1, 3, 5, 7, 9, 11])
shape 可显示张量的维度
x.shape
out:torch.Size([12])
numel 可获取张量中的元素数量
x.numel()
out:12
reshape(m,n,…) 将张量进行重新排布
X = x.reshape(3, 4)
Y = x.reshape(3, 2, 2)
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
tensor([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])