expand和repeat区别
Swift Lv6

expandrepeat 都是对张量进行扩维,这里记录下使用区别。

expand()

Returns a new view of the :attr:self tensor with singleton dimensions expanded to a larger size.

将张量=1的维度进行扩展,>1的维度保持不变。

1
import torch
1
2
a = torch.tensor([[12, 3, 4]])
a.expand(3,-1)
tensor([[12,  3,  4],
        [12,  3,  4],
        [12,  3,  4]])

repeat()

Repeats this tensor along the specified dimensions.

将张量沿着特定维度进行复制。

1
2
b = torch.tensor([[1,2,4],[4,5,6]])
b.repeat(3,2)
tensor([[1, 2, 4, 1, 2, 4],
        [4, 5, 6, 4, 5, 6],
        [1, 2, 4, 1, 2, 4],
        [4, 5, 6, 4, 5, 6],
        [1, 2, 4, 1, 2, 4],
        [4, 5, 6, 4, 5, 6]])
Powered by Hexo & Theme Keep
Unique Visitor Page View