Making Fibonacci sphere number of points differentiable

username: lelouedec
cooked: <p>Hey,</p>
<p>I am trying to generate kind of procedural displacement maps using Fibonacci sphere coordinates.</p>
<p>Basically the following code is generating me 2d coordinates I am using later own to create my texture.</p>
<blockquote>
<p>seed_nb = torch.tensor(600,device = pyredner.get_device(), requires_grad=True)<br>
goldenRatio = torch.tensor((1 + 5**0.5)/2,device = pyredner.get_device())<br>
i = torch.arange(0, seed_nb.long(),device = pyredner.get_device())<br>
theta      = 2 * torch_PI * i / goldenRatio<br>
fibo_phi   = torch.acos(torch.ones(1,device = pyredner.get_device()) - torch.tensor(2*(i+0.5)/n,device = pyredner.get_device()))<br>
fibo_theta = (theta%(2<em>torch_PI))-torch_PI<br>
# print(phi.min(),phi.max(),theta.min(),theta.max())<br>
indices_x = (fibo_phi</em>1800/np.pi).long()<br>
indices_y = ( ((fibo_theta*1800/np.pi)+1800)).long()</p>
</blockquote>
<p>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.</p>
<p>I am a bit stuck trying to find a way to go around this arange somehow.<br>
Any idea ?</p>
<p>Justin</p>

username: KFrank
cooked: <p>Hi Justin!</p>
<aside class="quote no-group quote-modified" data-username="lelouedec" data-post="1" data-topic="142048" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lelouedec/48/3563_2.png" class="avatar"> lelouedec:</div>
<blockquote>
<blockquote>
<p>seed_nb = torch.tensor(600,device = pyredner.get_device(), requires_grad=True)</p>
</blockquote>
<p>The problem is that I want the variable “seed_nb” to be differentiable/learned to be the right value when back-propagating.</p>
</blockquote>
</aside>
<p>There is no straightforward way to do what you want.  The problem is<br>
that the number of points on your sphere is an integer and there is no<br>
sensible way to differentiate with respect to an integer.</p>
<p>Helpfully, pytorch won’t let you do this:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.9.0'
&gt;&gt;&gt; torch.tensor (600, device = 'cpu', requires_grad = True)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
RuntimeError: Only Tensors of floating point and complex dtype can require gradients
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

username: lelouedec
cooked: <p>Hi Frank,</p>
<p>I Figured there wouldn’t straightforward way to do it unfortunately <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=12" title=":confused:" class="emoji" alt=":confused:"><br>
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 <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=12" title=":confused:" class="emoji" alt=":confused:"><br>
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.</p>
<p>Justin</p>

username: KFrank
cooked: <p>Hi Justin!</p>
<aside class="quote no-group" data-username="lelouedec" data-post="3" data-topic="142048" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lelouedec/48/3563_2.png" class="avatar"> lelouedec:</div>
<blockquote>
<p>having a very high number of points and learn some float array dropping part of it,</p>
</blockquote>
</aside>
<p>One general approach – that may not work for your use case – for<br>
making integer stuff differentiable is to blend (interpolate) neighboring<br>
integers together.</p>
<p>Let’s say that a Fibonacci sphere with a certain number of points (say<br>
600) leads to some result, <code>f (600)</code>, and a sphere with 601 points leads<br>
to some other result, <code>f (601)</code>.</p>
<p>You could have <code>seed_nb</code> to be a floating-point number (rather than<br>
an integer) and then define the corresponding result, <code>f (seed_nb)</code><br>
via interpolation:</p>
<pre data-code-wrap="python"><code class="lang-python">f (600.3)  =  0.7 * f (600)  +  0.3 * f (601)
</code></pre>
<p>Whether this could make sense depends on the specifics or your<br>
use case, but it does give you an expression that can be usefully<br>
differentiated with respect to a floating-point <code>seed_nb</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

username: lelouedec
cooked: <p>Hi Frank !</p>
<p>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.<br>
I am going to think about an other solution to that points problem while making it differentiable.<br>
Thank you Frank</p>
<p>Justin</p>