I'm trying to use the scipy function line_search, but I get an error, which doesn't make sense to me. Probably I am doing somenthing wrong. My goal is to do a line-search for a function f that maps matrices to the real numbers.
I've found a similar question here but unfortunately, applying the solution there doesn't solve my problem, although the author there wants to do something similar with vectors instead of matrices.
I've prepared an easy toy-example.
import numpy
from scipy.optimize import line_search
def fct(x):
return x[0,0]**2+x[1,1]**2
def grad(x):
return np.array([[2*x[0,0],0],[0,2*x[1,1]]])
start = np.array([[1,2],[3,4]])
direction = np.array([[1,1],[0,1]])
result = line_search(fct,grad,start,direction)
The error-message reads as follows:
File "C:\Users\myname\AppData\Local\Continuum\anaconda3\envs\MyEnv\lib\site-packages\scipy\optimize\linesearch.py", line 429, in scalar_search_wolfe2
if (phi_a1 > phi0 + c1 * alpha1 * derphi0) or \
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
It seems like the error occurs in a subroutine of line_search_wolfe2, which is called when I call line_search. Obviously, the value in the line where the error occurs, isn't scalar valued. I took a look in the source, but I couldn't find any reason why this value isn't scalar, as intended.
What is wrong with my input? Do I get anything wrong in the definition of the line_search function?
Thank you in advance for your help!