I work with the scipy.optimize.minimize
function.
My purpose is get w,z
which minimize f(w,z)
Both w
and z
are n by m matrices:
[[1,1,1,1],
[2,2,2,2]]
f(w,z) is receive parameter w and z.
I already tried the form given below:
def f(x):
w = x[0]
z = x[1]
...
minimize(f, [w,z])
but, minimize does not work well.
What is the valid form to put two matrices (n by m) into scipy.optimize.minimize
?
Optimize needs a 1D vector to optimize. You are on the right track. You need to flatten your argument to
minimize
and then inf
, start withx = np.reshape(x, (2, m, n))
then pull outw
andz
and you should be in business.I've run into this issue before. For example, optimizing parts of vectors in multiple different classes at the same time. I typically wind up with a function that maps things to a 1D vector and then another function that pulls the data back out into the objects so I can evaluate the cost function. As in: