rfeinman / pytorch-minimize

Newton and Quasi-Newton optimization with PyTorch
https://pytorch-minimize.readthedocs.io
MIT License
308 stars 34 forks source link

RuntimeError: ScalarFunction was supplied a function that does not return scalar outputs. #24

Closed zanellar closed 1 year ago

zanellar commented 1 year ago

hi, do you have implemented also the VectorFunction?

rfeinman commented 1 year ago

Hi @zanellar - can you explain a bit more what you are trying to do, or perhaps provide a code example?

Most of the tools in this package concern optimizing a scalar function of multiple variables.

zanellar commented 1 year ago

I need to find the root of a function from R^n to R^m


import torch
from torchmin import minimize
initial_x = torch.tensor([4.,5.], requires_grad = True) 

def solve_func(x): 
    return torch.exp(x) - torch.tensor([2.,1.])

res = minimize(
    solve_func, 
    initial_x, 
    method='bfgs', 
    options=dict(line_search='strong-wolfe'),
    max_iter=50,
    disp=2
)  
print('final x: {}'.format(res.x))
bhalazs commented 1 year ago

Hey @zanellar, to minimize an R^n to R^m function you should try the least squares solver: https://pytorch-minimize.readthedocs.io/en/latest/api/generated/torchmin.least_squares.html#torchmin.least_squares

rfeinman commented 1 year ago

Closing this now that @bhalazs has pointed out the correct solution