Created
November 10, 2013 00:11
-
-
Save neomatrix369/7391848 to your computer and use it in GitHub Desktop.
Gradient descent in R (using the cost function)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| assertThat <- function(actualValue, expectedValue) { | |
| if (as.character(actualValue) == as.character(expectedValue)) { | |
| print("TEST PASSED: actual and expected values match.") | |
| } else { | |
| print("TEST FAILED: actual and expected values do not match.") | |
| } | |
| } | |
| is <- function(value) { | |
| return(value) | |
| } | |
| equalTo <- is | |
| source('./supportingFunctions.r') | |
| houseSizes <- c( 2104, 1416, 1534, 852) ## x | |
| housePrices <- c(460000, 232000, 315000, 178000) ## y | |
| houseSizes <- c( 2, 3, 4, 5) ## x | |
| housePrices <- c( 1, 2, 3, 4) ## y | |
| theta0 <- 0 ### expected 50 | |
| theta1 <- 0 ### expected 0.06 | |
| ZERO <- 0 | |
| numberOfItemsInTheTrainingSet <- function(inTrainingSet) { | |
| return(length(inTrainingSet)) | |
| } | |
| # | |
| # Cost function: Linear regression model | |
| # | |
| # hypothesis: h_thetaOf_x = theta0 + (theta1 * x) | |
| # parameters: theta0, theta1 | |
| # J_of_theta0_theta1 = (1 / 2m) * sumOf(1..m)Of(h_thetaOf_x_i - y_i) square | |
| # | |
| costFunctionHypothesis <- function(inTheta0, inTheta1, inX) { | |
| h_theta_Of_x <- (inTheta0 + (inTheta1 * inX)) | |
| return(h_theta_Of_x) | |
| } | |
| costFunction <- function(inTheta0, inTheta1, inX, inY) { | |
| m <- numberOfItemsInTheTrainingSet(inX) | |
| partA <- 1 / (2 * m) | |
| result <- 0 | |
| for(i in c(1: m)) { | |
| x <- inX[i] | |
| y <- inY[i] | |
| partB <- (costFunctionHypothesis(inTheta0, inTheta1, x) - y) | |
| result <- result + (partB ^ 2) | |
| } | |
| result <- partA * result | |
| return(result) | |
| } | |
| derivativeOfCostFunctionTheta0 <- function(inTheta0, inTheta1, inX, inY) { | |
| m <- numberOfItemsInTheTrainingSet(inX) | |
| partA <- 1 / m | |
| result <- 0 | |
| for(i in c(1: m)) { | |
| x <- inX[i] | |
| y <- inY[i] | |
| partB <- (costFunctionHypothesis(inTheta0, inTheta1, x) - y) | |
| result <- result + partB | |
| } | |
| result <- partA * result | |
| return(result) | |
| } | |
| derivativeOfCostFunctionTheta1 <- function(inTheta0, inTheta1, inX, inY) { | |
| m <- numberOfItemsInTheTrainingSet(inX) | |
| partA <- 1 / m | |
| result <- 0 | |
| for(i in c(1: m)) { | |
| x <- inX[i] | |
| y <- inY[i] | |
| partB <- (costFunctionHypothesis(inTheta0, inTheta1, x) - y) * x | |
| result <- result + partB | |
| } | |
| result <- partA * result | |
| return(result) | |
| } | |
| convergeGradientDecent <- function() { | |
| alpha <- 0.1 | |
| newTheta0 <- 5 | |
| newTheta1 <- 5 | |
| ctr <- 0 | |
| while( ((newTheta0 - theta0) != ZERO) && ((newTheta1 - theta1) != ZERO)) { | |
| theta0 <- newTheta0 | |
| theta1 <- newTheta1 | |
| print(paste("theta0:", theta0, "theta1:", theta1, "newTheta0:", newTheta0, "newTheta1:", newTheta1)) | |
| derivativeValueTheta0 <- derivativeOfCostFunctionTheta0(theta0, theta1, houseSizes, housePrices) | |
| newTheta0 <- newTheta0 - (alpha * derivativeValueTheta0) | |
| derivativeValueTheta1 <- derivativeOfCostFunctionTheta1(theta0, theta1, houseSizes, housePrices) | |
| newTheta1 <- newTheta1 - (alpha * derivativeValueTheta1) | |
| #print(paste("(theta0-newTheta0)^2:", abs(theta0-newTheta0)^2, "(theta1-newTheta1)^2:", abs(theta1-newTheta1)^2)) | |
| #result <- costFunction(newTheta0, newTheta1, houseSizes, housePrices) | |
| #print(paste("Original cost function:", result)) | |
| print(paste("derivativeValueTheta0:", derivativeValueTheta0, "derivativeValueTheta1:", derivativeValueTheta1)) | |
| ctr <- ctr + 1 | |
| #if (ctr > 10000) { | |
| # break | |
| #} | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment