repeat()沿着特定的维度重复这个张量,按照倍数扩充
1、x.repeat(a)
列数乘以a倍,对x进行横向赋值

import torch
x = torch.tensor([1,2,3,4])
print(x)
print(x.shape)
xnew = x.repeat(3)
# 注意x的维度没有被改变,repeat后的维度仅仅传入xnew中。
print(x)
print(xnew)
print(xnew.shape)

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210301100958501.png

2、x.reshape(a,b)
列数先乘以b倍,再行数乘以a倍。即对x先横向复制b倍,再纵向复制a倍

import torch
x = torch.tensor([1,2,3,4])
print(x)
print(x.shape)
xnew_1 = x.repeat(1,3)
xnew_2 = x.repeat(3,1)
xnew_3 = x.repeat(2,3)
print(xnew_1,xnew_1.shape)
print(xnew_2,xnew_2.shape)
print(xnew_3,xnew_3.shape)

在这里插入图片描述
2、x.reshape(a,b,c)
同理,从c到a变化。

import torch
x = torch.tensor([1,2,3,4])
print(x)
print(x.shape)
xnew_1 = x.repeat(1,1,3)
xnew_2 = x.repeat(1,2,3)
xnew_3 = x.repeat(2,1,1)
xnew_4 = x.repeat(2,1,3)
xnew_5 = x.repeat(2,2,3)
print(xnew_1,xnew_1.shape)
print(xnew_2,xnew_2.shape)
print(xnew_3,xnew_3.shape)
print(xnew_4,xnew_4.shape)
print(xnew_5,xnew_5.shape)

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐