pytorch中expand()和expand_as()和repeat()函数解读

简要 三个函数都是不扩展维度却改变tensor维度数值存在的。关于扩展维度查看squeeze和unsqueeze; 关于更改维度位置查看transpose和 permute
1. expand()和expand_as() 这两个函数放在一起说比较好。
expand(*sizes) → Tensor 很简单,扩张函数。但是要注意的是:-1 代表了保持不更改该维度的尺寸大小。

  • example:
>>> x = torch.tensor([[1], [2], [3]]) >>> x.size() torch.Size([3, 1]) >>> x.expand(3, 4) tensor([[ 1,1,1,1], [ 2,2,2,2], [ 3,3,3,3]]) >>> x.expand(-1, 4)# -1 means not changing the size of that dimension tensor([[ 1,1,1,1], [ 2,2,2,2], [ 3,3,3,3]])

expand_as(other_tensor) → Tensor
  • 等价于self.expand(other_tensor.size())
>>> y=torch.tensor([[2,2],[3,3],[5,5]]) >>> print(y.size()) torch.Size([3, 2]) >>> x.expand_as(y) tensor([[2, 2], [3, 3], [4, 4]])

2.repeat() 【pytorch中expand()和expand_as()和repeat()函数解读】这个功能类似expand()
主要还是看样例
batch_size = 2 seq_len = 4 embedding_size =8 embedding = torch.rand(1, seq_len, seq_len)) # [1, 4, 4]repeat_dims = [1] * embedding.dim() # [1,1,1] repeat_dims[0] = batch_size # [2, 1,1] embedding = embedding.repeat(*repeat_dim) # [b, 4,4]

    推荐阅读