GithubHelp home page GithubHelp logo

ishmaelbelghazi / bigoptim Goto Github PK

View Code? Open in Web Editor NEW
9.0 5.0 4.0 15.41 MB

Large Scale Machine learning Optimization through Stochastic Average Gradient

License: Other

R 45.64% C 51.36% Makefile 0.02% C++ 2.98%

bigoptim's Introduction

BigOptim – Large Scale Finite Sums Cost functions Optimization for R

https://travis-ci.org/IshmaelBelghazi/bigoptim.svg https://coveralls.io/repos/IshmaelBelghazi/bigoptim/badge.svg?branch=master&service=github

Description

BigOptim is an R package that implements the Stochastic Average Gradient(SAG)[1] optimization method. For strongly convex problems, SAG achieves batch gradient descent convergence rates while keeping the iteration complexity of stochastic gradient descent. This allows for efficient training of machine learning algorithms with convex cost functions.

Setup

install.packages("devtools")
devtools::install_github("hadley/devtools")  ## Optional
devtools::install_github("IshmaelBelghazi/bigoptim")

Example: Fit with Linesearch

## Loading Data set
data(covtype.libsvm)
## Normalizing Columns and adding intercept
X <- cbind(rep(1, NROW(covtype.libsvm$X)), scale(covtype.libsvm$X))
y <- covtype.libsvm$y
y[y == 2] <- -1
## Setting seed
#set.seed(0)
## Setting up problem
maxiter <- NROW(X) * 10  ## 10 passes throught the dataset
lambda <- 1/NROW(X) 
sag_ls_fit <- sag_fit(X=X, y=y, lambda=lambda,
                      maxiter=maxiter, 
                      tol=1e-04, 
                      family="binomial", 
                      fit_alg="linesearch",
                      standardize=FALSE)
## Getting weights
weights <- coef(sag_ls_fit)
## Getting cost
cost <- get_cost(sag_ls_fit)

Example: Demo – Monitoring gradient norm

demo("monitoring_training")

misc/readme/grad_norm_covtype.png

Runtime comparison

Ran on intel i7 4710HQ 16G with intel MKL and compilers.

demo("run_times")

Dense dataset: Logistic regression on covertype

Logistic Regression on Covertype – 581012 sample points, 55 variables

constantlinesearchadaptiveglmnet
Cost at optimum0.5136030.5134970.5136760.513693
Gradient L2 norm at optimum0.0013610.0011200.0077130.001806
Approximate gradient L2 norm at optimum0.0017940.0001460.000214NA
Time(seconds)1.9302.3928.0578.749

Sparse dataset: Logistic regression on rcv1_train

Logistic Regression on RCV1_train – 20242 sample points, 47237 variables

constantlinesearchadaptiveglmnet
Cost at optimum0.0463390.0463390.0463390.046342
Gradient L2 norm at optimum3.892572e-074.858723e-076.668943e-107.592185e-06
Approximate gradient L2 norm at optimum3.318267e-074.800463e-072.647663e-10NA
Time(seconds)0.8140.8721.3684.372

References

[1] Mark Schmidt, Nicolas Le Roux, and Francis Bach. Minimizing Finite Sums with the Stochastic Average Gradient. arXiv:1309.2388 [cs, math, stat], September 2013. arXiv: 1309.2388. [ bib | http ]

bigoptim's People

Contributors

ishmaelbelghazi avatar tdhock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bigoptim's Issues

could not find function "get_cost"

Hey @IshmaelBelghazi nice to see that you wrote some demos. However I am getting an error when I try to run them. Maybe you need to export get_cost?

thocking@silene:~/R$ R --vanilla

R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(bigoptim)
Loading required package: Matrix
demo> demo("example_SAG", package="bigoptim")


    demo(example_SAG)
    ---- ~~~~~~~~~~~

Type  <Return>   to start : 

> library(Matrix)

> data(rcv1_train)

> X <- rcv1_train$X

> ##X <- cBind(rep(1, NROW(X), X), X)
> y <- rcv1_train$y

> n <- NROW(X)

> p <- NCOL(X)

> ## Setting seed
> ##set.seed(0)
> maxIter <- n * 20 

> lambda <- 1/n

> tol <- 0

> print("Running Stochastic average gradient with constant step size\n")
[1] "Running Stochastic average gradient with constant step size\n"

> ## -----------------------------------------------------------------------------
> ## SAG with Constant step size
> sag_constant_fit <- sag_fit(X=X, y=y, lambda=lambda, maxiter=maxIter,
+                             tol=0, fit_alg="constant", model="binomial")

> cost_constant <- get_cost(sag_constant_fit, X=X, y=y) 
Error in eval(expr, envir, enclos) : could not find function "get_cost"
> demo("example_SAG2", package="bigoptim")


    demo(example_SAG2)
    ---- ~~~~~~~~~~~~

Type  <Return>   to start : 

> ## Loading Data set
> data(covtype.libsvm)

> ## Normalizing Columns and adding intercept
> X <- cbind(rep(1, NROW(covtype.libsvm$X)), scale(covtype.libsvm$X))

> y <- covtype.libsvm$y

> y[y == 2] <- -1

> n <- NROW(X)

> p <- NCOL(X)

> ## Setting seed
> #set.seed(0)
> ## Setting up problem
> maxiter <- n * 20  ## 10 passes throught the dataset

> lambda <- 1/n 

> tol <- 1e-4

> ## -----------------------------------------------------------------------------
> ## SAG with Constant step size
> print("Running Stochastic Average Gradient with constant step size")
[1] "Running Stochastic Average Gradient with constant step size"

> sag_constant_fit <- sag_fit(X=X, y=y, lambda=lambda, maxiter=maxiter,
+                             tol=tol, family="binomial",
+                             fit_alg="constant", standardize=FALSE)

> cost_constant <- get_cost(sag_constant_fit, X, y)
Error in eval(expr, envir, enclos) : could not find function "get_cost"
In addition: Warning message:
In sag_fit(X = X, y = y, lambda = lambda, maxiter = maxiter, tol = tol,  :
  Optimisation stopped before convergence. Try incrasing maximum number of iterations
> demo("monitoring_training", package="bigoptim")


    demo(monitoring_training)
    ---- ~~~~~~~~~~~~~~~~~~~

Type  <Return>   to start : 

> suppressPackageStartupMessages(library(ggplot2))

> suppressPackageStartupMessages(library(glmnet))

> ## Loading Data set
> data(covtype.libsvm)

> ## Normalizing Columns and adding intercept
> X <- cbind(rep(1, NROW(covtype.libsvm$X)), scale(covtype.libsvm$X))

> y <- covtype.libsvm$y

> y[y == 2] <- -1

> n <- NROW(X)

> p <- NCOL(X)

> ## Setting seed
> #set.seed(0)
> ## Setting up problem
> n_passes <- 50  ## number of passses trough the dataset

> maxiter <- n * n_passes

> lambda <- 1/n 

> tol <- 0

> family <- "binomial"

> fit_algs <- list(constant="constant",
+                  linesearch="linesearch",
+                  adaptive="adaptive")

> sag_fits <- lapply(fit_algs, function(fit_alg) sag_fit(X, y,
+                                                    lambda=lambda,
+                                                    maxiter=maxiter,
+                                                    family=family,
+                                                    fit_alg=fit_alg,
+                                                    standardize=FALSE,
+                                                    tol=tol, monitor=TRUE))

> print(lapply(sag_fits, function(sag_fit) get_cost(sag_fit, X, y)))
Warning message:
In sag_fit(X, y, lambda = lambda, maxiter = maxiter, family = family,  :
  Optimisation stopped before convergence. Try incrasing maximum number of iterations
Error in print(lapply(sag_fits, function(sag_fit) get_cost(sag_fit, X,  : 
  error in evaluating the argument 'x' in selecting a method for function 'print': Error in FUN(X[[i]], ...) : could not find function "get_cost"
> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-unknown-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_CA.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_CA.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] glmnet_1.9-5        ggplot2_1.0.1       bigoptim_0.0.0.9000
[4] Matrix_1.2-1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6        lattice_0.20-31    digest_0.6.4       MASS_7.3-40       
 [5] grid_3.2.1         plyr_1.8.1         gtable_0.1.2       scales_0.2.3      
 [9] reshape2_1.2.2     labeling_0.2       proto_1.0.0        RColorBrewer_1.0-5
[13] tools_3.2.1        stringr_0.6.2      dichromat_2.0-0    munsell_0.4.2     
[17] colorspace_1.2-4  
> 

Tidy up C code

Tidy up C code before first milestones. This inclues:

  • Improve variable naming
  • Improve code structure

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.