在torch中,如果要改变某一个tensor的维度,可以利用view、expand、repeat、transpose和permute等方法,这里对这些方法的一些容易混淆的地方做个总结。
expand和repeat
关于expand,例子如下:
a = torch.tensor([[[1, 2, 3]]])
a.shape
>> torch.size([1, 1, 3])
b = a.expand(2, 1, 3) # 该处的3对应a中3个数字,需要匹配,而由于其他维度没有数据体现,所以维度可以随意改变
b = a.expand(123, 2, 2421, 3)
b.shape
>> torch.size([123, 2, 2421, 3])
b = a.expand(532, 123, 1, 213, -1) # 当某一维度为-1时,表示维持原维度数据不变
b = a.expand(2, 3) # 这是错误的,需要对每个维度的数据扩增进行指示,只能大于或等于原数据维度,不可小于
a = torch.tensor([[[1, 2, 3], [1, 2, 3]]])
a.shape
>> torch.size([1, 2, 3])
b = a.expand(2, 1, 3) # 错误
a = torch.tensor([[[1, 2, 3]], [[1, 2, 3]]])
a.shape
>> torch.size([2, 1, 3])
b = a.expand(10, 4, 12, 3) # 错误
关于repeat,例子如下:
a = torch.tensor([[[1, 2, 3]]])
a.shape
>> torch.Size([1, 1, 3])
b = a.repeat(2, 2, 4).shape
>> torch.Size([2, 2, 12])
b = a.repeat(2, 2, 2, 4).shape
>> torch.Size([2, 2, 2, 12])
b = a.repeat(-1, -1, 4).shape # 错误,-1不可用在repeat中
repeat可以实现与expand同样的效果,但不同的是,repeat开辟了新的内存空间,返回了一个新的张量,而expand返回的张量与原版张量具有相同内存地址,例子如下:
a = torch.tensor([[[1, 2, 3]]])
b = a.expand(3, -1, -1, -1)
c = a.repeat(3, 1, 1, 1)
print(a)
print(b)
print(c)
print(a.data_ptr())
print(b.data_ptr())
print(c.data_ptr())
a[0][0][2] = 666
print(a)
print(b)
print(c)
输出为:
tensor([[[1, 2, 3]]])
tensor([[[[1, 2, 3]]],
[[[1, 2, 3]]],
[[[1, 2, 3]]]])
tensor([[[[1, 2, 3]]],
[[[1, 2, 3]]],
[[[1, 2, 3]]]])
1765580867264
1765580867264
1765577935296
tensor([[[ 1, 2, 666]]])
tensor([[[[ 1, 2, 666]]],
[[[ 1, 2, 666]]],
[[[ 1, 2, 666]]]])
tensor([[[[1, 2, 3]]],
[[[1, 2, 3]]],
[[[1, 2, 3]]]])
可以看出,b与a有着相同的地址,b相当于是a的某种视图映射,但仍然受到a的影响,但c是另外开辟内存空间进行存储,不受a的影响。