Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #301 from tqchen/master
Browse files Browse the repository at this point in the history
[R] Update the R for roxygen
  • Loading branch information
tqchen committed Oct 16, 2015
2 parents 3e7919c + 51911e4 commit 23b5c56
Show file tree
Hide file tree
Showing 27 changed files with 303 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ rcpplint:
python dmlc-core/scripts/lint.py mxnet-rcpp ${LINT_LANG} R-package/src

rcppexport:
Rscript -e "require(mxnet); mxnet::mxnet.export(\"R-package/R\")"
Rscript -e "require(mxnet); mxnet::mxnet.export(\"R-package\")"

roxygen:
Rscript -e "require(roxygen2); roxygen2::roxygenise(\"R-package\")"
Expand Down
73 changes: 72 additions & 1 deletion R-package/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
exportPattern("^[[:alpha:]]+")
# Generated by roxygen2 (4.1.1): do not edit by hand

S3method(Ops,MXNDArray)
S3method(as.array,MXNDArray)
S3method(as.matrix,MXNDArray)
S3method(dim,MXNDArray)
S3method(length,MXNDArray)
S3method(predict,MXFeedForwardModel)
S3method(print,MXNDArray)
export(arguments)
export(ctx)
export(is.mx.context)
export(is.mx.dataiter)
export(is.mx.ndarray)
export(is.mx.symbol)
export(mx.apply)
export(mx.callback.log.train.metric)
export(mx.callback.save.checkpoint)
export(mx.cpu)
export(mx.ctx.default)
export(mx.exec.backward)
export(mx.exec.forward)
export(mx.exec.update.arg.arrays)
export(mx.exec.update.aux.arrays)
export(mx.gpu)
export(mx.init.create)
export(mx.init.normal)
export(mx.init.uniform)
export(mx.io.ImageRecordIter)
export(mx.io.MNISTIter)
export(mx.io.arrayiter)
export(mx.io.extract)
export(mx.kv.create)
export(mx.metric.accuracy)
export(mx.model.FeedForward.create)
export(mx.model.load)
export(mx.model.save)
export(mx.nd.array)
export(mx.nd.clip)
export(mx.nd.copyto)
export(mx.nd.dot)
export(mx.nd.load)
export(mx.nd.ones)
export(mx.nd.save)
export(mx.nd.zeros)
export(mx.opt.create)
export(mx.opt.get.updater)
export(mx.rnorm)
export(mx.runif)
export(mx.set.seed)
export(mx.simple.bind)
export(mx.symbol.Activation)
export(mx.symbol.BatchNorm)
export(mx.symbol.Concat)
export(mx.symbol.Convolution)
export(mx.symbol.Dropout)
export(mx.symbol.ElementWiseSum)
export(mx.symbol.Flatten)
export(mx.symbol.FullyConnected)
export(mx.symbol.Group)
export(mx.symbol.LRN)
export(mx.symbol.LeakyReLU)
export(mx.symbol.Pooling)
export(mx.symbol.Reshape)
export(mx.symbol.SliceChannel)
export(mx.symbol.Softmax)
export(mx.symbol.Variable)
export(mx.symbol.infer.shape)
export(mx.symbol.load)
export(mx.symbol.save)
export(mxnet.export)
export(outputs)
import(Rcpp)
import(methods)
7 changes: 5 additions & 2 deletions R-package/R/callback.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Log training metric each period
#' Log training metric each period
#' @export
mx.callback.log.train.metric <- function(period) {
function(iteration, nbatch, env) {
Expand All @@ -10,7 +10,10 @@ mx.callback.log.train.metric <- function(period) {
}


#
#' Save checkpoint to files each period iteration.
#'
#' @param prefix The prefix of the model checkpoint.
#'
#' @export
mx.callback.save.checkpoint <- function(prefix, period=1) {
function(iteration, nbatch, env) {
Expand Down
4 changes: 4 additions & 0 deletions R-package/R/context.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ init.context.default <- function() {
#'
#' @param new, optional takes \code{mx.cpu()} or \code{mx.gpu(id)}, new default ctx.
#' @return The default context.
#'
#' @export
mx.ctx.default <- function(new = NULL) {
if (!is.null(new)) {
Expand All @@ -18,6 +19,7 @@ mx.ctx.default <- function(new = NULL) {
#' Check if the type is mxnet context.
#'
#' @return Logical indicator
#'
#' @export
is.mx.context <- function(x) {
class(x) == "MXContext"
Expand All @@ -30,6 +32,7 @@ is.mx.context <- function(x) {
#' The device ID, this is meaningless for CPU, included for interface compatiblity.
#' @return The CPU context.
#' @name mx.cpu
#'
#' @export
NULL

Expand All @@ -39,5 +42,6 @@ NULL
#' The GPU device ID, starts from 0.
#' @return The GPU context.
#' @name mx.gpu
#'
#' @export
NULL
7 changes: 5 additions & 2 deletions R-package/R/executor.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Simple bind the symbol to executor,
# with information from input shapes.
#' Simple bind the symbol to executor,
#' with information from input shapes.
#'
#' @export
mx.simple.bind <- function(symbol, ctx, grad.req=FALSE, ...) {
if (!is.MXSymbol(symbol)) stop("symbol need to be MXSymbol")
Expand Down Expand Up @@ -43,13 +44,15 @@ mx.exec.update.aux.arrays <- function(exec, arg.arrays, match.name=FALSE, skip.n

#' Peform an forward on the executors
#' This function will MUTATE the state of exec
#'
#' @export
mx.exec.forward <- function(exec, is.train=TRUE) {
exec$forward(is.train, list())
}

#' Peform an backward on the executors
#' This function will MUTATE the state of exec
#'
#' @export
mx.exec.backward <- function(exec, ...) {
exec$backward(list(...))
Expand Down
12 changes: 10 additions & 2 deletions R-package/R/initializer.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#' Internal default value initialization scheme.
#'
#' @param name the name of the variable.
#' @param shape the shape of the array to be generated.
#'
mx.init.internal.default <- function(name, shape, ctx, allow.unknown=FALSE) {
if (mx.util.str.endswith(name, "bias")) return (mx.nd.zeros(shape, ctx))
if (mx.util.str.endswith(name, "gamma")) return (mx.nd.ones(shape, ctx))
Expand All @@ -27,7 +29,7 @@ mx.init.uniform <- function(scale) {

#' Create a initializer that initialize the weight with normal(0, sd)
#'
#' @param scale The scale of uniform distribution
#' @param sd The standard deviation of normal distribution
#'
#' @export
mx.init.normal <- function(sd) {
Expand All @@ -39,7 +41,13 @@ mx.init.normal <- function(sd) {
}
}

# Create initialization of argument like arg.array
#' Create initialization of argument like arg.array
#'
#' @param initializer The initializer.
#' @param shape.array named-list The shape of the weights
#' @param ctx mx.context The context of the weights
#' @param skip.unknown Whether skip the unknown weight types
#' @export
mx.init.create <- function(initializer, shape.array, ctx, skip.unknown=TRUE) {
if (length(shape.array) == 0) return(list())
names = names(shape.array)
Expand Down
22 changes: 16 additions & 6 deletions R-package/R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ is.MXDataIter <- function(x) {
inherits(x, "Rcpp_MXArrayDataIter")
}

#' Judge if an object is mx.dataiter
#'
#' @return Logical indicator
#'
#' @export
is.mx.dataiter <- is.MXDataIter

#' Extract a certain field from DataIter.
#'
#' @export
mx.io.extract <- function(iter, field) {
packer <- mx.nd.arraypacker.create()
packer <- mx.nd.arraypacker()
iter$reset()
while (iter$iter.next()) {
dlist <- iter$value()
Expand All @@ -15,9 +23,11 @@ mx.io.extract <- function(iter, field) {
oshape <- dim(data)
packer$push(mx.nd.slice(data, 0, oshape[[1]] - padded))
}
iter$reset()
return(packer$get())
}

#
#' Create MXDataIter compatible iterator from R's array
#'
#' @param data The data array.
Expand All @@ -26,11 +36,11 @@ mx.io.extract <- function(iter, field) {
#' @param shuffle Whether shuffle the data
#'
#' @export
mx.io.ArrayIter <- function(data, label=NULL,
mx.io.arrayiter <- function(data, label=NULL,
batch.size=128,
shuffle=FALSE) {
mx.io.internal.ArrayIter.create(as.array(data),
as.array(label),
batch.size,
shuffle)
mx.io.internal.arrayiter(as.array(data),
as.array(label),
batch.size,
shuffle)
}
9 changes: 9 additions & 0 deletions R-package/R/kvstore.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
is.MXKVStore <- function(x) {
inherits(x, "Rcpp_MXKVStore")
}

#' Create a mxnet KVStore.
#'
#' @param type string(default="local") The type of kvstore.
#' @return The kvstore.
#'
#' @name mx.kv.create
#' @export
NULL
3 changes: 2 additions & 1 deletion R-package/R/metric.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ mx.metric.custom <-function(name, feval) {
return(ret)
}

# accuracy metric
#' Accuracy metric
#'
#' @export
mx.metric.accuracy <- mx.metric.custom("accuracy", function(label, pred) {
ypred = max.col(as.array(pred), tie="first")
Expand Down
9 changes: 7 additions & 2 deletions R-package/R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ mx.model.init.iter <- function(X, y, batch.size, is.train) {
shape <- dim(X)
y <- c(1:shape[[1]]) * 0
}
return(mx.io.ArrayIter(X, y, batch.size=batch.size, shuffle=is.train))
return(mx.io.arrayiter(X, y, batch.size=batch.size, shuffle=is.train))
}

#' Create a MXNet Feedforward neural net model with the specified training.
Expand Down Expand Up @@ -288,6 +288,7 @@ mx.model.init.iter <- function(X, y, batch.size, is.train) {
#' @param kvstore string (default="local")
#' The parameter synchronization scheme in multiple devices.
#' @return model A trained mxnet model.
#'
#' @export
mx.model.FeedForward.create <-
function(symbol, X, y=NULL, ctx=NULL,
Expand Down Expand Up @@ -333,6 +334,7 @@ function(symbol, X, y=NULL, ctx=NULL,
#' @param X The dataset to predict.
#' @param ctx mx.cpu() or mx.gpu(i) The device used to generate the prediction.
#' @param array.batch.size The batch size used in batching. Only used when X is R's array.
#'
#' @export
predict.MXFeedForwardModel <- function(model, X, ctx=NULL, array.batch.size=128) {
if (is.null(ctx)) ctx <- mx.ctx.default()
Expand All @@ -343,7 +345,7 @@ predict.MXFeedForwardModel <- function(model, X, ctx=NULL, array.batch.size=128)
pexec <- mx.simple.bind(model$symbol, ctx=ctx, data=dim(dlist$data), grad.req=FALSE)
mx.exec.update.arg.arrays(pexec, model$arg.params, match.name=TRUE)
mx.exec.update.aux.arrays(pexec, model$aux.params, match.name=TRUE)
packer <- mx.nd.arraypacker.create()
packer <- mx.nd.arraypacker()
X$reset()
while (X$iter.next()) {
dlist = X$value()
Expand All @@ -354,13 +356,15 @@ predict.MXFeedForwardModel <- function(model, X, ctx=NULL, array.batch.size=128)
oshape <- dim(out.pred)
packer$push(mx.nd.slice(out.pred, 0, oshape[[1]] - padded))
}
X$reset()
return(packer$get())
}

#' Load model checkpoint from file.
#'
#' @param prefix string prefix of the model name
#' @param iteration integer Iteration number of model we would like to load.
#'
#' @export
mx.model.load <- function(prefix, iteration) {
symbol <- mx.symbol.load(paste0(prefix, "-symbol.json"))
Expand Down Expand Up @@ -398,6 +402,7 @@ mx.model.load <- function(prefix, iteration) {
#' @param model The feedforward model to be saved.
#' @param prefix string prefix of the model name
#' @param iteration integer Iteration number of model we would like to load.
#'
#' @export
mx.model.save <- function(model, prefix, iteration) {
arg.params <- model$arg.params
Expand Down
Loading

0 comments on commit 23b5c56

Please sign in to comment.