I have the following segment of nested for loop in my code. The nested loop is slowing down my complete execution.
for a torch tensor extended_output with shape [batchSize,nClass*repeat] and another torch tensor with dimension [batchSize,nClass], I want the aggregation to happen as follows:
for q in range(nClass):
for u in range(repeat):
output[:,q]=output[:,q]+extended_output[:,(q+u*nClass)]
Here, nClass,repeat all are integer variables with value 1400 and 8 repectively.
Can this nested for loop be avoided using pytorch broadcasting? Any help will be highly useful.
A sample working cpode might be like this
import torch
nClass=1400
repeat=8
batchSize=64
output=torch.zeros([batchSize,nClass])
extended_output=torch.rand([batchSize,nClass*repeat])
for q in range(nClass):
for u in range(repeat):
output[:,q]=output[:,q]+extended_output[:,(q+u*nClass)]