[SciPy-User] Gradient inputs to SciPy optimize routines
David Baddeley
david_baddeley@yahoo.com...
Mon Jul 23 20:21:31 CDT 2012
I would speculate that the reason the optimise routines don't accept arbitrary sequence types for the Jacobian is one of performance. The gradient functions get called many times during the optimisation and pythons sequence types are slow enough that converting the arbitrary sequence into an array would probably be slower than computing the Jacobian using finite differences within the algorithm. Although calling np.ascontiguous has relatively low overhead if the data is already an array, it is another function call (expensive in python) and could be up to ~30% of total running time for a simple model function. The same applies to too many checks on data type. Within the optimising loop, they will be costly.
I like to think of all scipy functions as being designed to work on numpy arrays, but some of them will gracefully do the conversion for you if you give them the wrong data type.
cheers,
David
________________________________
From: The Helmbolds <helmrp@yahoo.com>
To: Sturla Molden <sturla@molden.no>; SciPy Users List <scipy-user@scipy.org>
Sent: Tuesday, 24 July 2012 3:12 AM
Subject: Re: [SciPy-User] Gradient inputs to SciPy optimize routines
Thanks. That's good to know. So if all the optimize routines simply inserted
fprime = np.ascontinuaousarray(fprime)
before calling _minimize_(whatever), the user's `fprime` function could return any "array like" object and all would be well?
The Helmbolds
2645 E Southern Ave A241
Tempe AZ 85282
Email: helmrp@yahoo.com
VOX: 480-831-3611
CELL Igor: 480-438-3918
CELL Alf: 602-568-6948 (but not often turned on)
From: Sturla Molden <sturla@molden.no>
>To: The Helmbolds <helmrp@yahoo.com>; SciPy Users List <scipy-user@scipy.org>
>Cc: Warren Weckesser <warren.weckesser@enthought.com>; SciPy Users List <scipy-user@scipy.org>
>Sent: Monday, July 23, 2012 7:36 AM
>Subject: Re: [SciPy-User] Gradient inputs to SciPy optimize routines
>
<snip>
>
>The NumPy functions np.ascontiguousarray and np.asfortranarray are often used internally in SciPy for this purpose; that is, they will only make a copy if the argument is not a NumPy ndarray that can be passed safely to C or Fortran. Any Python sequence which is not a C or Fortran contiguous NumPy array will be converted in that process.
>
>Sturla
_______________________________________________
SciPy-User mailing list
SciPy-User@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120723/7d4d7993/attachment.html
More information about the SciPy-User
mailing list