一. torch.repeat()函数解析
1. 说明
官网:torch.tensor.repeat(),函数说明如下图所示:
2. 函数功能
torch.tensor.repeat()函数可以对张量进行重复扩充
1) 当参数只有两个时:(行的重复倍数,列的重复倍数),1表示不重复。
2) 当参数有三个时:(通道数的重复倍数,行的重复倍数,列的重复倍数),1表示不重复。
3. 代码例子如下:
3.1 输入一维张量,参数为一个,即表示在列上面进行重复n次
a = torch.randn(3)
a,a.repeat(4)
结果如下所示:
(tensor([ 0.81, -0.57, 0.10]),
tensor([ 0.81, -0.57, 0.10, 0.81, -0.57, 0.10, 0.81, -0.57, 0.10, 0.81,
-0.57, 0.10]))
3.2 输入一维张量,参数为两个(m,n),即表示先在列上面进行重复n次,再在行上面重复m次,输出张量为二维
a = torch.randn(3)
a,a.repeat(4,2)
(tensor([ 0.06, -0.76, -0.59]),
tensor([[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59],
[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59],
[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59],
[ 0.06, -0.76, -0.59, 0.06, -0.76, -0.59]]))
3.3 输入一维张量,参数为三个(b,m,n),即表示先在列上面进行重复n次,再在行上面重复m次,最后在通道上面重复b次,输出张量为三维
a = torch.randn(3)
a,a.repeat(3,4,2)
输出结果如下:
(tensor([2.25, 0.49, 1.47]),
tensor([[[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47]],
[[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47]],
[[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47],
[2.25, 0.49, 1.47, 2.25, 0.49, 1.47]]]))
3.4 输入二维张量,参数为两个(m,n),即表示先在列上面进行重复n次,再在行上面重复m次,输出张量为两维(注意参数个数必须大于输入张量维度个数)
a = torch.randn(3,2)
a,a.repeat(4,2)
输出结果如下:
(tensor([[-0.58, -1.21],
[-0.35, 0.68],
[ 0.33, 0.70]]),
tensor([[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70],
[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70],
[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70],
[-0.58, -1.21, -0.58, -1.21],
[-0.35, 0.68, -0.35, 0.68],
[ 0.33, 0.70, 0.33, 0.70]]))
3.5 输入二维张量,参数为三个(b,m,n),即表示先在列上面进行重复n次,再在行上面重复m次,最后在通道上面重复b次,输出张量为三维。(注意输出张量维度个数为参数个数)
a = torch.randn(3,2)
a,a.repeat(3,4,2)
输出结果如下:
(tensor([[-0.75, 1.20],
[-1.50, 1.75],
[-0.05, 0.40]]),
tensor([[[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40]],
[[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40]],
[[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40],
[-0.75, 1.20, -0.75, 1.20],
[-1.50, 1.75, -1.50, 1.75],
[-0.05, 0.40, -0.05, 0.40]]]))
参考知识文章