How to replace "repeat" method instead of "for loop"

Hi,
I have two “for” loop in my code:

for i in range(0, 64):
for j in range (0,64):
y = torch.cat((A[:,i,:], B[:, j, :]), dim =2)

I want to replace the second for with “repeat” command (repeating 64 regions in B). Any one help?
Thanks!

The shape of A[:,i,:] and B[:, j, :] is CxW, so the range of dim is [-2, 1], your ‘dim=2’ itself is wrong.

1 Like

Thank you, and suggestion for how to replace j loop with “repeat” function?

Assume the shape of B is CxHxW. If the shape of repeat output of B is

  1. 64xCxHxW: B.unsqueeze(dim=0).repeat(64,1,1,1)
  2. 64CxHxW: B.repeat(64,1,1)
  3. Cx64HxW: B.repeat(1,64,1)
  4. CXHx64W: B.repeat(1,1,64)

Thank you so much, so in this case, it can be like:

for i in range(0, 64):
y = torch.cat((A[:,i,:], repeatB[1, 64, 1]), dim =[-2, 1])

right?

No, may you give me the shape of A and B and detailedly describe your problem ?

A and B both are in: [506, 16, 256]
And I want to move through each 4*4 partitions. (repeat: 16 times)

Also, for two for loop;
for i in range(1, 17):
for j in range (1, 17):
y = torch.cat((A[:,i,:], B[:, j, :]), dim =[-2, 1])
I faced this error:
cat(): argument ‘dim’ must be int, not list
Thanks!

I need further to know the shape of output y. In your above code, y is overwritten 16 times. And which axis you want to cat along,? since the shape of A[:,i,:] and B[:, j, :] is 506x256(CXW), cat along C-rows or W-cols?

well, y is needed to be [506, 256, 512], hence I want to do cat in dim=2.
Actually, I want to reshape and transpose both A and B to be as [506, 16, 256] first, then by overwritten each of them 16 times, concating them together. Finally, the achieved result should be in [506, 256, 512] (first dimension for A and second for B).