gpt4 book ai didi

python - 脚本时间改进

转载 作者:行者123 更新时间:2023-11-28 18:40:15 24 4
gpt4 key购买 nike

我正在尝试提高部分脚本的时间效率,但我没有更多想法。我在 Matlab 和 Python 中运行了以下脚本,但 Matlab 的实现比 Python 的实现快四倍。知道如何改进吗?

Python:

import time
import numpy as np

def ComputeGradient(X, y, theta, alpha):
m = len(y)
factor = alpha / m
h = np.dot(X, theta)
theta = [theta[i] - factor * sum((h-y) * X[:,i]) for i in [0,1]]
#Also tried this but with worse performances
#diff = np.tile((h-y)[:, np.newaxis],2)
#theta = theta - factor * sum(diff * X)
return theta

if __name__ == '__main__':
data = np.loadtxt("data_LinReg.txt", delimiter=',')
theta = [0, 0]
alpha = 0.01
X = data[:,0]
y = data[:,1]
X = np.column_stack((np.ones(len(y)), X))
start_time = time.time()
for i in range(0, 1500, 1):
theta = ComputeGradient(X, y, theta, alpha)
stop_time = time.time()
print("--- %s seconds ---" % (stop_time - start_time))

--> 0.048s

Matlab:

data = load('data_LinReg.txt');
X = data(:, 1); y = data(:, 2);
m = length(y);
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);
iterations = 1500;
alpha = 0.01;
tic
for i = 1:1500
theta = gradientDescent(X, y, theta, alpha);
end
toc

function theta = gradientDescent(X, y, theta, alpha)
m = length(y); % number of training examples
h = X * theta;
t1 = theta(1) - alpha * sum(X(:,1).*(h-y)) / m;
t2 = theta(2) - alpha * sum(X(:,2).*(h-y)) / m;
theta = [t1; t2];
end

--> 0.01s

[编辑]:解决方案

一种可能的途径是使用 numpy 向量化而不是 python 根函数。在建议的代码中,将 sum 替换为 np.sum 提高了时间效率,使其更接近 Matlab(0.019s 而不是 0.048s)

此外,我分别测试了向量上的函数:np.dot、np.sum、*(乘积),所有这些函数似乎都比等效的 Matlab 更快(在某些情况下确实更快)。我想知道为什么它在 Python 中仍然比较慢....

最佳答案

此解决方案提供了一个优化的 MATLAB 实现 -

  • gradient-descent 实现的函数内联。
  • 预先计算在循环中重复使用的某些值。

代码-

data = load('data_LinReg.txt');

iterations = 1500;
alpha = 0.01;
m = size(data,1);

M = alpha/m; %// scaling factor

%// Pre-compute certain values that are repeatedly used inside the loop
sum_a = M*sum(data(:,1));
sum_p = M*sum(data(:,2));
sum_ap = M*sum(data(:,1).*data(:,2));
sum_sqa = M*sum(data(:,1).^2);
one_minus_alpha = 1 - alpha;
one_minus_sum_sqa = 1 - sum_sqa;

%// Start processing
t1n0 = 0;
t2n0 = 0;
for i = 1:iterations
temp = t1n0*one_minus_alpha - t2n0*sum_a + sum_p;
t2n0 = t2n0*one_minus_sum_sqa - t1n0*sum_a + sum_ap;
t1n0 = temp;
end
theta = [t1n0;t2n0];

快速测试表明,这比问题中发布的 MATLAB 代码有明显的加速。

现在,我对 python 不太熟悉,但我认为这个 MATLAB 代码可以很容易地移植到 python。

关于python - 脚本时间改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27134344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com