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

早过忘川 提交于 2019-12-05 14:15:17

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