我目前正在进行一个机器学习项目,在这个项目中,给定一个数据矩阵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)