Binning tensor values

Hello,
I am looking for a way to bin tensor values.
Say my tensor has values between -0.255 and +0.256, I am looking for a way to represent this will “less numbers”.
For example I would like to bin this range into 32 values so I will only have the values -0.255, -0.239, -0.223 and so on… (the bins are in jumps of 16 because 512/32=16)
I found torch.histc but it gives me only a histogram, this way I lose “which index was mapped to each bin”.
Thanks!

1 Like

I’m having the same issue. Have you found a clean solution for this?

@erap129 , @nemz I guess you are looking for quantization.

The following snippet ‘bins’ values to the nearest multiple of 0.05

In [17]: a = torch.rand(10)

In [18]: a
Out[18]:
tensor([0.1523, 0.5210, 0.9864, 0.3272, 0.3473, 0.5408, 0.3397, 0.4553, 0.0236,
        0.7338])

In [19]: b=0.05

In [20]: torch.round(a/b)*b
Out[20]:
tensor([0.1500, 0.5000, 1.0000, 0.3500, 0.3500, 0.5500, 0.3500, 0.4500, 0.0000,
        0.7500])