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

[R] An easy-to-use interface for mlp training #460

Merged
merged 6 commits into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R-package/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Imports:
magrittr,
stringr
Suggests:
testthat
testthat, mlbench
LinkingTo: Rcpp
RoxygenNote: 5.0.0
3 changes: 3 additions & 0 deletions R-package/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export(mx.metric.custom)
export(mx.metric.mae)
export(mx.metric.rmse)
export(mx.metric.rmsle)
export(mx.mlp)
export(mx.model.FeedForward.create)
export(mx.model.load)
export(mx.model.save)
Expand Down Expand Up @@ -65,6 +66,7 @@ export(mx.symbol.BatchNorm)
export(mx.symbol.BlockGrad)
export(mx.symbol.Concat)
export(mx.symbol.Convolution)
export(mx.symbol.Deconvolution)
export(mx.symbol.Dropout)
export(mx.symbol.ElementWiseSum)
export(mx.symbol.Flatten)
Expand All @@ -78,6 +80,7 @@ export(mx.symbol.Pooling)
export(mx.symbol.Reshape)
export(mx.symbol.SliceChannel)
export(mx.symbol.Softmax)
export(mx.symbol.SoftmaxOutput)
export(mx.symbol.Variable)
export(mx.symbol.exp)
export(mx.symbol.infer.shape)
Expand Down
69 changes: 69 additions & 0 deletions R-package/R/mlp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' Convenience interface for multiple layer perceptron
#'
#' @param data the input matrix.
#' @param label the training label.
#' @param hidden_node a vector containing number of hidden nodes on each hidden layer as well as the output layer.
#' @param out_node the number of nodes on the output layer.
#' @param dropout a number in [0,1) containing the dropout ratio from the last hidden layer to the output layer.
#' @param activation either a single string or a vector containing the names of the activation functions.
#' @param out_activation a single string containing the name of the output activation function.
#' @param device whether train on cpu (default) or gpu.
#' @param eval_metric the evaluation metric/
#' @param ... other parameters passing to \code{mx.model.FeedForward.create}/
#'
#' @examples
#'
#' require(mlbench)
#' data(Sonar, package="mlbench")
#' Sonar[,61] = as.numeric(Sonar[,61])-1
#' train.ind = c(1:50, 100:150)
#' train.x = data.matrix(Sonar[train.ind, 1:60])
#' train.y = Sonar[train.ind, 61]
#' test.x = data.matrix(Sonar[-train.ind, 1:60])
#' test.y = Sonar[-train.ind, 61]
#' model = mx.mlp(train.x, train.y, hidden_node = 10, out_node = 2, out_activation = "softmax",
#' learning.rate = 0.1)
#' preds = predict(model, test.x)
#'
#' @export
mx.mlp <- function(data, label, hidden_node = 1, out_node, dropout = NULL,
activation = "tanh", out_activation = "softmax",
device=mx.ctx.default(), ...) {

m <- length(hidden_node)
if (!is.null(dropout)) {
if (length(dropout) != 1) {
stop("only accept dropout ratio of length 1.")
}
dropout = max(0,min(dropout, 1-1e-7))
}

# symbol construction
act <- mx.symbol.Variable("data")
if (length(activation) == 1) {
activation <- rep(activation, m)
} else {
if (length(activation) != m) {
stop(paste("Length of activation should be",m))
}
}
for (i in 1:m) {
fc <- mx.symbol.FullyConnected(act, num_hidden=hidden_node[i])
act <- mx.symbol.Activation(fc, act_type=activation[i])
if (i == m && !is.null(dropout)) {
act <- mx.symbol.Dropout(act, p = dropout)
}
}
fc <- mx.symbol.FullyConnected(act, num_hidden=out_node)
if (out_activation == "rmse") {
out <- mx.symbol.LinearRegressionOutput(fc)
} else if (out_activation == "softmax") {
out <- mx.symbol.SoftmaxOutput(fc)
} else if (out_activation == "logistic") {
out <- mx.symbol.LogisticRegressionOutput(fc)
} else {
stop("Not supported yet.")
}
model <- mx.model.FeedForward.create(out, X=data, y=label, ctx=device, ...)
return(model)
}
50 changes: 49 additions & 1 deletion R-package/R/mxnet_generated.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,37 @@ mx.symbol.Convolution <- function(...) {
mx.varg.symbol.Convolution(list(...))
}

#' Apply deconvolution to input then add a bias.
#'
#' @param data Symbol
#' Input data to the DeconvolutionOp.
#' @param weight Symbol
#' Weight matrix.
#' @param bias Symbol
#' Bias parameter.
#' @param kernel Shape(tuple), required
#' deconvolution kernel size: (y, x)
#' @param stride Shape(tuple), optional, default=(1, 1)
#' deconvolution stride: (y, x)
#' @param pad Shape(tuple), optional, default=(0, 0)
#' pad for deconvolution: (y, x)
#' @param num.filter int (non-negative), required
#' deconvolution filter(channel) number
#' @param num.group int (non-negative), optional, default=1
#' number of groups partition
#' @param workspace long (non-negative), optional, default=512
#' Tmp workspace for deconvolution (MB)
#' @param no.bias boolean, optional, default=True
#' Whether to disable bias parameter.
#' @param name string, optional
#' Name of the resulting symbol.
#' @return out The result mx.symbol
#'
#' @export
mx.symbol.Deconvolution <- function(...) {
mx.varg.symbol.Deconvolution(list(...))
}

#' Apply dropout to input
#'
#' @param data Symbol
Expand Down Expand Up @@ -479,7 +510,7 @@ mx.symbol.SliceChannel <- function(...) {
mx.varg.symbol.SliceChannel(list(...))
}

#' Perform a softmax transformation on input.
#' DEPRECATED: Perform a softmax transformation on input. Please use SoftmaxOutput
#'
#' @param data Symbol
#' Input data to softmax.
Expand All @@ -496,6 +527,23 @@ mx.symbol.Softmax <- function(...) {
mx.varg.symbol.Softmax(list(...))
}

#' Perform a softmax transformation on input, backprop with logloss.
#'
#' @param data Symbol
#' Input data to softmax.
#' @param grad.scale float, optional, default=1
#' Scale the gradient by a float factor
#' @param multi.output boolean, optional, default=False
#' If set to true, for a (n,k,x_1,..,x_n) dimensionalinput tensor, softmax will generate n*x_1*...*x_n output, eachhas k classes
#' @param name string, optional
#' Name of the resulting symbol.
#' @return out The result mx.symbol
#'
#' @export
mx.symbol.SoftmaxOutput <- function(...) {
mx.varg.symbol.SoftmaxOutput(list(...))
}

#' Take exp of the src
#'
#' @param src Symbol
Expand Down
50 changes: 50 additions & 0 deletions R-package/man/mx.mlp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions R-package/man/mx.symbol.Deconvolution.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions R-package/man/mx.symbol.Softmax.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions R-package/man/mx.symbol.SoftmaxOutput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.