Hi Niko!



 nikogamulin:

When I tried to run the optimizer, it returned nan values.
This is the code that I used to find the minimum with scipy.optimize:
def get_pow_geometric(f, angle, power):
    return np.sign(f(angle))*np.abs(f(angle)) ** power

…
Then I tried to use the optimizer the following way:
def get_pow_geometric(f, angle, power):
    return torch.pow(torch.sign(f(angle))*torch.abs(f(angle)), power)



Your pytorch code raises, in effect, f (angle) to a power, while your
numpy code raises np.abs (f (angle)) to a power.  When f (angle)
is negative and power is fractional, your pytorch code will return nan.
(As an aside, pytorch also supports the “**” syntax for exponentiation.)
Consider:
>>> import torch
>>> torch.__version__
'1.7.1'
>>> def get_pow_geometric (f, angle, power):
...     return torch.pow (torch.sign (f (angle)) * torch.abs (f (angle)), power)
...
>>> def get_pow_geometric_B (f, angle, power):
...     return torch.sign (f (angle)) * torch.pow (torch.abs (f (angle)), power)
...
>>> def get_pow_geometric_C (f, angle, power):
...     return torch.sign (f (angle)) * torch.abs (f (angle))**power
...
>>> get_pow_geometric (torch.sin, torch.tensor ([1.0]), 0.5)
tensor([0.9173])
>>> get_pow_geometric (torch.sin, torch.tensor ([-1.0]), 0.5)
tensor([nan])
>>> get_pow_geometric_B (torch.sin, torch.tensor ([-1.0]), 0.5)
tensor([-0.9173])
>>> get_pow_geometric_C (torch.sin, torch.tensor ([-1.0]), 0.5)
tensor([-0.9173])

Best.
K. Frank