Search code examples
pythonpytorch

How do I undo torch.unique_consecutive?


How can I efficiently revert torch.unique_consecutive? I.e.:

x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2])
output, counts = torch.unique_consecutive(x, return_counts=True)



y = torch.SOMETHING(output,counts) #y equals x

Solution

  • Please use torch.repeat_interleave(**args) for your task

    x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2])
    output, counts = torch.unique_consecutive(x, return_counts=True)
    y = torch.repeat_interleave(output, counts)
    #>>y = [1, 1, 2, 2, 3, 1, 1, 2]