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

六月ゝ 毕业季﹏ 提交于 2019-12-07 07:22:05

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/31292374/how-do-i-put-2-matrix-into-scipy-optimize-minimize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!