1、
view()
函数,该函数可任意改变矩阵形状。
要求数据必须时连续存储的,通常在此操作前一步使用contiguous()
函数。
contiguous()
函数返回一个连续内存空间,与原张量数据相同。
import torch
torch.manual_seed(1)
a = torch.randn(3, 4)
b = a.view(2, 6)
c = a.contiguous().view(2,6)
'''
print(a)
a[0][0] = 1.111
print(a)
print(b)
print(c)
'''
2、
permute()
函数,将tensor的维度换位。
import torch
torch.manual_seed(1)
a = torch.randn(2, 3, 5)
b = a.permute(2,0,1)
'''
a[0][0][0] = 1.111
print(a)
print(a.size())
print(b)
print(b.size())
'''
3、
transpose(dim0, dim1)
函数,交换多为矩阵中的两个维度,input和output共享底层存储,改变其中一个张量内容,也会改变另一个张量内容。
import torch
torch.manual_seed(1)
a = torch.randn(3, 4)
b = a.transpose(0, 1)
'''
a[0][0] = 1.111
print(a)
print(a.size())
print(b)
print(b.size())
'''