Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Benchmark code
// define inside file timetest.m
function blank = timetest(x, y, MAX_ITR)
m = length(y);
x = [ones(m, 1) x];
theta = zeros(size(x(1,:)))'; % initialize fitting parameters
alpha = 0.07;
for num_iterations = 1:MAX_ITR
grad = (1/m).* x' * ((x * theta) - y);
theta = theta - alpha .* grad;
endfor
endfunction
x = load('/tmp/ex2x.dat'); y = load('/tmp/ex2y.dat');
runtimes = [];
for i = 1:100
t = cputime; timetest(x, y, 1500); e = cputime-t
runtimes = [runtimes e]
endfor
google.spreadsheet <- function (key) {
library(RCurl)
# ssl validation off
ssl.verifypeer <- FALSE
tt <- getForm("https://spreadsheets.google.com/spreadsheet/pub",
hl ="en_GB",
key = key,
single = "true", gid ="0",
output = "csv",
.opts = list(followlocation = TRUE, verbose = TRUE))
read.csv(textConnection(tt), header = TRUE)
}
mydata = google.spreadsheet("0AnypY27pPCJydDB4N3MxM0tENlk3UElnZ013cW1iM3c")
timetask <- function (mydata, iterations) {
alpha = 0.07
m = length(mydata$x)
theta = matrix(c(0,0), nrow=1)
x = matrix(c(rep(1,m), mydata$x), ncol=2)
y = matrix(mydata$y, ncol=1)
delta = function(x,y,th) {
delta = (t(x) %*% ((x %*% t(th)) - y))
return(t(delta))
}
for (i in 1:iterations) {
theta = theta - alpha * 1/m * delta(x,y,theta)
}
}
runtimes = c()
for (i in 1:100) {
runtime = system.time(
timetask(mydata, 1500)
)
runtimes = if (is.null(runtimes)) c(runtime["user.self"]) else c(runtimes, runtime["user.self"])
}
// iterative version
val x = io.Source.fromFile("/tmp/ex2x.dat").getLines.toList map { x => List(1.0, x.toDouble) }
val y = io.Source.fromFile("/tmp/ex2y.dat").getLines.toList map { y => y.toDouble }
def timetestIterative(x: List[List[Double]], y: List[Double], iterations: Int = 1500) = {
val m = x.size
val alpha = 0.07
var theta = List(0.0, 0.0)
// linear regression model
val h: List[Double] => Double = { x => theta(0)*x(0) + theta(1)*x(1) }
// sum the difference (error) for each xy pair
// using the current values of theta
val delta: Int => Double = { idx =>
0 to m-1 map { i =>
val xi = x(i)
val yi = y(i)
(h(xi) - yi) * x(i)(idx)
} reduceLeft(_+_)
}
for (k <- 1 to iterations) {
// calculate theta values separately
val theta0 = theta(0) - (alpha * 1/m * delta(0))
val theta1 = theta(1) - (alpha * 1/m * delta(1))
theta = List(theta0, theta1)
}
}
var runtimes = 1 to 100 map { _ =>
val start = System.currentTimeMillis
timetestIterative(x, y, 15000)
val end = System.currentTimeMillis
(end - start) / 1000.
}
// linear alegebra version
val tempx = io.Source.fromFile("/tmp/ex2x.dat").getLines.toList map { x => (1.0, x.toDouble) }
val tempy = io.Source.fromFile("/tmp/ex2y.dat").getLines.toList map { y => y.toDouble }
def timetestLA(x: List[(Double, Double)], y: List[Double], iterations: Int = 1500) = {
import scalala.tensor.dense._
val x = DenseMatrix(tempx:_*)
val y = DenseVector(tempy:_*)
val m = x.numRows // slight change
val alpha = 0.07
var theta = DenseVector(0.0, 0.0)
1 to iterations foreach { _ =>
val grad = x.t * ((x * theta) - y) / m
theta = theta - (grad * alpha)
}
}
var runtimes = 1 to 100 map { _ =>
val start = System.currentTimeMillis
timetestLA(tempx, tempy, 15000)
val end = System.currentTimeMillis
(end - start) / 1000.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment