Convert tensor of floats to tensor of ordinal data

Hi,

Let’s say I have a tensor with shape (32,), e.g.

X = [ 0.0379, -0.0372,  0.0277, -0.1918, -0.0679,  0.0858,  0.0655,  0.2807,
     -0.1375, -0.0066,  0.1309,  0.0893,  0.0757,  0.1891,  0.2998, -0.1810,
     -0.0809,  0.1543, -0.0852, -0.1351,  0.0900, -0.1985, -0.0525,  0.0582,
      0.0410,  0.3986,  0.3757,  0.2447, -0.3319, -0.2319, -0.0495, -0.0034]

I’m seeking how to convert X into a (32, 100) tensor where 100 is the dimension of a one-hot encoding.
I know that values range between [-1, 1].
I would like to discretise this interval in 100 bins and place a 1 in the bin the value corresponds to, and 0 elsewhere.
Is it possible to do this efficiently with pytorch?

Thanks

You could use np.digitize to create the indices of the bins and then use the one_hot method as seen here:

x = np.array([ 0.0379, -0.0372,  0.0277, -0.1918, -0.0679,  0.0858,  0.0655,  0.2807,
              -0.1375, -0.0066,  0.1309,  0.0893,  0.0757,  0.1891,  0.2998, -0.1810,
              -0.0809,  0.1543, -0.0852, -0.1351,  0.0900, -0.1985, -0.0525,  0.0582,
              0.0410,  0.3986,  0.3757,  0.2447, -0.3319, -0.2319, -0.0495, -0.0034])

bins = np.linspace(x.min(), x.max(), 100)
y = np.digitize(x, bins) - 1
y = torch.from_numpy(y)
one_hot = torch.nn.functional.one_hot(y, num_classes=100)