首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在带有附加参数的numpy数组上使用scipy.optimize.root

在带有附加参数的numpy数组上使用scipy.optimize.root
EN

Stack Overflow用户
提问于 2019-06-11 17:17:48
回答 1查看 872关注 0票数 0

给定优化问题(1) (如下所示),其中p_ip'_iw_ji用于i=0,...,6889,我想使用Levenberg-Marquardt方法来使用scipy.optimize.rootR_jv_j找到最佳解决方案(我对任何其他建议都持开放态度)。

但是,我不知道如何设置需要传递给root的可调用函数。到目前为止,我所拥有的就是这个,这显然是错误的。

代码语言:javascript
复制
def fun(x, (old_points, new_points, weights, n_joints)):
    """
    :param x: variable to optimize. It is supposed to encapsulate R and v from (1)
    :param old_points: original vertex positions, (6890,3) numpy array
    :param new_points: transformed vertex positions, (6890,3) numpy array
    :param weights: weight matrix obtained from spectral clustering, (n_joints, 6890) numpy array
    :param n_joints: number of joints
    :return: non-linear cost function to find the root of
    """
    # Extract rotations and offsets
    R = np.array([(np.array(x[j * 15:j * 15 + 9]).reshape(3, 3)) for j in range(n_joints)])
    v = np.array([(np.array(x[j * 15 + 9:j * 15 + 12])) for j in range(n_joints)])

    # Use equation (1) for the non-linear pass.
    # R_j p_i
    Rp = np.einsum('jkl,il', x, old_points) # x shall replace R
    # w_ji (Rp_ij + v_j)
    wRpv = np.einsum('ji,ijk->ik', weights, Rp + x) # x shall replace v

    # Set up a non-linear cost function, then compute the squared norm.
    d = new_points - wRpv
    result = np.einsum('ik,ik', d, d)

    return result

编辑:现在这是正确的结果。

EN

回答 1

Stack Overflow用户

发布于 2019-06-11 18:04:05

使用你原来的fun (但给它一个更好的名字)

代码语言:javascript
复制
def fun(x, (old_points, new_points, weights, n_joints)):
    """
    :param x: variable to optimize. It is supposed to encapsulate R and v from (1)
    :param old_points: original vertex positions, (6890,3) numpy array
    :param new_points: transformed vertex positions, (6890,3) numpy array
    :param weights: weight matrix obtained from spectral clustering, (n_joints, 6890) numpy array
    :param n_joints: number of joints
    :return: non-linear cost function to find the root of
    """
    # Extract rotations and offsets
    R = np.array([(np.array(x[j * 15:j * 15 + 9]).reshape(3, 3)) for j in range(n_joints)])
    v = np.array([(np.array(x[j * 15 + 9:j * 15 + 12])) for j in range(n_joints)])

    # Use equation (1) for the non-linear pass.
    # R_j p_i
    Rp = np.einsum('jkl,il', x, old_points) # x shall replace R
    # w_ji (Rp_ij + v_j)
    wRpv = np.einsum('ji,ijk->ik', weights, Rp + x) # x shall replace v

    # Set up a non-linear cost function, then compute the squared norm.
    d = new_points - wRpv
    result = np.einsum('ik,ik', d, d)

    return result

对它做一个闭包,这样它就只有一个输入(您正在求解的变量):

代码语言:javascript
复制
old_points = ...
new_points = ...
weights = ...
rv = ...
n_joints = ...
def cont_function(x):
    return fun(x, old_points, new_points, weights, rv, n_joints)

现在尝试在roots中使用cost_function

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56540350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档