# Histogram for 3D tensor with negative weights

Hello.

I have a `(B, N, 3)` pointsets and `(B, N, 1)` pointwise features which I wish to voxelize. The goal of this voxelisation is to be able to query from the voxel, (approximated) feature at any given point with minimal computation time. For example, given a new point and I wish to compute its feature, with a voxel representation, I can interpolate from neighbouring nodes of the given point, which is easy to compute knowing the edge information. Alternatively, I can use KNN , but when N is large (typically 1million), this operation is quite slow.

I can construct my voxels from pointset using `torch.histogram`, but how can I fill those voxels with my features instead of binary values? I can possibly use the weights option in `torch.histogram` however, my point-wise features contain negative values while I can imagine the histogram weights to be positive. Can someone please help me out?

Thank you.

Depending on the numbers B, N, and the size of the voxel grid here, one option might be to do kernel regression. This is quite computationally intensive during voxelization, but in contrast to binning like histograms do, does interpolation, too.

A blunt way to mimic what a histogram does is to quantize the coordinates into integers starting from 0 and then use them as the indices of a sparse tensor. These are allowed to have duplicate indices. Then tensor.coalesce() will sum all values at a given coordinate.

Of course, there so many ways to do this, but these are the ones I’d try first.

Best regards

Thomas

Hello Thomas,

Thanks for the reply.

A blunt way to mimic what a histogram does is to quantize the coordinates into integers starting from 0 and then use them as the indices of a sparse tensor. These are allowed to have duplicate indices. Then tensor.coalesce() will sum all values at a given coordinate.

Could you please elaborate on the quantizing part? If suppose my batched point cloud `A = torch.randn((32, 10000, 3))` is quantized to a variable `B` (an integer) such that `B.shape == (32, 10000, 3)` with each value pointing to a place where corresponding point in A falls into the grid of shape `(64,64,64)` that would be excellent for my usecase.

With those indices, I can then do scatter_mean over my feature tensor.

Thank you