Machine Learning: Difference between revisions

From Adam Colton
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 227: Line 227:
When available, use linear algebra libraries rather than reinventing the wheel.
When available, use linear algebra libraries rather than reinventing the wheel.


...then there was a lot of math and I had to go lay down for a while.
  h<sub>&Theta;</sub>(x) = &sum; &Theta;<sub>j</sub>x<sub>j</sub> = &Theta;<sup>T</sup>x
where &Theta; is your feature vector. With the vectorized implementation - there's a for-loop (about 4 lines). The vector is a single line, and probably more optimized.
 
Note: Octave is 1 base index.
 
For gradient descent
  &Theta; := &Theta; - &alpha;&delta;
  &delta; = (1/m) &sum;<sup>m</sup> (h<sub>&Theta;</sub>(x<sup>i</sup>) - y<sup>i</sup>)x<sup>i</sup>


=== Working on and Submitting Programming Exercises ===
=== Working on and Submitting Programming Exercises ===
Line 235: Line 242:
=== Classification ===
=== Classification ===
Logistic Regression is meant for classification problems.
Logistic Regression is meant for classification problems.
Classification is grouping into finite, discreet labels.
In {0,1}, 0 is 'negative class', and 1 is 'positive class'. This is binary classification.
Applying linear regression to classification can work very poorly - increasing the data can actually make the model worse.
Using logistic regression 0 &le; h<sub>&Theta;</sub>(x) &le; 1


=== Hypothesis Representation ===
=== Hypothesis Representation ===
We will be using the logistic function
We will be using the logistic function (also known as sigmoid function)
   g(z) = 1 / (1 + e<sup>-z</sup>)
   g(z) = 1 / (1 + e<sup>-z</sup>)


We will use it as
We will use it as
   h<sub>&Theta;</sub>(x) = g(&Theta)<sup>T</sup>x)
   h<sub>&Theta;</sub>(x) = g(&Theta;<sup>T</sup>x)
which is the same as
which is the same as
   h<sub>&Theta;</sub>(x) = 1 / (1 + e<sup>&Theta;<sup>T</sup>x</sup>)
   h<sub>&Theta;</sub>(x) = 1 / (1 + e<sup>&Theta;<sup>T</sup>x</sup>)
Line 254: Line 269:


=== Decision Boundary ===
=== Decision Boundary ===
There's lines, they're boundaries. One side is 1 or yes or true, the other is none of those things.
There's lines, they're boundaries. One side is 1 or yes or true, the other is none of those things. This is defined by something like "y=1 if h<sub>&Theta;</sub>(x) &ge; 0.5"


We can include non-linear attributes allowing the linear regression to use them for classification (bulls-eye example).
We can include non-linear attributes allowing the linear regression to use them for classification (bulls-eye example).


=== Cost Function ===
=== Cost Function ===
Cost function for logistic regression, Cost(h<sub>&Theta;</sub>(x), y)
Cost function for logistic regression is
  Cost(h<sub>&Theta;</sub>(x), y)
where h<sub>&Theta;</sub>(x) is the prediction made using parameter vector &Theta; and y is the actual value for x from the training data.
 
We can't use the same function we used for linear regression because it is non-convex and therefor cannot be used with gradient descent.
   if y = 1 : -log(h<sub>&Theta;</sub>(x))
   if y = 1 : -log(h<sub>&Theta;</sub>(x))
   if y = 0 : -log(1 - h<sub>&Theta;</sub>(x))
   if y = 0 : -log(1 - h<sub>&Theta;</sub>(x))
Line 270: Line 289:


   J(&Theta;) = (-1/m) &Sigma;<sup>m</sup> -y<sub>i</sub>(log( h<sub>&Theta;</sub>(x<sub>i</sub>) ) - (1-y<sub>i</sub>) log( 1 - h<sub>&Theta;</sub>(x<sub>i</sub>) )
   J(&Theta;) = (-1/m) &Sigma;<sup>m</sup> -y<sub>i</sub>(log( h<sub>&Theta;</sub>(x<sub>i</sub>) ) - (1-y<sub>i</sub>) log( 1 - h<sub>&Theta;</sub>(x<sub>i</sub>) )
   h<sub>&Theta;</sub>(x) = 1 / ( 1 + e<sup>&Theta<sup>T</sup>x</sup>)
   h<sub>&Theta;</sub>(x) = 1 / ( 1 + e<sup>&Theta;<sup>T</sup>x</sup>)
 
Taking the derivative (note: what derivative) we get the gradient descent function
  &Theta;<sub>j</sub> := &Theta;<sub>j</sub> - &alpha; &Sigma;<sup>m</sup> ( h<sub>&Theta;</sub>(x<sub>i</sub>) - y<sub>i</sub>)x<sub>i,j</sub>
This ends up being exactly the same as for linear regression.


=== Advanced Optimization ===
=== Advanced Optimization ===
Given a way to compute J(&Theta;) and it's partial derivative, there are many options that can find a optimum, not just gradient descent.
* Conjugate gradient
* BFGS
* L-BFGS
None of these need &alpha; and they will all (often) converge faster than gradient descent. But they are more complex. But they can be used without fully understanding them.
Example of this being used in Octave. To use it, you need a function that returns cost and gradient as a function of theta.
=== Multiclass Classification: Ove-vs-all ===
=== Multiclass Classification: Ove-vs-all ===


[[Category:Notes]]
[[Category:Notes]]
[[Category:Course Work]]
[[Category:Course Work]]

Latest revision as of 15:11, 24 November 2013

Week 1 - Intro

What is Machine Learning

  • Checkers playing computer (Arthur Samuel)

Supervised Learning

Right answer is provided. Types of problems

  • Regression
  • Classification

Unsupervised Learning

The data has no "correct answer"

Cocktail party algorithm

Week 1 - Linear Regression with One variable

Model Representation

Working with housing prices in Portland as our dataset.

Symbols used

  • m - number of training examples
  • x - input variables
  • y - output variable
  • h - hypothesis (function h(x) = y)

Univariate linear regression

Cost Function

hΘ(x) = Θ0 + Θ1x

sum of square error as our cost function. He's using 1/2m rather than 1/m, I'm not sure why.

Cost Function - Intuition : 1

Demo of technique.

Cost Function - Intuition : 2

Contour plots.

Gradient Descent

Θj = Θj - α (d/dΘj) * J(Θ0, Θ1)

Gradient Descent Intuition

Explaining gradient descent.

Gradient Descent for Linear Regression

  • Θ0 = Θ0 - α(1/m) Σm (hΘ(xi) - yi)
  • Θ1 = Θ1 - α(1/m) Σm (hΘ(xi) - yi) * xi

Linear Regression with Multiple Variables - Week 2

Multiple Features

Gradient Descent for Multiple Variables

  • Parameters: Θ0,Θ1,...,Θn
  • Cost Function: J( Θ0,Θ1,...,Θn ) or simply J(Θ)

Much like before, gradient descent is calculated for all Θs before being applied. To make the formula more uniform we set x0,i = 1 for all i then the formula becomes

 Θj = Θj - (α/m) * Σi=1m (h(Θ, xi) - yi) * xi,j

Note: I may have my notation for rows and columns mixed up in the formula - double check before using.

Gradient Descent in Practice 1 - Feature Scaling

Try to make features on a similar scale. In the house example, if our features are size 0-2000ft2 and bedrooms (1-5), it will take a long time to converge, because the first feature is three orders of magnitude larger. Either scale bedrooms up or the ft2 down. A simple way to do this is scale all features filling the range (-1, 1). If future data lies outside this range, it is not a problem. If future data falls way outside this range, you'll have bigger problems.

Try and set the mean to 0. This is "mean normalization".

Of course, none of this applies to x0 = 1.

Gradient Descent in Practice 2 - Learning Rate

The correct α should cause J to decrease continuously. There is not a good automatic way to choose α at this time, but by looking at the iterations graph you can see that the slope is decreasing quickly enough, but not increasing.

Features and Polynomial Regression

You are free to make new features from the features you are given. This includes taking the root, log or square of a feature. Continue to use scaling.

Normal Equation

Using matrices to solve the equation. Feature scaling is not necessary using the normal method.

Gradient descent does not work as well as the number of features becomes very large (because it has an N^2 component). Somewhere around 10K features is a good point to cross over and start using gradient descent.

Normal Equation Noninvertibility

Some matrices are non-invertible.

Octave Tutorial - Week 2

Basic Operations

  • ~= Not equal

Semi-colon suppresses echo

To define a matrix

 A = [1 2; 3 4; 5 6]

so the delimiter between cols is just a space and the delimiter between rows is semi-colon.

A vector range can be defined with

 V = start:step:stop

such as

 V = 1:1:5

Giving us the same as

 V = [1 2 3 4 5]

note the inclusivity.

We can generate a matrix of all ones with

 M = ones(h,w)

with w be width and h being height. Also works with zeros, rand (uniform distribution), randn (normal distribution).

Similarly we can do

 eye(x)

producing a square identity matrix.

Print a histogram (bar graph) with

 hist(v[, d])

where v is a vector and the optional d is the number of divisions (bars).

Finally,

 help command

Moving Data Around

  • size(m) : returns size of V as a 1x2 matrix
    • size(m,1) : returns just the number of rows
    • size(m,2) : returns just the number of columns
  • length(m) : returns the largest dimension, normally reserved for vectors
  • pwd : present working directory
  • cd : change directory
  • ls : list
  • load(filename) : loads a file - I think it needs to be .dat, will create a variable with the name of the file
  • who : shows the variables
    • whos: detailed view
  • clear var : deletes a variable
  • save filename.mat var : saves a variable file in a binary format (matlab)
    • save filename.txt var -ascii : saves in human readable format

Grab a subset of a vector

 vss = v(1:10)

To get a specific value from a matrix

 v = A(3,2)

gets the value at 3,2 in A

To get a row

 A(3,:)

to get a col

 A(:,1)

to get specific rows (in this case rows 1 and 3)

 A([1 3], :)

you can also do assignmetns that way

 A(:, 2) = [10; 11; 12]

To add a column

 A = [A, [5; 6; 7]]

Matrices can be combined horizontally [A B] or vertically [A; B].

Computing on Data

  • PS1(prompt) : Reset the prompt
 A .* B

Elementwise multiplication

 A.^2

Elementwise squaring

 1 ./ A

Elementwise reciprocal

 A'

Transpose

 max(V)
 [v,i] = max(V)

Returns the max value in V, in the second line it returns both the max and the index

 A < 3

Returns a matrix of bools indicating if each position-wise value was less than 3.

 magic(3)

Returns a 3x3 magic square.

 find(A >= 7)

returns all the indices of values greater than or equal to 7.

Plotting Data

 x = [0:0.1:0.98];
 s = sin(2*pi*4*x);
 c = cos(2*pi*4*x);
 plot(x,s);
 hold on;
 plot(x,c, 'r');
 xlabel('X');
 ylabel('Squiggles');
 legend('sin', 'cos');
 title('Sin/Cos plot');
 print -dpng 'myPlot.png';

This will plot sin and cosin from 0 to 0.98 on the same graph with cos being graphed in red. Also labels the axis and prints a legend matching the color to the legend label. Finally, saves the plot as a png.

To clear the graph, just type 'close'. We can specify different figures by numbering them

 figure(1); plot(x, s);
 figure(2); plot(x, c);

You can do side by side plots with subplot

 subplot(1,2,1);
 plot(x, s)
 subplot(1,2,2);
 plot(x,c);

Neat command

 imagesc(M), colorbar, colormap gray;

plots the matrix M as blocks of gray-scale colors and includes a legend for reference.

Control Statements: for, while, if statements

 for i=1:10,
   v(i) = 2^i;
 end;

You can also use a vector (or matrix?) in place of 1:10.

 i=1;
 while i <= 5,
   v(i) = 100*i;
   i = i+1;
 end;

Also supports break

 if i==6,
   i=3
 elseif i==5,
   i=6
 else
   i=5
 end;

Octave will automatically search the pwd for functions. To add a directory to search

 addpath(path)

Vectorization

When available, use linear algebra libraries rather than reinventing the wheel.

 hΘ(x) = ∑ Θjxj = ΘTx

where Θ is your feature vector. With the vectorized implementation - there's a for-loop (about 4 lines). The vector is a single line, and probably more optimized.

Note: Octave is 1 base index.

For gradient descent

 Θ := Θ - αδ
 δ = (1/m) ∑m (hΘ(xi) - yi)xi

Working on and Submitting Programming Exercises

To submit, just type submit()

Logistic Regression - Week 3

Classification

Logistic Regression is meant for classification problems.

Classification is grouping into finite, discreet labels.

In {0,1}, 0 is 'negative class', and 1 is 'positive class'. This is binary classification.

Applying linear regression to classification can work very poorly - increasing the data can actually make the model worse.

Using logistic regression 0 ≤ hΘ(x) ≤ 1

Hypothesis Representation

We will be using the logistic function (also known as sigmoid function)

 g(z) = 1 / (1 + e-z)

We will use it as

 hΘ(x) = g(ΘTx)

which is the same as

 hΘ(x) = 1 / (1 + eΘTx)

The logistic or sigmoid function is S shaped

  • g(-∞) = 0
  • g(∞) = 1
  • g(0) = 0.5

The output of hΘ(x) is interpreted as a probability that the actually classification of x is 1, or it can be interpreted as the probability that they hypothesis x is true. Expressed as

 hΘ(x) = P(y=1 | x; Θ)

Decision Boundary

There's lines, they're boundaries. One side is 1 or yes or true, the other is none of those things. This is defined by something like "y=1 if hΘ(x) ≥ 0.5"

We can include non-linear attributes allowing the linear regression to use them for classification (bulls-eye example).

Cost Function

Cost function for logistic regression is

 Cost(hΘ(x), y)

where hΘ(x) is the prediction made using parameter vector Θ and y is the actual value for x from the training data.

We can't use the same function we used for linear regression because it is non-convex and therefor cannot be used with gradient descent.

 if y = 1 : -log(hΘ(x))
 if y = 0 : -log(1 - hΘ(x))

This function has a zero cost when it's correct and an infinite cost if it predicts 1.0 and the right answer is 0.0.

Simplified Cost Function and Gradient Descent

 Cost(hΘ(x), y) = -y(log( hΘ(x) ) - (1-y) log( 1 - hΘ(x) )

This cost function is convex and (for complex reasons) efficient.

 J(Θ) = (-1/m) Σm -yi(log( hΘ(xi) ) - (1-yi) log( 1 - hΘ(xi) )
 hΘ(x) = 1 / ( 1 + eΘTx)

Taking the derivative (note: what derivative) we get the gradient descent function

 Θj := Θj - α Σm ( hΘ(xi) - yi)xi,j

This ends up being exactly the same as for linear regression.

Advanced Optimization

Given a way to compute J(Θ) and it's partial derivative, there are many options that can find a optimum, not just gradient descent.

  • Conjugate gradient
  • BFGS
  • L-BFGS

None of these need α and they will all (often) converge faster than gradient descent. But they are more complex. But they can be used without fully understanding them.

Example of this being used in Octave. To use it, you need a function that returns cost and gradient as a function of theta.

Multiclass Classification: Ove-vs-all