Skip to content

Commit

Permalink
add an example for gridtune
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Jul 20, 2014
1 parent a690587 commit 5f30b36
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/gridtune.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# A simple example to demonstrate the use of gridtune

using MLBase
using MultivariateStats

## prepare data

n_tr = 20 # number of training samples
n_te = 10 # number of testing samples
d = 5 # dimension of observations

theta = randn(d)
X_tr = randn(n_tr, d)
y_tr = X_tr * theta + 0.1 * randn(n_tr)
X_te = randn(n_te, d)
y_te = X_te * theta + 0.1 * randn(n_te)

## tune the model

function estfun(regcoef, bias)
s = ridge(X_tr, y_tr, regcoef; bias=bias)
return bias ? (s[1:end-1], s[end]) : (s, 0.0)
end

evalfun(m) = msd(X_te * m[1] + m[2], y_te)

r = gridtune(estfun, evalfun,
("regcoef", [1.0e-3, 1.0e-2, 1.0e-1, 1.0]),
("bias", (true, false));
ord=Reverse,
verbose=true)

best_model, best_cfg, best_score = r

## print results

θ, b = best_model
println("Best model:")
println(" θ = $(θ')"),
println(" b = $b")
println("Best config: regcoef = $(best_cfg[1]), bias = $(best_cfg[2])")
println("Best score: $(best_score)")

0 comments on commit 5f30b36

Please sign in to comment.