How Do I put 2 matrix into scipy.optimize.minimize?

lee6boy picture lee6boy · Jul 8, 2015 · Viewed 7.3k times · Source

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?

Answer

Ben picture Ben · Jul 8, 2015

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