1、Tensor的维度和尺寸
不同类型的数据可以用不同维度(dimension)的张量来表示,标量为0维张量,向量为1维张量,矩阵为2维以上的张量;可以简单地总结为:有几层中括号,就是多少维的张量;可以使用shape属性或者size()方法查看张量在每一维的长度;
标量:
# 标量(0维张量)
scalar = torch.tensor(1)
print(scalar)
print(scalar.dim())
print(scalar.size())
print(scalar.shape)
# 输出
tensor(1)
0
torch.Size([])
torch.Size([])
向量:
#向量(1维张量)
vector = torch.tensor([1.0, 2.0, 3.0, 4.0])
print(vector)
print(vector.dim())
print(vector.size())
print(vector.shape)
# 输出
tensor([1., 2., 3., 4.])
1
torch.Size([4])
torch.Size([4])
矩阵
# 2维张量
matrix = torch.tensor([[1.0,2.0],[3.0,4.0]])
print(matrix)
print(matrix.dim())
print(matrix.size())
print(matrix.shape)
# 输出
tensor([[1., 2.],
[3., 4.]])
2
torch.Size([2, 2])
torch.Size([2, 2])
# 3维张量
tensor3 = torch.tensor([[[1.0,2.0],[3.0,4.0]],[[5.0,6.0],[7.0,8.0]]])
print(tensor3)
print(tensor3.dim())
print(tensor3.size())
print(tensor3.shape)
# 输出
tensor([[[1., 2.],
[3., 4.]],
[[5., 6.],
[7., 8.]]])
3
torch.Size([2, 2, 2])
torch.Size([2, 2, 2])
# 4维张量
tensor4 = torch.tensor([[[[1.0,1.0],[2.0,2.0]],[[3.0,3.0],[4.0,4.0]]],
[[[5.0,5.0],[6.0,6.0]],[[7.0,7.0],[8.0,8.0]]]])
print(tensor4)
print(tensor4.dim())
print(tensor4.size())
print(tensor4.shape)
# 输出
tensor([[[[1., 1.],
[2., 2.]],
[[3., 3.],
[4., 4.]]],
[[[5., 5.],
[6., 6.]],
[[7., 7.],
[8., 8.]]]])
4
torch.Size([2, 2, 2, 2])
torch.Size([2, 2, 2, 2])
2、创建Tensor
Import from List:
a = [1, 2]
a = torch.tensor(a)
print(a)
# 输出
tensor([1, 2])
Import from numpy:
a = np.array([1, 2])
a = torch.from_numpy(a)
print(a)
# 输出
tensor([1, 2], dtype=torch.int32)
Torch.FloatTensor(d1, d2, d3)
tensor = torch.FloatTensor(1,2)
print(tensor)
print(tensor.dim())
print(tensor.shape)
# 输出
tensor([[-1.2401e-17, 4.5907e-41]])
2
torch.Size([1, 2])
tensor = torch.FloatTensor(2, 3, 3)
print(tensor)
print(tensor.dim())
print(tensor.shape)
# 输出
tensor([[[9.2755e-39, 1.0561e-38, 1.0929e-38],
[9.6429e-39, 8.9082e-39, 9.4592e-39],
[5.0510e-39, 4.2246e-39, 1.0286e-38]],
[[1.0653e-38, 1.0194e-38, 8.4490e-39],
[1.0469e-38, 9.3674e-39, 9.9184e-39],
[8.7245e-39, 9.2755e-39, 8.9082e-39]]])
3
torch.Size([2, 3, 3])
rand
a = torch.rand(2,3)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[0.4862, 0.3772, 0.4883],
[0.8780, 0.4097, 0.4205]])
2
torch.Size([2, 3])
rand_like
a = torch.rand(2,3)
b = torch.rand_like(a)
print(b)
print(b.dim())
print(b.shape)
# 输出
tensor([[0.6247, 0.7405, 0.7412],
[0.8705, 0.7099, 0.0307]])
2
torch.Size([2, 3])
randint
a = torch.randint(1, 10, (3, 3))
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[6, 3, 1],
[1, 4, 5],
[9, 5, 9]])
2
torch.Size([3, 3])
randn
a = torch.randn(3,3)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[ 0.8001, 1.7763, 0.3723],
[ 0.0721, 0.6027, 0.1838],
[-0.9242, -1.8679, -0.1747]])
2
torch.Size([3, 3])
full
a = torch.full([2, 3], 10)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[10, 10, 10],
[10, 10, 10]])
2
torch.Size([2, 3])
arange
a = torch.arange(0, 10)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1
torch.Size([10])
a = torch.arange(0, 10, 2)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([0, 2, 4, 6, 8])
1
torch.Size([5])
ones
a = torch.ones(3, 3)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
2
torch.Size([3, 3])
zeros
a = torch.zeros(3, 3)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
2
torch.Size([3, 3])
eye
a = torch.eye(3)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
2
torch.Size([3, 3])
randperm
a = torch.randperm(10)
print(a)
print(a.dim())
print(a.shape)
# 输出
tensor([3, 4, 2, 8, 7, 5, 0, 9, 1, 6])
1
torch.Size([10])