Optimize needs a 1D vector to optimize. You are on the right track. You need to flatten your argument to minimize
and then in f
, start with x = np.reshape(x, (2, m, n))
then pull out w
and z
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:
def toVector(w, z):
assert w.shape == (2, 4)
assert z.shape == (2, 4)
return np.hstack([w.flatten(), z.flatten()])
def toWZ(vec):
assert vec.shape == (2*2*4,)
return vec[:2*4].reshape(2,4), vec[2*4:].reshape(2,4)
def doOptimization(f_of_w_z, w0, z0):
def f(x):
w, z = toWZ(x)
return f_of_w_z(w, z)
result = minimize(f, toVec(w0, z0))
# Different optimize functions return their
# vector result differently. In this case it's result.x:
result.x = toWZ(result.x)
return result