梯度下降码的矢量化 [英] Vectorization of a gradient descent code
问题描述
我正在Matlab上实现批量梯度下降.我在theta
的更新步骤中遇到问题.
theta
是两个分量(两行)的向量.
X
是一个包含m
行(训练样本数)和n=2
列(特征数)的矩阵.
Y是m
行向量.
I am implementing a batch gradient descent on Matlab. I have a problem with the update step of theta
.
theta
is a vector of two components (two rows).
X
is a matrix containing m
rows (number of training samples) and n=2
columns (number of features).
Y is an m
rows vector.
在更新步骤中,我需要将每个theta(i)
设置为
During the update step, I need to set each theta(i)
to
theta(i) = theta(i) - (alpha/m)*sum((X*theta-y).*X(:,i))
这可以通过for
循环来完成,但是我不知道如何对其进行矢量化(由于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).
有什么建议吗?
推荐答案
好像您正在尝试进行简单的矩阵乘法,MATLAB最擅长于此.
Looks like you are trying to do a simple matrix multiplication, the thing MATLAB is supposedly best at.
theta = theta - (alpha/m) * (X' * (X*theta-y));
这篇关于梯度下降码的矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!