当前位置: 代码迷 >> 综合 >> expand 和 repeat 区别
  详细解决方案

expand 和 repeat 区别

热度:47   发布时间:2024-02-10 08:25:27.0

expand()

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

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

import torch
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.

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

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]])
  相关解决方案