Hi Frederik!



 FCHR18:

We read that as long as we use the torch functions it should work, but can someone verify this


No, this won’t work.
Being discrete in nature, the histogramming operation is not (usefully)
differentiable, so, mathematically, there is no way to backpropagate
through it.
It is true – for reasons that I do not understand – that torch.histc()
does have a companion HistcBackward function.  It doesn’t, however,
implement a derivative for histc() (because it wouldn’t make sense):
>>> import torch
>>> torch.__version__
'1.9.0'
>>> hist = torch.histc (torch.randn (10, requires_grad = True), bins = 10)
>>> hist.grad_fn
<HistcBackward object at 0x000001F87CB2E5F8>
>>> hist.sum().backward()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<path_to_pytorch_install>\torch\_tensor.py", line 255, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "<path_to_pytorch_install>\torch\autograd\__init__.py", line 149, in backward
    allow_unreachable=True, accumulate_grad=True)  # allow_unreachable flag
RuntimeError: the derivative for 'histc' is not implemented.

Best.
K. Frank