Advertisement
Guest User

Untitled

a guest
May 24th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. # Config
  2.  
  3. learningRate <- 0.000001 # alpha
  4. maxIterations <- 10000000 # abort after n iterations
  5. maxThetaDifference <- 0.000001 # assume t in theta is close enough if diff after iter < than this
  6. data <- read.csv("data1.tsv", sep = "\t", header = FALSE)
  7.  
  8. hypothesis <- function(theta, x){
  9. return(theta[1] + theta[2] * x)
  10. }
  11.  
  12. loss <- function(theta, x, y){
  13. return (sum((hypothesis(theta, x) - y) ^ 2)) / (2 * length(x))
  14. }
  15.  
  16. # Set initial values
  17. theta <- c(0, 0)
  18. thetaFinished <- rep(FALSE, length(theta)) # For each theta, contains true iff approximation is complete
  19. iterations <- 0
  20. differences <- c() # Keeps track of the changes of all thetas
  21. losses <- c()
  22.  
  23. while (!all(thetaFinished) && iterations < maxIterations) {
  24. oldTheta <- theta # Copy theta
  25. predictions <- hypothesis(oldTheta, data[, 1]) # Compute vector of current predictions
  26. actual <- data[, 2] # Get actual y-values
  27.  
  28. # x_0 = 1
  29. theta[1] <- oldTheta[1] - learningRate * sum(predictions - actual) / nrow(data)
  30. thetaFinished[1] = TRUE
  31.  
  32. # Iterate over all other thetas, apply descent
  33. for (t in 2:length(theta)) {
  34. if (thetaFinished[t]) next
  35.  
  36. # As per slide 46:
  37. theta[t] <- oldTheta[t] - learningRate * sum((predictions - actual) * data[, t - 1]) / nrow(data)
  38. thetaFinished[t] = abs(oldTheta[t] - theta[t]) < maxThetaDifference
  39. }
  40.  
  41. iterations <- iterations + 1
  42. differences <- c(differences, sum(abs(theta - oldTheta))) # Append total sum of differences
  43. losses <- c(losses, loss(theta, data[, 1], data[, 2])) # Append new loss
  44. }
  45.  
  46. plot(data[, 1], data[, 2], col = "red", main = "Actual data (red), regression line (green)")
  47. lines(data[, 1], hypothesis(oldTheta, data[, 1]), col = "green")
  48.  
  49. dev.new()
  50. plot(differences, main = "Sum of theta differences over iterations")
  51.  
  52. dev.new()
  53. plot(losses, main = "Loss over iterations")
  54.  
  55. print("Theta =")
  56. print(theta)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement