首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非线性函数的数值爱因斯坦求和

非线性函数的数值爱因斯坦求和
EN

Stack Overflow用户
提问于 2019-07-13 15:27:56
回答 1查看 80关注 0票数 1

我正在尝试设置以下非线性成本函数(1)和(2)

通过使用numpy einsum函数。

我尝试将它们转换为python,其中R是形状(14,3,3)的数组,而vvt (表示等式(1)中的v‘)是形状(14,3)。分别代表pp'old_pointsnew_points的形状为(6890,3)。

代码语言:javascript
复制
def costfunc(x, old_points, new_points, weights, n_joints):
    """
    Set up non-linear cost functions by using equations from LBS:
    (1) p'_i = sum{j}(w_ji (R_j p_i + v_j))
    (2) p_i - sum{j}(w_ji (R_j p'_i + v'_j))
    where Rt denotes the transpose of R.
    :param old_points: original vertex positions
    :param new_points: transformed vertex positions
    :param weights: weight matrix obtained from spectral clustering
    :param n_joints: number of joints
    :return: non-linear cost functions as in (1), (2) to find the root of
    """

    # Extract rotations R, Rt and offsets v, v' from rv
    R = np.array([(np.array(x[j * 15:j * 15 + 9]).reshape(3, 3)) for j in range(n_joints)])
    Rt = np.array([R[j].T for j in range(n_joints)])
    v = np.array([(np.array(x[j * 15 + 9:j * 15 + 12])) for j in range(n_joints)])
    vt = np.array([(np.array(x[j * 15 + 12:j * 15 + 15])) for j in range(n_joints)])

    ## Use equations (1) and (2) for the non-linear pass.
    # R_j p_i
    Rp = np.einsum('jkl,il', R, old_points)
    Rtv = np.einsum('jkl,il', Rt, v)
    # Rt_j p'_i
    Rtp = np.einsum('jkl,il', Rt, new_points)
    Rvt = np.einsum('jkl,il', R, vt)

    # w_ji (Rp_ij - Rtv_j)
    wRpv = np.einsum('ji,ijk->ik', weights, Rp - Rvt)
    # w_ji (Rtp'_ij - Rv'_j)
    wRtpv = np.einsum('ji,ijk->ik', weights, Rtp - Rtv)

    # Set up a non-linear cost function, then compute the squared norm.
    d = new_points - wRpv
    dt = old_points - wRtpv

    norm = np.linalg.norm(d, axis=1)
    normt = np.linalg.norm(dt, axis=1)

    result = np.concatenate([norm, normt])

    return np.power(result, 2)

现在,在ValueError: operands could not be broadcast together with shapes (6890,14,3) (14,14,3)计算wRpvwRtpv的行中有一个错误。我该如何解决这个问题?任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-14 03:17:52

我现在明白了。这就是解决方案:

代码语言:javascript
复制
def costfunc(x, old_points, new_points, weights, n_joints):
    """
    Set up non-linear cost functions by using equations from LBS:
    (1) p'_i = sum{j}(w_ji (R_j p_i + v_j))
    (2) p_i - sum{j}(w_ji (R_j p'_i + v'_j))
    where Rt denotes the transpose of R.
    :param old_points: original vertex positions
    :param new_points: transformed vertex positions
    :param weights: weight matrix obtained from spectral clustering
    :param n_joints: number of joints
    :return: non-linear cost functions as in (1), (2) to find the root of
    """

    # Extract rotations R, Rt and offsets v, v' from rv
    R = np.array([(np.array(x[j * 15:j * 15 + 9]).reshape(3, 3)) for j in range(n_joints)])
    Rt = np.array([R[j].T for j in range(n_joints)])
    v = np.array([(np.array(x[j * 15 + 9:j * 15 + 12])) for j in range(n_joints)])
    vt = np.array([(np.array(x[j * 15 + 12:j * 15 + 15])) for j in range(n_joints)])

    ## Use equations (1) and (2) for the non-linear pass.
    # R_j p_i
    Rp = np.einsum('jkl,il', R, old_points)
    # Rt_j p'_i
    Rtp = np.einsum('jkl,il', Rt, new_points)

    # R_j v'_j
    Rvt = np.array([R[i] @ vt[i] for i in range(n_joints)])
    # Rt_j v_j
    Rtv = np.array([Rt[i] @ v[i] for i in range(n_joints)])

    # w_ji (Rp_ij - Rtv_j)
    wRpv = np.einsum('ji,ijk->ik', weights, Rp - Rvt)
    # w_ji (Rtp'_ij - Rv'_j)
    wRtpv = np.einsum('ji,ijk->ik', weights, Rtp - Rtv)

    # Set up a non-linear cost function, then compute the squared norm.
    d = new_points - wRpv
    dt = old_points - wRtpv

    norm = np.linalg.norm(d, axis=1)
    normt = np.linalg.norm(dt, axis=1)

    result = np.concatenate([norm, normt])

    return np.power(result, 2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57017071

复制
相关文章

相似问题

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