[英]Vectorization of a gradient descent code
I am implementing a batch gradient descent on Matlab. 我正在Matlab上实现批量梯度下降。 I have a problem with the update step of
theta
. 我对
theta
的更新步骤有theta
。 theta
is a vector of two components (two rows). theta
是两个组件(两行)的向量。 X
is a matrix containing m
rows (number of training samples) and n=2
columns (number of features). X
是包含m
行(训练样本数)和n=2
列(特征数)的矩阵。 Y is an m
rows vector. Y是
m
行向量。
During the update step, I need to set each theta(i)
to 在更新步骤中,我需要将每个
theta(i)
为
theta(i) = theta(i) - (alpha/m)*sum((X*theta-y).*X(:,i))
This can be done with a for
loop, but I can't figure out how to vectorize it (because of the X(:,i)
term). 这可以通过
for
循环完成,但我无法弄清楚如何对其进行矢量化(因为X(:,i)
术语)。
Any suggestion? 有什么建议吗?
看起来你正在尝试做一个简单的矩阵乘法,MATLAB应该是最好的。
theta = theta - (alpha/m) * (X' * (X*theta-y));
除了Mad Physicist给出的答案之外,还可以应用以下内容。
theta = theta - (alpha/m) * sum( (X * theta - y).* X )';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.