expand和repeat区别
expand
和 repeat
都是对张量进行扩维,这里记录下使用区别。
expand()
Returns a new view of the :attr:
self
tensor with singleton dimensions expanded to a larger size.
将张量=1
的维度进行扩展,>1
的维度保持不变。
1 | import torch |
1 | a = torch.tensor([[12, 3, 4]]) |
tensor([[12, 3, 4],
[12, 3, 4],
[12, 3, 4]])
repeat()
Repeats this tensor along the specified dimensions.
将张量沿着特定维度进行复制。
1 | b = torch.tensor([[1,2,4],[4,5,6]]) |
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]])