python加速幂_在Python中加速矩阵向量乘法和幂运算,可能通过调用C/C++实现

该博客探讨了一位机器学习项目开发者如何加速Python中的矩阵向量乘法和幂运算,尤其是针对Logistic损失函数的计算。通过使用NumPy库,作者发现计算效率仍有提升空间,期望不借助并行化而是利用C或C++进行优化,以应对数据矩阵中稀疏向量和大量行的情况。文章提供了当前实现的代码,并寻求进一步优化的建议。
摘要由CSDN通过智能技术生成

我目前正在进行一个机器学习项目,在这个项目中,给定一个数据矩阵Z和一个向量rho,我必须计算logistic loss function在rho处的值和斜率。计算包括基本的矩阵向量乘法和log/exp运算,以及避免数值溢出的技巧(如本文previous post所述)。在

我目前使用的是Python中的NumPy,如下所示(作为参考,这段代码在0.2s中运行)。虽然这很好用,但是我想加快速度,因为我在代码中多次调用这个函数(它代表了我项目中90%以上的计算量)。在

我正在寻找任何方法来提高这个代码的运行时间而不进行并行化(即只有1个CPU)。< /强>我很高兴使用Python中的任何公开可用的包,或者调用C或C++(因为我听说这会提高运行时间一个数量级)。预处理数据矩阵Z也可以。一些可以用来更好地计算的东西是向量rho通常是稀疏的(大约50%的条目=0),并且通常far行多于列(在大多数情况下n_cols <= 100)import time

import numpy as np

np.__config__.show() #make sure BLAS/LAPACK is being used

np.random.seed(seed = 0)

#initialize data matrix X and label vector Y

n_rows, n_cols = 1e6, 100

X = np.random.random(size=(n_rows, n_cols))

Y = np.random.randint(low=0, high=2, size=(n_rows, 1))

Y[Y==0] = -1

Z = X*Y # all operations are carried out on Z

def compute_logistic_loss_value_and_slope(rho, Z):

#compute the value and slope of the logistic loss function in a way that is numerically stable

#loss_value: (1 x 1) scalar = 1/n_rows * sum(log( 1 .+ exp(-Z*rho))

#loss_slope: (n_cols x 1) vector = 1/n_rows * sum(-Z*rho ./ (1+exp(-Z*rho))

#see also: https://stackoverflow.com/questions/20085768/

scores = Z.dot(rho)

pos_idx = scores > 0

exp_scores_pos = np.exp(-scores[pos_idx])

exp_scores_neg = np.exp(scores[~pos_idx])

#compute loss value

loss_value = np.empty_like(scores)

loss_value[pos_idx] = np.log(1.0 + exp_scores_pos)

loss_value[~pos_idx] = -scores[~pos_idx] + np.log(1.0 + exp_scores_neg)

loss_value = loss_value.mean()

#compute loss slope

phi_slope = np.empty_like(scores)

phi_slope[pos_idx] = 1.0 / (1.0 + exp_scores_pos)

phi_slope[~pos_idx] = exp_scores_neg / (1.0 + exp_scores_neg)

loss_slope = Z.T.dot(phi_slope - 1.0) / Z.shape[0]

return loss_value, loss_slope

#initialize a vector of integers where more than half of the entries = 0

rho_test = np.random.randint(low=-10, high=10, size=(n_cols, 1))

set_to_zero = np.random.choice(range(0,n_cols), size =(np.floor(n_cols/2), 1), replace=False)

rho_test[set_to_zero] = 0.0

start_time = time.time()

loss_value, loss_slope = compute_logistic_loss_value_and_slope(rho_test, Z)

print "total runtime = %1.5f seconds" % (time.time() - start_time)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值