Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into dmlc-sparse-squash
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-haibin-lin committed Jul 27, 2017
2 parents ec2c4bf + 72d00fd commit 88eaac6
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 215 deletions.
10 changes: 4 additions & 6 deletions R-package/R/io.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
is.MXDataIter <- function(x) {
inherits(x, "Rcpp_MXNativeDataIter") ||
inherits(x, "Rcpp_MXArrayDataIter")
}

#' Judge if an object is mx.dataiter
#'
#' @return Logical indicator
#'
#' @export
is.mx.dataiter <- is.MXDataIter
is.mx.dataiter <- function(x) {
inherits(x, "Rcpp_MXNativeDataIter") ||
inherits(x, "Rcpp_MXArrayDataIter")
}

#' Extract a certain field from DataIter.
#'
Expand Down
10 changes: 10 additions & 0 deletions R-package/R/metric.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ mx.metric.rmsle <- mx.metric.custom("rmsle", function(label, pred) {
return(res)
})

#' Perplexity metric for language model
#'
#' @export
mx.metric.Perplexity <- mx.metric.custom("Perplexity", function(label, pred) {
label_probs <- as.array(mx.nd.choose.element.0index(pred, label))
batch <- length(label_probs)
NLL <- -sum(log(pmax(1e-15, as.array(label_probs)))) / batch
Perplexity <- exp(NLL)
return(Perplexity)
})
10 changes: 8 additions & 2 deletions R-package/R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ mx.model.train <- function(symbol, ctx, input.shape, output.shape,
return(model)
}

# Initialize parameters
#' Parameter initialization
#' @param symbol The symbolic configuration of the neural network.
#' @param input.shape The shape of the input for the neural network.
#' @param output.shape The shape of the output for the neural network. It can be NULL.
#' @param initializer, initializer object. The initialization scheme for parameters.
#' @param ctx mx.context. The devices used to perform initialization.
#' @export
mx.model.init.params <- function(symbol, input.shape, output.shape, initializer, ctx) {
if (!is.MXSymbol(symbol)) stop("symbol need to be MXSymbol")

Expand All @@ -296,7 +302,7 @@ mx.model.init.params <- function(symbol, input.shape, output.shape, initializer,

# Initialize the data iter
mx.model.init.iter <- function(X, y, batch.size, is.train) {
if (is.MXDataIter(X)) return(X)
if (is.mx.dataiter(X)) return(X)
if (is.null(y)) {
if (is.train) stop("Need to provide parameter y for training with R arrays.")
shape <- dim(X)
Expand Down
16 changes: 3 additions & 13 deletions R-package/R/rnn_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@ is.param.name <- function(name) {
grepl('gamma$', name) || grepl('beta$', name) )
}

# Initialize parameters
mx.model.init.params.rnn <- function(symbol, input.shape, initializer, ctx) {
if (!is.mx.symbol(symbol)) stop("symbol need to be MXSymbol")
slist <- symbol$infer.shape(input.shape)
if (is.null(slist)) stop("Not enough information to get shapes")
arg.params <- mx.init.create(initializer, slist$arg.shapes, ctx, skip.unknown=TRUE)
aux.params <- mx.init.create(initializer, slist$aux.shapes, ctx, skip.unknown=FALSE)
return(list(arg.params=arg.params, aux.params=aux.params))
}

# Initialize the data iter
mx.model.init.iter.rnn <- function(X, y, batch.size, is.train) {
if (is.MXDataIter(X)) return(X)
if (is.mx.dataiter(X)) return(X)
shape <- dim(X)
if (is.null(shape)) {
num.data <- length(X)
Expand Down Expand Up @@ -56,11 +46,11 @@ setup.rnn.model <- function(rnn.sym, ctx,
}
}
}
params <- mx.model.init.params.rnn(rnn.sym, input.shapes, initializer, mx.cpu())
params <- mx.model.init.params(rnn.sym, input.shapes, NULL, initializer, mx.cpu())
args <- input.shapes
args$symbol <- rnn.sym
args$ctx <- ctx
args$grad.req <- "add"
args$grad.req <- "write"
rnn.exec <- do.call(mx.simple.bind, args)

mx.exec.update.arg.arrays(rnn.exec, params$arg.params, match.name=TRUE)
Expand Down
10 changes: 5 additions & 5 deletions R-package/tests/testthat/test_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ test_that("Fine-tune", {

new_fc <- mx.symbol.FullyConnected(data = flatten, num_hidden = 2, name = "fc1")
new_soft <- mx.symbol.SoftmaxOutput(data = new_fc, name = "softmax")
arg_params_new <- mxnet:::mx.model.init.params(symbol = new_soft,
input.shape = list("data" = c(224, 224, 3, 8)),
output.shape = NULL,
initializer = mx.init.uniform(0.1),
ctx = mx.cpu())$arg.params
arg_params_new <- mx.model.init.params(symbol = new_soft,
input.shape = list("data" = c(224, 224, 3, 8)),
output.shape = NULL,
initializer = mx.init.uniform(0.1),
ctx = mx.cpu())$arg.params
fc1_weights_new <- arg_params_new[["fc1_weight"]]
fc1_bias_new <- arg_params_new[["fc1_bias"]]

Expand Down
3 changes: 2 additions & 1 deletion amalgamation/amalgamation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
'kvstore_dist.h', 'mach/clock.h', 'mach/mach.h',
'malloc.h', 'mkl.h', 'mkl_cblas.h', 'mkl_vsl.h', 'mkl_vsl_functions.h',
'nvml.h', 'opencv2/opencv.hpp', 'sys/stat.h', 'sys/types.h', 'cuda.h', 'cuda_fp16.h',
'omp.h', 'execinfo.h', 'packet/sse-inl.h', 'emmintrin.h', 'thrust/device_vector.h'
'omp.h', 'execinfo.h', 'packet/sse-inl.h', 'emmintrin.h', 'thrust/device_vector.h',
'cusolverDn.h'
]

minimum = int(sys.argv[6]) if len(sys.argv) > 5 else 0
Expand Down
1 change: 1 addition & 0 deletions amalgamation/mxnet_predict0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


#include "src/ndarray/ndarray_function.cc"
#include "src/ndarray/autograd.cc"
#include "src/ndarray/ndarray.cc"

#include "src/engine/engine.cc"
Expand Down
Loading

0 comments on commit 88eaac6

Please sign in to comment.