pytorch中repeat方法
repeat()沿着特定的维度重复这个张量,按照倍数扩充1、x.repeat(a)列数乘以a倍,对x进行横向赋值import torchx = torch.tensor([1,2,3,4])print(x)print(x.shape)xnew = x.repeat(3)# 注意x的维度没有被改变,repeat后的维度仅仅传入xnew中。print(x)print(xnew)print(xnew.s
·
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)
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)
更多推荐
已为社区贡献1条内容
所有评论(0)