Lets say i have to tensors
x (len, batch, feature) and duration (len, batch)
And I want to convert it for example like this:
x (1, 2, 3, 4) + dur (3, 3, 2, 1) = > (1, 1, 1, 2, 2, 2, 3, 3, 4)
So repeat each element with value for second tensor times.
For now i made this code, but it is super ineffective
max_len = dur.sum(0).max()

out_tensor = torch.zeros((max_len, x.size(1), x.size(2))).cuda()
for idx in range(x.size(1)):
    text_expanded = torch.cat([text_.repeat(len_, 1)
                                for text_, len_ in zip(x[:, idx], dur[:, idx])])
    out_tensor[:len(text_expanded), idx] = text_expanded