You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by qk...@apache.org on 2017/08/01 01:09:17 UTC

[incubator-mxnet] branch master updated: [R][DOC] make sure all code in vignettes can run without error (#7274)

This is an automated email from the ASF dual-hosted git repository.

qkou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 92a93b8  [R][DOC] make sure all code in vignettes can run without error (#7274)
92a93b8 is described below

commit 92a93b803e58d0df3011584ae6d04684f928f759
Author: Qiang Kou (KK) <qk...@qkou.info>
AuthorDate: Tue Aug 1 01:09:14 2017 +0000

    [R][DOC] make sure all code in vignettes can run without error (#7274)
---
 R-package/vignettes/CallbackFunctionTutorial.Rmd   | 32 ++++++-------
 .../{CatsDogsFinetune.rmd => CatsDogsFinetune.Rmd} | 18 ++++----
 R-package/vignettes/CharRnnModel.Rmd               | 52 +++++++++++++++-------
 .../classifyRealImageWithPretrainedModel.Rmd       | 17 +++----
 R-package/vignettes/fiveMinutesNeuralNetwork.Rmd   |  1 -
 5 files changed, 67 insertions(+), 53 deletions(-)

diff --git a/R-package/vignettes/CallbackFunctionTutorial.Rmd b/R-package/vignettes/CallbackFunctionTutorial.Rmd
index 97b6ce3..91b4c09 100644
--- a/R-package/vignettes/CallbackFunctionTutorial.Rmd
+++ b/R-package/vignettes/CallbackFunctionTutorial.Rmd
@@ -1,14 +1,10 @@
-MXNet R Tutorial on Callback Function
+MXNet R Tutorial for Callback Function
 ======================================
 
 This vignette gives users a guideline for using and writing callback functions,
-which can very useful in model training. 
+which can be very useful in model training. 
 
-This tutorial is written in Rmarkdown.
-
-- You can directly view the hosted version of the tutorial from [MXNet R Document](http://mxnet.readthedocs.io/en/latest/packages/r/CallbackFunctionTutorial.html)
-
-- You can find the Rmarkdown source from [here](https://github.com/dmlc/mxnet/blob/master/R-package/vignettes/CallbackFunctionTutorial.Rmd)
+This tutorial is written in Rmarkdown. You can find the Rmarkdown source from [here](https://github.com/dmlc/mxnet/blob/master/R-package/vignettes/CallbackFunctionTutorial.Rmd)
 
 Model training example
 ----------
@@ -50,11 +46,12 @@ model <- mx.model.FeedForward.create(
   ctx=mx.cpu(), num.round=10, array.batch.size=20,
   learning.rate=2e-6, momentum=0.9, eval.metric=mx.metric.rmse,
   epoch.end.callback = mx.callback.save.checkpoint("boston"))
+list.files(pattern = "^boston")
 ```
 
 
-- `mx.callback.log.train.metric` is used to log training metric each period. You can use it either as a `batch.end.callback` or a
-`epoch.end.callback`.
+- `mx.callback.log.train.metric` is used to log training metric each period.
+You can use it either as a `batch.end.callback` or a `epoch.end.callback`.
 
 ```{r}
 model <- mx.model.FeedForward.create(
@@ -97,26 +94,25 @@ The `mx.callback.save.checkpoint` function below is stateless. It just get the m
 
 ```{r, eval=FALSE}
 mx.callback.save.checkpoint <- function(prefix, period=1) {
-  function(iteration, nbatch, env, verbose) {
+  function(iteration, nbatch, env, verbose=TRUE) {
     if (iteration %% period == 0) {
       mx.model.save(env$model, prefix, iteration)
-      if(verbose) cat(sprintf("Model checkpoint saved to %s-%04d.params\n", prefix, iteration))
+      if(verbose) message(sprintf("Model checkpoint saved to %s-%04d.params\n", prefix, iteration))
     }
     return(TRUE)
   }
 }
 ```
 
-The `mx.callback.log.train.metric` is a little more complex. It will hold a reference class and update it during the training
-process.
+The `mx.callback.log.train.metric` is a little more complex. It holds a reference class and update it during the training process.
 
 ```{r, eval=FALSE}
 mx.callback.log.train.metric <- function(period, logger=NULL) {
-  function(iteration, nbatch, env, verbose) {
+  function(iteration, nbatch, env, verbose=TRUE) {
     if (nbatch %% period == 0 && !is.null(env$metric)) {
       result <- env$metric$get(env$train.metric)
-      if (nbatch != 0)
-        if(verbose) cat(paste0("Batch [", nbatch, "] Train-", result$name, "=", result$value, "\n"))
+      if (nbatch != 0 & verbose)
+        message(paste0("Batch [", nbatch, "] Train-", result$name, "=", result$value))
       if (!is.null(logger)) {
         if (class(logger) != "mx.metric.logger") {
           stop("Invalid mx.metric.logger.")
@@ -124,8 +120,8 @@ mx.callback.log.train.metric <- function(period, logger=NULL) {
         logger$train <- c(logger$train, result$value)
         if (!is.null(env$eval.metric)) {
           result <- env$metric$get(env$eval.metric)
-          if (nbatch != 0)
-            cat(paste0("Batch [", nbatch, "] Validation-", result$name, "=", result$value, "\n"))
+          if (nbatch != 0 & verbose)
+            message(paste0("Batch [", nbatch, "] Validation-", result$name, "=", result$value))
           logger$eval <- c(logger$eval, result$value)
         }
       }
diff --git a/R-package/vignettes/CatsDogsFinetune.rmd b/R-package/vignettes/CatsDogsFinetune.Rmd
similarity index 93%
rename from R-package/vignettes/CatsDogsFinetune.rmd
rename to R-package/vignettes/CatsDogsFinetune.Rmd
index c137ee8..e30b513 100644
--- a/R-package/vignettes/CatsDogsFinetune.rmd
+++ b/R-package/vignettes/CatsDogsFinetune.Rmd
@@ -30,7 +30,7 @@ library(abind)
 ### Renaming train files
 
 ```{r}
-files <- list.files("./train")
+files <- list.files("./train/")
 old_names <- sapply(files, strsplit, split = ".", fixed = TRUE)
 max_length <- max(sapply(old_names, function(x) nchar(x[[2]])))
 zeros <- max_length - sapply(old_names, function(x) nchar(x[[2]]))
@@ -51,7 +51,7 @@ Map(function(x, y) file.rename(from = x, to = y), files, new_names)
 ### Training images: 224x224, padded with empty space
 
 ```{r}
-files <- list.files("./train", recursive = TRUE)
+files <- list.files("./train/", recursive = TRUE)
 new_names <- paste0("./train_pad_224x224/", files)
 files <- paste0("./train/", files)
 dir.create("./train_pad_224x224/")
@@ -77,7 +77,7 @@ Map(function(x, y) {
 ### Renaming test files
 
 ```{r}
-files <- list.files("./test")
+files <- list.files("./test/")
 max_length <- max(sapply(files, nchar))
 zeros <- max_length - sapply(files, nchar)
 zeros <- sapply(zeros, function(x) paste(rep(0, x), collapse = ""))
@@ -92,7 +92,7 @@ Map(function(x, y) file.rename(from = x, to = y), files, newnames)
 ### Test images: 224x224, padded with empty space
 
 ```{r}
-files <- list.files("./test")
+files <- list.files("./test/")
 new_names <- paste0("./test_pad_224x224/", files)
 files <- paste0("./test/", files)
 dir.create("./test_pad_224x224/")
@@ -168,11 +168,11 @@ new_soft <- mx.symbol.SoftmaxOutput(data = new_fc,
                                     name = "softmax")
 # set name to original name in symbol$arguments
 
-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"]]
 
diff --git a/R-package/vignettes/CharRnnModel.Rmd b/R-package/vignettes/CharRnnModel.Rmd
index 2cb4b00..9dc00a3 100644
--- a/R-package/vignettes/CharRnnModel.Rmd
+++ b/R-package/vignettes/CharRnnModel.Rmd
@@ -1,23 +1,20 @@
 Char RNN Example
 =============================================
 
-This example aims to show how to use lstm model to build a char level language model, and generate text from it. We use a tiny shakespeare text for demo purpose.
-
-Data can be found at [here](https://github.com/dmlc/web-data/tree/master/mxnet/tinyshakespeare)
-
-Preface
--------
-This tutorial is written in Rmarkdown.
-- You can directly view the hosted version of the tutorial from [MXNet R Document](http://mxnet.readthedocs.io/en/latest/packages/r/CharRnnModel.html)
-- You can find the download the Rmarkdown source from [here](https://github.com/dmlc/mxnet/blob/master/R-package/vignettes/CharRnnModel.Rmd)
+This example aims to show how to use the LSTM model to build a char-level language model, and generate text from it. We use a tiny shakespeare text for demo purpose. Data can be found at [here](https://github.com/dmlc/web-data/tree/master/mxnet/tinyshakespeare).
+This tutorial is written in Rmarkdown. You can find the Rmarkdown source from [here](https://github.com/dmlc/mxnet/blob/master/R-package/vignettes/CharRnnModel.Rmd).
 
 Load Data 
 ---------
+
 First of all, load in the data and preprocess it.
+
 ```{r}
 require(mxnet)
 ```
+
 Set basic network parameters.
+
 ```{r}
 batch.size = 32
 seq.len = 32
@@ -30,7 +27,9 @@ wd=0.00001
 clip_gradient=1
 update.period = 1
 ```
-download the data.
+
+Download the data.
+
 ```{r}
 download.data <- function(data_dir) {
     dir.create(data_dir, showWarnings = FALSE)
@@ -40,7 +39,9 @@ download.data <- function(data_dir) {
     }
 }
 ```
+
 Make dictionary from text.
+
 ```{r}
 make.dict <- function(text, max.vocab=10000) {
     text <- strsplit(text, '')
@@ -58,7 +59,9 @@ make.dict <- function(text, max.vocab=10000) {
     return (dic)
 }
 ```
+
 Transfer text into data feature.
+
 ```{r}
 make.data <- function(file.path, seq.len=32, max.vocab=10000, dic=NULL) {
     fi <- file(file.path, "r")
@@ -91,7 +94,9 @@ make.data <- function(file.path, seq.len=32, max.vocab=10000, dic=NULL) {
     return (list(data=data, dic=dic, lookup.table=lookup.table))
 }
 ```
+
 Move tail text.
+
 ```{r}
 drop.tail <- function(X, batch.size) {
     shape <- dim(X)
@@ -99,7 +104,9 @@ drop.tail <- function(X, batch.size) {
     return (X[, 1:(nstep * batch.size)])
 }
 ```
-get the label of X
+
+Get the label of X
+
 ```{r}
 get.label <- function(X) {
     label <- array(0, dim=dim(X))
@@ -113,7 +120,9 @@ get.label <- function(X) {
     return (label)
 }
 ```
-get training data and eval data
+
+Get training data and eval data
+
 ```{r}
 download.data("./data/")
 ret <- make.data("./data/input.txt", seq.len=seq.len)
@@ -141,6 +150,7 @@ X.val <- list(data=X.val.data, label=X.val.label)
 
 Training Model
 --------------
+
 In `mxnet`, we have a function called `mx.lstm` so that users can build a general lstm model. 
 
 ```{r}
@@ -164,7 +174,9 @@ model <- mx.lstm(X.train, X.val,
 
 Inference from model
 --------------------
-helper function for random sample.
+
+Some helper functions for random sample.
+
 ```{r}
 cdf <- function(weights) {
     total <- sum(weights)
@@ -190,6 +202,7 @@ search.val <- function(cdf, x) {
     }
     return (l)
 }
+
 choice <- function(weights) {
     cdf.vals <- cdf(as.array(weights))
     x <- runif(1)
@@ -197,7 +210,9 @@ choice <- function(weights) {
     return (idx)
 }
 ```
-we can use random output or fixed output by choosing largest probability.
+
+We can use random output or fixed output by choosing largest probability.
+
 ```{r}
 make.output <- function(prob, sample=FALSE) {
     if (!sample) {
@@ -212,7 +227,7 @@ make.output <- function(prob, sample=FALSE) {
 ```
 
 In `mxnet`, we have a function called `mx.lstm.inference` so that users can build a inference from lstm model and then use function `mx.lstm.forward` to get forward output from the inference.
-Build inference from model.
+
 ```{r}
 infer.model <- mx.lstm.inference(num.lstm.layer=num.lstm.layer,
                                  input.size=vocab,
@@ -222,7 +237,9 @@ infer.model <- mx.lstm.inference(num.lstm.layer=num.lstm.layer,
                                  arg.params=model$arg.params,
                                  ctx=mx.cpu())
 ```
-generate a sequence of 75 chars using function `mx.lstm.forward`.
+
+Generate a sequence of 75 chars using function `mx.lstm.forward`.
+
 ```{r}
 start <- 'a'
 seq.len <- 75
@@ -238,7 +255,7 @@ for (i in (1:(seq.len-1))) {
     last.id <- make.output(prob, random.sample)
     out <- paste0(out, lookup.table[[last.id]])
 }
-cat (paste0(out, "\n"))
+message(out)
 ```
 The result:
 ```
@@ -250,4 +267,5 @@ Other RNN models
 ----------------
 In `mxnet`, other RNN models like custom RNN and gru is also provided.
 - For **custom RNN model**, you can replace `mx.lstm` with `mx.rnn` to train rnn model. Also, you can replace `mx.lstm.inference` and `mx.lstm.forward` with `mx.rnn.inference` and `mx.rnn.forward` to inference from rnn model and get forward result from the inference model.
+
 - For **GRU model**, you can replace `mx.lstm` with `mx.gru` to train gru model. Also, you can replace `mx.lstm.inference` and `mx.lstm.forward` with `mx.gru.inference` and `mx.gru.forward` to inference from gru model and get forward result from the inference model.
\ No newline at end of file
diff --git a/R-package/vignettes/classifyRealImageWithPretrainedModel.Rmd b/R-package/vignettes/classifyRealImageWithPretrainedModel.Rmd
index 34847fd..f9d14d9 100644
--- a/R-package/vignettes/classifyRealImageWithPretrainedModel.Rmd
+++ b/R-package/vignettes/classifyRealImageWithPretrainedModel.Rmd
@@ -1,24 +1,24 @@
-Classify Real-World Images with Pre-trained Model
+Classify Real-world Images with Pre-trained Model
 =================================================
 
 MXNet is a flexible and efficient deep learning framework. One of the cool things that a deep learning
 algorithm can do is to classify real world images.
 
-In this example we will show how to use a pretrained Inception-BatchNorm Network to predict the class of
+In this example we will show how to use a pretrained Inception-BatchNorm network to predict the content of
 real world image. The network architecture is described in [1].
 
-The pre-trained Inception-BatchNorm network is able to be downloaded from [this link](http://data.mxnet.io/mxnet/data/Inception.zip)
-This model gives the recent state-of-art prediction accuracy on image net dataset.
+The pre-trained Inception-BatchNorm network can be downloaded from [this link](http://data.mxnet.io/mxnet/data/Inception.zip).
+This model gives the recent state-of-art prediction accuracy on the image net dataset.
 
 Preface
 -------
-This tutorial is written in Rmarkdown.
-- You can directly view the hosted version of the tutorial from [MXNet R Document](http://mxnet.readthedocs.io/en/latest/packages/r/classifyRealImageWithPretrainedModel.html)
-- You can find the download the Rmarkdown source from [here](https://github.com/dmlc/mxnet/blob/master/R-package/vignettes/classifyRealImageWithPretrainedModel.Rmd)
+This tutorial is written in Rmarkdown. You can find the Rmarkdown source from [here](https://github.com/dmlc/mxnet/blob/master/R-package/vignettes/classifyRealImageWithPretrainedModel.Rmd)
 
 Package Loading
 ---------------
-To get started, we load the mxnet package by require mxnet.
+
+To get started, we load the `mxnet` package first.
+
 ```{r}
 require(mxnet)
 ```
@@ -31,6 +31,7 @@ require(imager)
 
 Load the Pretrained Model
 -------------------------
+
 Make sure you unzip the pre-trained model in current folder. And we can use the model
 loading function to load the model into R.
 
diff --git a/R-package/vignettes/fiveMinutesNeuralNetwork.Rmd b/R-package/vignettes/fiveMinutesNeuralNetwork.Rmd
index 5cb9aaf..bc45c96 100644
--- a/R-package/vignettes/fiveMinutesNeuralNetwork.Rmd
+++ b/R-package/vignettes/fiveMinutesNeuralNetwork.Rmd
@@ -177,4 +177,3 @@ dim(test.y)
 ```
 Congratulations! Now you have learnt the basic for using `mxnet`. Please check the other tutorials for advanced features.
 
-

-- 
To stop receiving notification emails like this one, please contact
['"commits@mxnet.apache.org" <co...@mxnet.apache.org>'].