逻辑回归的向量化实现样例

(Difference between revisions)
 Revision as of 05:25, 8 April 2013 (view source)Kandeng (Talk | contribs)← Older edit Revision as of 07:54, 8 April 2013 (view source)Kandeng (Talk | contribs) (→中英文对照)Newer edit → Line 75: Line 75: ==中英文对照== ==中英文对照== - + :批量梯度上升法 batch gradient ascent + :截距 intercept term + :对数似然函数 the log likelihood + :导函数 derivative + :梯度 gradient ==中文译者== ==中文译者==

Revision as of 07:54, 8 April 2013

\begin{align} h_\theta(x) = \frac{1}{1+\exp(-\theta^Tx)}, \end{align}

[注：下文的符号规范与<公开课程视频>或<教学讲义CS229：机器学习>中的相同，详细内容可以参见公开课程视频或教学讲义#1 http://cs229.stanford.edu/]

\begin{align} \nabla_\theta \ell(\theta) = \sum_{i=1}^m \left(y^{(i)} - h_\theta(x^{(i)}) \right) x^{(i)}_j. \end{align}

% 代码1
for i=1:m,
h = sigmoid(theta'*x(:,i));
temp = y(i) - h;
for j=1:n+1,
grad(j) = grad(j) + temp * x(j,i);
end;
end;

% 代码2
for i=1:m,
grad = grad + (y(i) - sigmoid(theta'*x(:,i)))* x(:,i);
end;

% 矩阵-向量乘法运算的低效代码
for i=1:m,
grad = grad + b(i) * A(:,i);  % 通常写法为A(:,i)*b(i)
end;

% 矩阵-向量乘法运算的高效代码
grad = A*b;

% 代码3
grad = x * (y- sigmoid(theta'*x));

中文译者

Language : English