hetong007 closed pull request #10450: [MXNET-212] [R] Fix Random Samplers from 
Uniform and Gaussian distribution in R bindings
URL: https://github.com/apache/incubator-mxnet/pull/10450
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/R-package/R/random.R b/R-package/R/random.R
index 1badce872e8..6d015928c10 100644
--- a/R-package/R/random.R
+++ b/R-package/R/random.R
@@ -49,7 +49,7 @@ mx.runif <- function(shape, min=0, max=1, ctx=NULL) {
   if (!is.numeric(min)) stop("mx.rnorm only accept numeric min")
   if (!is.numeric(max)) stop("mx.rnorm only accept numeric max")
   ret <- mx.nd.internal.empty(shape, ctx)
-  return (mx.nd.internal.sample.uniform(min, max, shape=shape, out=ret))
+  return (mx.nd.internal.random.uniform(min, max, shape=shape, out=ret))
 }
 
 #' Generate nomal distribution with mean and sd.
@@ -73,5 +73,5 @@ mx.rnorm <- function(shape, mean=0, sd=1, ctx=NULL) {
   if (!is.numeric(mean)) stop("mx.rnorm only accept numeric mean")
   if (!is.numeric(sd)) stop("mx.rnorm only accept numeric sd")
   ret <- mx.nd.internal.empty(shape, ctx)
-  return (mx.nd.internal.sample.normal(mean, sd, shape=shape, out=ret))
+  return (mx.nd.internal.random.normal(mean, sd, shape=shape, out=ret))
 }
diff --git a/R-package/tests/testthat/test_random.R 
b/R-package/tests/testthat/test_random.R
new file mode 100644
index 00000000000..411d0c768a6
--- /dev/null
+++ b/R-package/tests/testthat/test_random.R
@@ -0,0 +1,19 @@
+require(mxnet)
+
+context("random")
+
+test_that("mx.runif", {
+  X <- mx.runif(shape=50000, min=0, max=1, ctx=mx.ctx.default())
+  expect_equal(X>=0, mx.nd.ones(50000))
+  expect_equal(X<=1, mx.nd.ones(50000))
+  sample_mean = mean(as.array(X))
+  expect_equal(sample_mean, 0.5, tolerance=1e-2)
+})
+
+test_that("mx.rnorm", {
+  X <- mx.rnorm(shape=50000, mean=5, sd=0.1, ctx=mx.ctx.default())
+  sample_mean = mean(as.array(X))
+  sample_sd = sd(as.array(X))
+  expect_equal(sample_mean, 5, tolerance=1e-2)
+  expect_equal(sample_sd, 0.1, tolerance=1e-2)
+})


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to