# NOT RUN {
### Example regression with randomly-generated data
library(stochQN)
### Will sample data y ~ Ax + epsilon
true_coefs <- c(1.12, 5.34, -6.123)
generate_data_batch <- function(true_coefs, n = 100) {
X <- matrix(
rnorm(length(true_coefs) * n),
nrow=n, ncol=length(true_coefs))
y <- X %*% true_coefs + rnorm(n)
return(list(X = X, y = y))
}
### Regular regression function that minimizes RMSE
eval_fun <- function(coefs, X, y, weights=NULL, lambda=1e-5) {
pred <- as.numeric(X %*% coefs)
RMSE <- sqrt(mean((pred - y)^2))
reg <- 2 * lambda * as.numeric(coefs %*% coefs)
return(RMSE + reg)
}
eval_grad <- function(coefs, X, y, weights=NULL, lambda=1e-5) {
pred <- X %*% coefs
grad <- colMeans(X * as.numeric(pred - y))
grad <- grad + 2 * lambda * as.numeric(coefs^2)
return(grad)
}
pred_fun <- function(X, coefs, ...) {
return(as.numeric(X %*% coefs))
}
### Initialize optimizer form arbitrary values
x0 <- c(1, 1, 1)
optimizer <- adaQN(x0, grad_fun=eval_grad,
pred_fun=pred_fun, obj_fun=eval_fun, initial_step=1e-0)
val_data <- generate_data_batch(true_coefs, n=100)
### Fit to 50 batches of data, 100 observations each
for (i in 1:50) {
set.seed(i)
new_batch <- generate_data_batch(true_coefs, n=100)
partial_fit(
optimizer,
new_batch$X, new_batch$y,
lambda=1e-5)
x_curr <- get_curr_x(optimizer)
i_curr <- get_iteration_number(optimizer)
if ((i_curr %% 10) == 0) {
cat(sprintf(
"Iteration %d - E[f(x)]: %f - values of x: [%f, %f, %f]\n",
i_curr,
eval_fun(x_curr, val_data$X, val_data$y, lambda=1e-5),
x_curr[1], x_curr[2], x_curr[3]))
}
}
### Predict for new data
new_batch <- generate_data_batch(true_coefs, n=10)
yhat <- predict(optimizer, new_batch$X)
# }
Run the code above in your browser using DataLab