矢量值函数

发布于 2025-01-22 11:38:12 字数 1131 浏览 0 评论 0原文

我有以下功能:

def h_Y1(X, theta):
    EF = X[0]
    FF = X[1]
    j = X[-2]
    k = X[-1]
    W = X[2:-2]
    Sigma = theta[0]
    sigma_xi2 = theta[1]
    gamma_alpha = theta[2]
    gamma_z = np.array(theta[3:])
    gW = gamma_z @ W
    eps1 = EF -  gamma_alpha * gW
    if j == k:
        eps2 = FF - (gamma_alpha**2)*gW - gW*sigma_xi2 - Sigma
        eps3 = 0
    else:
        eps2 = 0
        eps3 = FF - (gamma_alpha**2)*gW
    h1 = [eps1 * Wk for Wk in W]
    h2 = [eps2 * Wk for Wk in W]
    h3 = [eps3 * Wk for Wk in W]
    return np.concatenate([h1, h2, h3])

我需要为一系列值执行函数,这些范围存储在gmmarray中。具体来说,我希望为gmmarray的每一行作为函数参数x,用于固定theta

我目前使用以下代码这样做:

import numpy as np
theta = [0.01, 1, 1, 0, 0]
gmmarray = np.random.random((1120451, 6))
test = np.apply_along_axis(h_Y1, 1, gmmarray, theta = init)

但是,这很慢 - 大约需要19秒。我尝试将函数矢量化如下:

Vh_Y1 = np.vectorize(h_Y1, signature = '(n),(j)->(i)')
test1 = Vh_Y1(gmmarray, init)

但是,这仍然需要16秒。我在这里做错了什么,还是有办法进一步加快速度?

非常感谢!

I have the following function:

def h_Y1(X, theta):
    EF = X[0]
    FF = X[1]
    j = X[-2]
    k = X[-1]
    W = X[2:-2]
    Sigma = theta[0]
    sigma_xi2 = theta[1]
    gamma_alpha = theta[2]
    gamma_z = np.array(theta[3:])
    gW = gamma_z @ W
    eps1 = EF -  gamma_alpha * gW
    if j == k:
        eps2 = FF - (gamma_alpha**2)*gW - gW*sigma_xi2 - Sigma
        eps3 = 0
    else:
        eps2 = 0
        eps3 = FF - (gamma_alpha**2)*gW
    h1 = [eps1 * Wk for Wk in W]
    h2 = [eps2 * Wk for Wk in W]
    h3 = [eps3 * Wk for Wk in W]
    return np.concatenate([h1, h2, h3])

I need to execute the function for a range of values, which are stored in gmmarray. Specifically, I'd like the function to be run for each row of gmmarray as the function argument X, for a fixed theta.

I'm currently doing this using the following code:

import numpy as np
theta = [0.01, 1, 1, 0, 0]
gmmarray = np.random.random((1120451, 6))
test = np.apply_along_axis(h_Y1, 1, gmmarray, theta = init)

However, this is slow - it takes around 19 seconds. I tried vectorizing the function as follows:

Vh_Y1 = np.vectorize(h_Y1, signature = '(n),(j)->(i)')
test1 = Vh_Y1(gmmarray, init)

However, this still takes 16 seconds. Am I doing something wrong here or is there a way to speed things up further?

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2025-01-29 11:38:12

您可以将完整的gmmarray作为x参数传递。然后,您可以在其列上使用矢量化操作,而不是通过gmmarray的每一行循环。

这样的事情:

def h_Y1_vectorized(X, theta):
    EF, FF, W, j, k = np.hsplit(X, [1,2,4,5])         # Column vectors (except W)
    Sigma, sigma_xi2, gamma_alpha, *gamma_z = theta

    gW = (W @ gamma_z)[:, None]                       # Ensure column vector
    ga_gW = gamma_alpha * gW
    FF_ga2gW = FF - gamma_alpha * ga_gW

    eps1 = EF - ga_gW
    j_equal_k = j == k
    eps2 = np.where(j_equal_k, FF_ga2gW - gW * sigma_xi2 - Sigma, 0)
    eps3 = np.where(j_equal_k, 0, FF_ga2gW)

    h1 = eps1 * W
    h2 = eps2 * W
    h3 = eps3 * W

    return np.hstack([h1, h2, h3])

调用

>>> h_Y1_vectorized(gmmarray, theta)

大约提高100倍的速度会产生相同的结果。

You can pass the full gmmarray as the X parameter. Then, instead of looping through each row of gmmarray, you can use vectorized operations on its columns.

Something like this:

def h_Y1_vectorized(X, theta):
    EF, FF, W, j, k = np.hsplit(X, [1,2,4,5])         # Column vectors (except W)
    Sigma, sigma_xi2, gamma_alpha, *gamma_z = theta

    gW = (W @ gamma_z)[:, None]                       # Ensure column vector
    ga_gW = gamma_alpha * gW
    FF_ga2gW = FF - gamma_alpha * ga_gW

    eps1 = EF - ga_gW
    j_equal_k = j == k
    eps2 = np.where(j_equal_k, FF_ga2gW - gW * sigma_xi2 - Sigma, 0)
    eps3 = np.where(j_equal_k, 0, FF_ga2gW)

    h1 = eps1 * W
    h2 = eps2 * W
    h3 = eps3 * W

    return np.hstack([h1, h2, h3])

Calling

>>> h_Y1_vectorized(gmmarray, theta)

produces the same result with roughly a 100x speed increase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文