Hi Alex!



 Alex07:

possibly avoiding the unique and loop


There’s no way you can avoid unique() (or its equivalent).  That’s
an inherent bit of processing required by your problem.
You can avoid the loop – depending on the details of your use case,
such as having the values in a being non-negative integers that are
not ridiculously large – by building a look-up table and indexing into
it to translate the values in a:
>>> import torch
>>> torch.__version__
'1.9.0'
>>> a = torch.tensor ([3, 2, 7, 9, 2, 9, 9])
>>> a_unique = torch.unique (a)
>>> lt = torch.full ((a.max() + 1, ), -1)
>>> lt[a_unique] = torch.arange (len (a_unique))
>>> r = lt[a]
>>> r
tensor([1, 0, 2, 3, 0, 3, 3])

(If the elements of a are integers but some are negative, you can
still use the look-up-table approach by shifting the values of a up
so that they become non-negative.)
Best.
K. Frank