[SciPy-User] optimize.least_squares memory allocation every function call

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] optimize.least_squares memory allocation every function call

BrianHare
I have a question about the scipy.optimize.least_squares function.  I have found that the function to optimize needs to reallocate the return array every time the function is called. I've tried having the calling function return the same array every time (just re-filled with the correct return values), but least_squares doesn't seem to converge when I do this.

I do not like having to allocate memory in the calling function, this seems slow and unnecessary. Is there a way around this? If not, could scipy.optimize.least_squares be adjusted so this is possible?

The Gnu Scientific Library (which I sometimes fall back on when scipy.optimize.least_squares is too slow, I think this allocation issue is one major contributor),  passes an array to the calling function to fill.

Thank you,
Brian Hare

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: optimize.least_squares memory allocation every function call

Dominik Stańczak
Hey Brian,

I haven't tested this, but, off the top of my head: would it not be possible to pass a pre-existing allocated array as a `kwarg` to the function, and have your function write values into that array? Then you can add it to the `least_squares` call as `{"return_array": np.zeros(N)}).

Cheers,
Dominik

On Tue, 14 Apr 2020 at 10:40, Brian Hare <[hidden email]> wrote:
I have a question about the scipy.optimize.least_squares function.  I have found that the function to optimize needs to reallocate the return array every time the function is called. I've tried having the calling function return the same array every time (just re-filled with the correct return values), but least_squares doesn't seem to converge when I do this.

I do not like having to allocate memory in the calling function, this seems slow and unnecessary. Is there a way around this? If not, could scipy.optimize.least_squares be adjusted so this is possible?

The Gnu Scientific Library (which I sometimes fall back on when scipy.optimize.least_squares is too slow, I think this allocation issue is one major contributor),  passes an array to the calling function to fill.

Thank you,
Brian Hare
_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: optimize.least_squares memory allocation every function call

Andrew Nelson
In reply to this post by BrianHare
How big a return array are we talking about? 1000 values, 100000?

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user