The problem is that I want the variable “seed_nb” to be differentiable/learned to be the right value when back-propagating. So I used a long tensor with one value inside. But because or “torch.arange” it sees that the gradient of this tensor is always None, hence nothing is learned.

I am a bit stuck trying to find a way to go around this arange somehow.
Any idea ?

There is no straightforward way to do what you want. The problem is
that the number of points on your sphere is an integer and there is no
sensible way to differentiate with respect to an integer.

Helpfully, pytorch won’t let you do this:

>>> import torch
>>> torch.__version__
'1.9.0'
>>> torch.tensor (600, device = 'cpu', requires_grad = True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Only Tensors of floating point and complex dtype can require gradients

I Figured there wouldn’t straightforward way to do it unfortunately
I am doing differentiable rendering and thought that perhaps using this float tensor and casting it as an int when using it arange would still let me optimize over it
I am trying to find an other way, perhaps using some sparse tensor or having a very high number of points and learn some float array dropping part of it, but haven’t found anyway yet.

One general approach – that may not work for your use case – for
making integer stuff differentiable is to blend (interpolate) neighboring
integers together.

Let’s say that a Fibonacci sphere with a certain number of points (say
600) leads to some result, f (600), and a sphere with 601 points leads
to some other result, f (601).

You could have seed_nb to be a floating-point number (rather than
an integer) and then define the corresponding result, f (seed_nb)
via interpolation:

f (600.3) = 0.7 * f (600) + 0.3 * f (601)

Whether this could make sense depends on the specifics or your
use case, but it does give you an expression that can be usefully
differentiated with respect to a floating-point seed_nb.

Unfortunately I am not sure there is a way to have this number differentiable, at least not through using this Fibonacci sphere or any method where the localization are integers.
I am going to think about an other solution to that points problem while making it differentiable.
Thank you Frank