【问题标题】:numpy equivalent code of unsqueeze and expand from torch tensor method从火炬张量方法中解压和扩展的numpy等效代码
【发布时间】:2021-07-26 22:54:21
【问题描述】:

我有这两个张量

box_a = torch.randn(1,4)
box_b = torch.randn(1,4)

我在 pytorch 中有一个代码

box_a[:, 2:].unsqueeze(1).expand(1, 1, 2)

但我想将上面的代码转换成 numpy
对于box_abox_b,我可以这样做

  box_a = numpy.random.randn(1,4)
  box_b = numpy.random.randn(1,4)

但是这个呢

box_a[:, 2:].unsqueeze(1).expand(1, 1, 2)

【问题讨论】:

    标签: python numpy pytorch tensor torch


    【解决方案1】:

    解决了

    box_a = np.random.randn(1,4)
    box_b = np.random.randn(1,4)
    max_xy = np.broadcast_to(np.expand_dims(box_a[:, 2:],axis=1),(1,1,2))
    

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 2015-07-29
      • 2022-07-20
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      • 2017-02-23
      • 2021-11-30
      相关资源
      最近更新 更多