R: Stochastic gradient descent (2025)

sgd {sgd}R Documentation

Description

Run stochastic gradient descent in order to optimize the induced lossfunction given a model and data.

Usage

sgd(x, ...)## S3 method for class 'formula'sgd(formula, data, model, model.control = list(), sgd.control = list(...), ...)## S3 method for class 'matrix'sgd(x, y, model, model.control = list(), sgd.control = list(...), ...)## S3 method for class 'big.matrix'sgd(x, y, model, model.control = list(), sgd.control = list(...), ...)

Arguments

x, y

a design matrix and the respective vector of outcomes.

...

arguments to be used to form the default sgd.controlarguments if it is not supplied directly.

formula

an object of class "formula" (or one that can becoerced to that class): a symbolic description of the model to be fitted.The details can be found in "glm".

data

an optional data frame, list or environment (or object coercibleby as.data.frame to a data frame) containing thevariables in the model. If not found in data, the variables are taken fromenvironment(formula), typically the environment from which glm is called.

model

character specifying the model to be used: "lm" (linearmodel), "glm" (generalized linear model), "cox" (Coxproportional hazards model), "gmm" (generalized method of moments),"m" (M-estimation). See ‘Details’.

model.control

a list of parameters for controlling the model.

family ("glm")

a description of the error distribution andlink function to be used in the model. This can be a character stringnaming a family function, a family function or the result of a call toa family function. (See family for details offamily functions.)

rank ("glm")

logical. Should the rank of the design matrixbe checked?

fn ("gmm")

a function g(\theta,x) which returns ak-vector corresponding to the k moment conditions. It is arequired argument if gr not specified.

gr ("gmm")

a function to return the gradient. Ifunspecified, a finite-difference approximation will be used.

nparams ("gmm")

number of model parameters. This isautomatically determined for other models.

type ("gmm")

character specifying the generalized method ofmoments procedure: "twostep" (Hansen, 1982), "iterative"(Hansen et al., 1996). Defaults to "iterative".

wmatrix ("gmm")

weighting matrix to be used in the lossfunction. Defaults to the identity matrix.

loss ("m")

character specifying the loss function to beused in the estimating equation. Default is the Huber loss.

lambda1

L1 regularization parameter. Default is 0.

lambda2

L2 regularization parameter. Default is 0.

sgd.control

an optional list of parameters for controlling the estimation.

method

character specifying the method to be used: "sgd","implicit", "asgd", "ai-sgd", "momentum","nesterov". Default is "ai-sgd". See ‘Details’.

lr

character specifying the learning rate to be used:"one-dim", "one-dim-eigen", "d-dim","adagrad", "rmsprop". Default is "one-dim".See ‘Details’.

lr.control

vector of scalar hyperparameters one canset dependent on the learning rate. For hyperparameters aimedto be left as default, specify NA in the correspondingentries. See ‘Details’.

start

starting values for the parameter estimates. Default israndom initialization around zero.

size

number of SGD estimates to store for diagnostic purposes(distributed log-uniformly over total number of iterations)

reltol

relative convergence tolerance. The algorithm stopsif it is unable to change the relative mean squared difference in theparameters by more than the amount. Default is 1e-05.

npasses

the maximum number of passes over the data. Defaultis 3.

pass

logical. Should tol be ignored and run thealgorithm for all of npasses?

shuffle

logical. Should the algorithm shuffle the data setincluding for each pass?

verbose

logical. Should the algorithm print progress?

Details

Models:The Cox model assumes that the survival data is ordered when passedin, i.e., such that the risk set of an observation i is all data points afterit.

Methods:

sgd

stochastic gradient descent (Robbins and Monro, 1951)

implicit

implicit stochastic gradient descent (Toulis et al.,2014)

asgd

stochastic gradient with averaging (Polyak and Juditsky,1992)

ai-sgd

implicit stochastic gradient with averaging (Toulis etal., 2015)

momentum

"classical" momentum (Polyak, 1964)

nesterov

Nesterov's accelerated gradient (Nesterov, 1983)

Learning rates and hyperparameters:

one-dim

scalar value prescribed in Xu (2011) as

a_n = scale * gamma/(1 + alpha*gamma*n)^(-c)

where the defaults arelr.control = (scale=1, gamma=1, alpha=1, c)where c is 1 if implemented without averaging,2/3 if with averaging

one-dim-eigen

diagonal matrixlr.control = NULL

d-dim

diagonal matrixlr.control = (epsilon=1e-6)

adagrad

diagonal matrix prescribed in Duchi et al. (2011) aslr.control = (eta=1, epsilon=1e-6)

rmsprop

diagonal matrix prescribed in Tieleman and Hinton(2012) aslr.control = (eta=1, gamma=0.9, epsilon=1e-6)

Value

An object of class "sgd", which is a list containing the followingcomponents:

model

name of the model

coefficients

a named vector of coefficients

converged

logical. Was the algorithm judged to have converged?

estimates

estimates from algorithm stored at each iterationspecified in pos

fitted.values

the fitted mean values

pos

vector of indices specifying the iteration number each estimatewas stored for

residuals

the residuals, that is response minus fitted values

times

vector of times in seconds it took to complete the number ofiterations specified in pos

model.out

a list of model-specific output attributes

Author(s)

Dustin Tran, Tian Lan, Panos Toulis, Ye Kuang, Edoardo Airoldi

References

John Duchi, Elad Hazan, and Yoram Singer. Adaptive subgradient methods foronline learning and stochastic optimization. Journal of MachineLearning Research, 12:2121-2159, 2011.

Yurii Nesterov. A method for solving a convex programming problem withconvergence rate O(1/k^2). Soviet Mathematics Doklady,27(2):372-376, 1983.

Boris T. Polyak. Some methods of speeding up the convergence of iterationmethods. USSR Computational Mathematics and Mathematical Physics,4(5):1-17, 1964.

Boris T. Polyak and Anatoli B. Juditsky. Acceleration of stochasticapproximation by averaging. SIAM Journal on Control and Optimization,30(4):838-855, 1992.

Herbert Robbins and Sutton Monro. A stochastic approximation method.The Annals of Mathematical Statistics, pp. 400-407, 1951.

Panos Toulis, Jason Rennie, and Edoardo M. Airoldi, "Statistical analysis ofstochastic gradient methods for generalized linear models", InProceedings of the 31st International Conference on Machine Learning,2014.

Panos Toulis, Dustin Tran, and Edoardo M. Airoldi, "Stability and optimalityin stochastic gradient descent", arXiv preprint arXiv:1505.02417, 2015.

Wei Xu. Towards optimal one pass large scale learning with averagedstochastic gradient descent. arXiv preprint arXiv:1107.2490, 2011.

# Dimensions

Examples

## Linear regressionset.seed(42)N <- 1e4d <- 5X <- matrix(rnorm(N*d), ncol=d)theta <- rep(5, d+1)eps <- rnorm(N)y <- cbind(1, X) %*% theta + epsdat <- data.frame(y=y, x=X)sgd.theta <- sgd(y ~ ., data=dat, model="lm")sprintf("Mean squared error: %0.3f", mean((theta - as.numeric(sgd.theta$coefficients))^2))

[Package sgd version 1.1.2 Index]

R: Stochastic gradient descent (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6149

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.