简体   繁体   中英

numpy equivalent code of unsqueeze and expand from torch tensor method

I have these 2 tensors

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

and i have a code in pytorch

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

but i want to convert the above code in numpy
for box_a and box_b i can do something like this

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

But what about this

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

solved it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM