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()

这个功能类似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]
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值