简体   繁体   English

如何将 Pytorch 张量拆分为不同的维度?

[英]How to split a Pytorch tensor into different dimensions?

I'm new to Pytorch .我是 Pytorch 的新手。

Let's say I have a tensor that has this shape torch.size([1, 25200, 11])假设我有一个具有这种形状的张量torch.size([1, 25200, 11])

I want to split it into 3 smaller tensors , each of 3 smaller tensors has the shape of 1st.我想把它分成 3 个更小的张量,3 个更小的张量中的每一个都具有第一个的形状。 torch.size([1, 3, 80, 80, 11]) and 2nd torch.size([1, 3, 40, 40 , 11]) and 3rd torch.size([1, 3, 20, 20, 11)]. torch.size([1, 3, 80, 80, 11])和第二个torch.size([1, 3, 40, 40 , 11])3rd torch.size([1, 3, 20, 20, 11)].

Really appreciate your help.真的很感谢你的帮助。

Thanks谢谢

Explain those numbers:解释这些数字:

80x80x3 = 19200 80x80x3 = 19200

40x40x3 = 4800 40x40x3 = 4800

20x20x3=1200 , add these result we have 25200, 1 is batch size, 11 is classes + xywh 20x20x3=1200 ,加上这些结果我们有 25200,1 是批量大小,11 是类 + xywh

Something like this should work.像这样的事情应该有效。

import torch
tensor = torch.ones((1, 25200, 11))
first_break = tensor[:, 0:19200, :].view((1, 3, 80, 80, 11))
second_break = tensor[:, 19200:19200+4800, :].view((1, 3, 40, 40, 11))
third_break = tensor[:, 19200+4800:19200+4800+1200, :].view((1, 3, 20, 20, 11))

If you give a bit more explanation and context the code could get cleaned up and not be so hardcoded, or maybe this gives you enough to run with.如果你给出更多的解释和上下文,代码可以得到清理而不是那么硬编码,或者这可能让你足够运行。

Have you tried:你有没有尝试过:


  T1= torch.narrow(YourTensor, 1,0 , 80*80*3)
  T1v = T1.view(1,3,80,80,11)

  T2= torch.narrow(YourTensor, 1,80*80*3 , 40*40*3)
  T2v = T2.view(1,3,40,40,11)

  T3= torch.narrow(YourTensor, 1,80*80*3 + 40*40*3 , 20*20*3)
  T3v = T3.view(1,3,20,20,11)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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