Logistic Regression Vectorization Example
From Ufldl
m |
|||
Line 18: | Line 18: | ||
\end{align}</math> | \end{align}</math> | ||
- | Suppose that the Matlab/Octave variable <tt>x</tt> is | + | Suppose that the Matlab/Octave variable <tt>x</tt> is a matrix containing the training inputs, so that |
<tt>x(:,i)</tt> is the <math>\textstyle i</math>-th training example <math>\textstyle x^{(i)}</math>, and <tt>x(j,i)</tt> is <math>\textstyle x^{(i)}_j</math>. | <tt>x(:,i)</tt> is the <math>\textstyle i</math>-th training example <math>\textstyle x^{(i)}</math>, and <tt>x(j,i)</tt> is <math>\textstyle x^{(i)}_j</math>. | ||
Further, suppose the Matlab/Octave variable <tt>y</tt> is a ''row'' vector of the labels in the | Further, suppose the Matlab/Octave variable <tt>y</tt> is a ''row'' vector of the labels in the | ||
training set, so that the variable <tt>y(i)</tt> is <math>\textstyle y^{(i)} \in \{0,1\}</math>. (Here we differ from the | training set, so that the variable <tt>y(i)</tt> is <math>\textstyle y^{(i)} \in \{0,1\}</math>. (Here we differ from the | ||
- | CS229 notation | + | CS229 notation. Specifically, in the matrix-valued <tt>x</tt> we stack the training inputs in columns rather than in rows; |
and <tt>y</tt><math>\in \Re^{1\times m}</math> is a row vector rather than a column vector.) | and <tt>y</tt><math>\in \Re^{1\times m}</math> is a row vector rather than a column vector.) | ||
Line 60: | Line 60: | ||
We recognize that Implementation 2 of our gradient descent calculation above is using the slow version with a for-loop, with | We recognize that Implementation 2 of our gradient descent calculation above is using the slow version with a for-loop, with | ||
- | <tt>b(i)</tt> playing the role of <tt>(y(i) - sigmoid(theta'*x(:,i)))</tt>. We can derive a fast implementation as follows: | + | <tt>b(i)</tt> playing the role of <tt>(y(i) - sigmoid(theta'*x(:,i)))</tt>, and <tt>A</tt> playing the role of <tt>x</tt>. We can derive a fast implementation as follows: |
<syntaxhighlight lang="matlab"> | <syntaxhighlight lang="matlab"> | ||
% Implementation 3 | % Implementation 3 |