repeat 函数
1.repeat参数个数与tensor向量维数一致
- a = torch.tensor([[1, 2, 3],
- [1, 2, 3]])
- b = a.repeat(2, 2)
- print(b.shape)
复制代码 效果为:
即repeat的参数是对应维度的复制个数,上段代码为0维复制两次,1维复制两次,则得到以上运行效果。其余扩展情况依此类推。
2.repeat参数个数与tensor向量维数不一致
在参数个数大于原tensor维度个数时,总是先在第0维扩展一个维数为1的维度,然后按照参数指定的复制次数进行复制。计算输出的形状时,可以按照 对应参数*对应维度维数 得到效果
- # a形状(2,3)
- a = torch.tensor([[1, 2, 3],
- [1, 2, 3]])
- # repeat参数比维度多
- # 首先在第0维扩展一个维度,维数为1,然后按照参数指定的次数进行复制
- # 在扩展前先将a的形状扩展为(1,2,3)然后复制
- b = a.repeat(1, 2, 1)
- print(b.shape) # 得到结果torch.Size([1, 4, 3])
复制代码- # a形状(2,3)
- a = torch.tensor([[1, 2, 3],
- [1, 2, 3]])
- # repeat参数比维度多,在扩展前先将a的形状扩展为(1,2,3)然后复制
- b = a.repeat(1, 1, 2)
- print(b.shape) # 得到结果torch.Size([1, 2, 6])
复制代码- # a形状(2,3)
- a = torch.tensor([[1, 2, 3],
- [1, 2, 3]])
- # repeat参数比维度多,在扩展前先将a的形状扩展为(1,2,3)然后复制
- b = a.repeat(2, 1, 1)
- print(b.shape) # 得到结果torch.Size([2, 2, 3])
复制代码 squeeze 函数
torch.unsqueeze()函数:减少数组A指定位置N的维度。
如果不指定位置参数N,如果数组A的维度为(1,1,3)。
如果指定位置参数,执行 torch.squeeze(A,1) ,A的维度变为 (1,3),中心的维度被删除。
注:
- 如果指定的维度大于1,那么将操作无效
- 如果不指定维度N,那么将删除全部维度为1的维度
- a=torch.randn(1,1,3)
- print(a.shape) # torch.Size([1, 1, 3])
- b=torch.squeeze(a)
- print(b.shape) # torch.Size([3])
- c=torch.squeeze(a,0)
- print(c.shape) # torch.Size([1, 3])
- d=torch.squeeze(a,1)
- print(d.shape) # torch.Size([1, 3])
- e=torch.squeeze(a,2)#如果去掉第三维,则数不够放了,所以直接保留
- print(e.shape) # torch.Size([1, 1, 3])
复制代码 unsqueeze 函数
torch.unsqueeze()函数:增加数组A指定位置N的维度。
两行三列的数组A维度为(2,3),那么这个数组就有三个位置可以增加维度,分别是
- ([位置0], 2,[位置1], 3, [位置2])
- 或者
- ( [位置-3] ,2,[位置-2], 3 ,[位置-1] )
复制代码 如果执行 torch.unsqueeze(A,1),数据的维度就变为了 (2,1,3)
- a=torch.randn(1,3)
- print(a.shape) # torch.Size([1, 3])
- b=torch.unsqueeze(a,0)
- print(b.shape) # torch.Size([1, 1, 3])
- c=torch.unsqueeze(a,1)
- print(c.shape) # torch.Size([1, 1, 3])
- d=torch.unsqueeze(a,2)
- print(d.shape) # torch.Size([1, 3, 1])
复制代码 flatten 函数
flatten() 是对多维数据的降维函数。
flatten(),默认缺省参数为0,也就是说flatten()和flatte(0)效果一样。
python里的flatten(dim)表示,从第dim个维度开始睁开,将后面的维度转化为一维.也就是说,只保留dim之前的维度,其他维度的数据全都挤在dim这一维。
- import torch
- a = torch.rand(2,3,4)
- print(a.shape) # torch.Size([2, 3, 4])
- b = a.flatten()
- print(b.shape) # torch.Size([24])
- c = a.flatten(0)
- print(c.shape) # torch.Size([24])
- d = a.flatten(1)
- print(d.shape) # torch.Size([2, 12])
- e = a.flatten(2)
- print(e.shape) # torch.Size([2, 3, 4])
复制代码 transpose函数
二维数组
- import numpy as np
- X=np.arange(6).reshape((2,3))
- print(X)
- #[[0 1 2]
- # [3 4 5]]
- print(X.transpose())
- #[[0 3]
- # [1 4]
- # [2 5]]
- print(X.T)
- #[[0 3]
- # [1 4]
- # [2 5]]
复制代码 多维数组
- x=np.arange(24).reshape((2,3,4))
- print(x.shape)
- y = x.transpose((0,1,2))
- print(y.shape)
- y = x.transpose((0,2,1))
- print(y.shape)
- y = x.transpose((2,1,0))
- print(y.shape)
- #(2, 3, 4)
- #(2, 3, 4)
- #(2, 4, 3)
- #(4, 3, 2)
复制代码 参考网址
https://blog.csdn.net/tequila53/article/details/119183678
https://blog.csdn.net/kuan__/article/details/116987162
说明
说明如下,如有侵权,非常抱歉,可联系本人删除对应内容。
会根据平时使用不断更新博客内容。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |