Processing math: 100%

Study Note of Machine Learning (I)

Chap 1 - Introduction

What is Machine Learning?

Definition by Arthur Samuel(1959): “Field of study that gives computers the ability to learn without being explicitly programmed.”
Definition by Tom Mitchell(1998): “A computer program is said to learn form experience E with respect to some task T and some performance measure P, if its performance on T, as measured by P, improves with experience E.”

In general, any ML problem can be assigned to one of two broad classification: supervised learning or unsupervised learning.

Supervised Learning

  • Supervised learning is the ML task of learning a way to map input signals to output values by training a model on a set of training examples where each example is a pair consisting of an input and a desired output value.
    • Alternatively, we know the relationship between the input and output.
  • Regression Problem: try to predict results within a continuous output (ie, map input variables to some continuous function)
    • E.g. House Price Prediction
  • Classification Problem: try to predict results in a discrete output (ie, map input variables into discrete categories)
    • E.g. Cancer Categorization (malignant/benign tumor), Email spam/not spam.

Unsupervised Learning

  • Unsupervised ML is the ML task of inferring a function to describe hidden structure from “unlabeled” data. Examples of unsupervised learning tasks include clustering (where we try to discover underlying groupings of examples) and anomaly detection (where we try to infer if some examples in a dataset do not conform to some expected pattern).
    • Alternatively, we have little or no idea what our results should look like.
  • There is no feedback based on the prediction results with unsupervised learning.
    • E.g.: Orgnize computing clusters, Social network analysis, Market segmentation, Astronomical data analysis
  • Clustering Problem Example: Automatically group a huge amount of genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on.
  • Non-clustering Problem Example: The “Cocktail Party Algorithm”, allows you to find structure in a chaotic environment.
    • Source separation: Cocktail party problem [W,s,v]=svd((repmat(sum(x.*x,1),size(x,1),1).*x)*x');
    • GNU Octave, a great PL for algorithm prototyping

Model and Cost Function

Model Representation

In regression problems, we are taking input variables and trying to fit the output onto a continuous expected result function.
Linear regression with one variable is also known as “univariate linear regression.”
Univariate linear regression is used when predicts a single output value y from a single input value x.

Notation

  • m = number of training examples
  • xs = “input” variable / features
  • ys = “output” variable / “target” variable
  • (x,y) = one training example
  • (x(i),y(i))=i(th) training example
  • θi=ith parameter of model

Hypothesis function: ˆy=hθ(x)=θ0+θ1x

Note that this is like the equation of a straight line. We give to hθ(x) values for θ0 and θ1 to get our estimated output ˆy. In other words, we are trying to create a function called hθ that is trying to map our input data (the xs) to the output data (the ys))

Parameter Learning

Cost Function

The accuracy of hypothesis function can be measured by a cost function. This takes an “average” of all the results of the hypothesis with inputs from xs compared to the actual output ys.

Cost Function: J(θ0,θ1)=12mmi=1(ˆyiyi)2=12mmi=1(hθ(xi)yi)2

This function is also known as “Squared Error Function”. - Please refer to Squared Error Function (aka, Mean squared error)

Gradient Descent

After having a hypothesis function and a cost function to measure hypothesis function, now we uses Gradient Descent to estimate the parameters in hypothesis function.

Imagine that we graph our hypothesis function based on its fields θ0 and θ1 (actually we are graphing the cost function as a function of the parameter estimates). This can be kind of confusing; we are moving up to a higher level of abstraction. We are not graphing x and y itself, but the parameter range of our hypothesis function and the cost resulting from selecting particular set of parameters.

We put θ0 on the x axis and θ1 on the y axis, with the cost function on the vertical z axis. The points on our graph will be the result of the cost function using our hypothesis with those specific theta parameters.

We will know that we have succeeded when our cost function is at the very bottom of the pits in our graph, i.e. when its value is the minimum.

The way we do this is by taking the derivative (the tangential line to a function) of our cost function. The slope of the tangent is the derivative at that point and it will give us a direction to move towards. We make steps down the cost function in the direction with the steepest descent, and the size of each step is determined by the parameter α, which is called the learning rate.

The gradient descent algorithm is:
repeat until convergence: {θj:=θjαθjJ(θ0,θ1)}

alt_text

3d-gradient-descent

Gradient Descent for Linear Regression

Algorithm:
repeat until convergence: {θ0:=θ0α1mmi=1(hθ(xi)yi)θ1:=θ1α1mmi=1((hθ(xi)yi)xi)}

Facts

  • “Batch” Gradient Descent: Each step of gradient descent uses all the training examples.
  • Gradient descent can converge even if α is kept fixed. (But α cannot be too large, or else it may fail to converge).
  • For the specific choice of cost function J used in linear regression, there are no local optima (other than the global optimum).

Chap 2 - Linear Regression with Multiple Variables

Linear regression with multiple variables is also known as “multivariate linear regression”.

We now introduce notation for equations where we can have any number of input variables.

x(i)j=value of feature j in the ith training examplex(i)=the column vector of all the feature inputs of the ith training examplem=the number of training examplesn=|x(i)|;(the number of features)

Now define the multivariable form of the hypothesis function as follows, accommodating these multiple features:
hθ(x)=θ0+θ1x1+θ2x2+θ3x3++θnxn

In order to develop intuition about this function, we can think about θ0 as the basic price of a house, θ1 as the price per square meter, θ2as the price per floor, etc. x1 will be the number of square meters in the house, x2 the number of floors, etc.

Using the definition of matrix multiplication, our multivariable hypothesis function can be concisely represented as:
hθ(x)=[θ0θ1...θn][x0x1xn]=θTx

Remark: Note that for convenience reasons in this course Mr. Ng assumes x(i)0=1 for (i1,,m)

The training examples are stored in X row-wise, like such:
X=[x(1)0x(1)1x(2)0x(2)1x(3)0x(3)1],θ=[θ0θ1]

You can calculate the hypothesis as a column vector of size (m x 1) with: hθ(X)=Xθ

For the rest of these notes, X will be represent a matrix of training examples x(i) stored row-wise.

Cost Function

For the parameter θ (of type R(n+1)or R(n+1)×1),
cost function: J(θ)=12mmi=1(hθ(x(i))y(i))2

The vectorized version: J(θ)=12m(Xθy)T(Xθy)

Gradient Descent for Multiple Variables

The gradient descent equation itself is generally the same form; we just have to repeat it for our ‘n’ features:
repeat until convergence:{θ0:=θ0α1mmi=1(hθ(x(i))y(i))x(i)0θ1:=θ1α1mmi=1(hθ(x(i))y(i))x(i)1θ2:=θ2α1mmi=1(hθ(x(i))y(i))x(i)2}

In other words:
repeat until convergence:{θj:=θjα1mmi=1(hθ(x(i))y(i))x(i)jfor j := 0..n}

Matrix Notation

The Gradient Descent rule can be expressed as:
θ:=θαJ(θ)

where J(θ) is a vector of the form:
J(θ)=[J(θ)θ0J(θ)θ1J(θ)θn]

The j-th component of the gradient is the summation of the product of two terms:
J(θ)θj=1mmi=1(hθ(x(i))y(i))x(i)j=1mmi=1x(i)j(hθ(x(i))y(i))

Sometimes, the summation of the product of two terms can be expressed as the product of two vectors.

Here, x(i)j for i = 1, 2, … m, represents the m elements of the j-th columns, xj, of the training set X.
The other term (hθ(x(i))y(i)) is the vector of the deviation between the prediction hθ(x(i)) and the true values y(i). Rewriting J(θ)θj, we have:

J(θ)θj=1mxjT(Xθy)J(θ)=1mXT(Xθy)

The vectorized matrix notation: θ:=θαmXT(Xθy)

Feature Normalization

We can speed up gradient descent by having each of our input values in roughly the same range. This is because θ will descend quickly on small ranges and slowly on large ranges, and so will oscillate inefficiently down to the optimum when the variables are very uneven.

The way to prevent this is to modify the ranges of our input variables so that they are all roughly the same. Ideally:

−1 ≤ x(i) ≤ 1 or −0.5 ≤ x(i) ≤ 0.5

These aren’t exact requirements; we are only trying to speed things up. The goal is to get all input variables into roughly one of these ranges, give or take a few.

Two techniques to help with this are feature scaling and mean normalization. Feature scaling involves dividing the input values by the range (i.e. the maximum value minus the minimum value) of the input variable, resulting in a new range of just 1. Mean normalization involves subtracting the average value for an input variable from the values for that input variable, resulting in a new average value for the input variable of just zero. To implement both of these techniques, adjust your input values as shown in this formula:

xi:=xiμisi

Where μi is the average of all the values for feature (i) and si is the range of values (max - min), or si is the standard deviation.

Note that dividing by the range, or dividing by the standard deviation, give different results. The quizzes in this course use range - the programming exercises use standard deviation.

Example: xi is housing prices with range of 100 to 2000, with a mean value of 1000. Then, xi:=price10001900

Gradient Descent Tips

  • Debugging gradient descent: Make a plot with number of iterations on the x-axis. Now plot the cost function, J(θ) over the number of iterations of gradient descent. If J(θ) ever increases, then you probably need to decrease α.
  • Automatic convergence test: Declare convergence if J(θ) decreases by less than E in one iteration, where E is some small value such as 103. However in practice it’s difficult to choose this threshold value.

It has been proven that if learning rate α is sufficiently small, then J(θ) will decrease on every iteration. Andrew Ng recommends decreasing α by multiples of 3.

Features and Polynomial Regression

We can improve our features and the form of our hypothesis function in a couple different ways.

We can combine multiple features into one. For example, we can combine x1 and x2 into a new feature x3 by taking x1x2.

Polynomial Regression Our hypothesis function need not be linear (a straight line) if that does not fit the data well.

We can change the behavior or curve of our hypothesis function by making it a quadratic, cubic or square root function (or any other form).

For example, if our hypothesis function is hθ(x)=θ0+θ1x1 then we can create additional features based on x1, to get the quadratic function hθ(x)=θ0+θ1x1+θ2x21 or the cubic function hθ(x)=θ0+θ1x1+θ2x21+θ3x31. In the cubic version, we have created new features x2=x21 and x3=x31.

To make it a square root function, we could do: hθ(x)=θ0+θ1x1+θ2x1

One important thing to keep in mind is, if you choose your features this way then feature scaling becomes very important.

eg. if x1 has range 1 - 1000 then range of x21 becomes 1 - 1000000 and that of x31 becomes 1 - 1000000000.

Normal Equation

The “Normal Equation” is a method of finding the optimum theta without iteration:
θ=(XTX)1XTy

Remark: There is no need to do feature scaling with the normal equation. Mathematics proofs: link 1, link 2.

The following is a comparison of gradient descent and the normal equation:

Gradient Descent Normal Equation
Need to choose α No need to choose α
Need many iterations No need to iterate
O(kn2) O(n3), need to calculate inverse of XTX
Works well when n is large slow if n is large

Remark: n10000

Normal Equation Non-invertibility

When implementing the normal equation in octave we want to use the ‘pinv’ function rather than ‘inv.’ XTX may be non-invertible. The common causes are:

  • Redundant features, where two features are very closely related (i.e. they are linearly dependent)
  • Too many features (e.g. mn). In this case, delete some features or use “regularization” (to be explained in a later lesson).

Reference

0%