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

Impossible to provide arguments to random_normal in scala ? #12140

Closed
mdespriee opened this issue Aug 12, 2018 · 12 comments
Closed

Impossible to provide arguments to random_normal in scala ? #12140

mdespriee opened this issue Aug 12, 2018 · 12 comments
Labels

Comments

@mdespriee
Copy link
Contributor

mdespriee commented Aug 12, 2018

Description

I'm trying to use random_normal in the Symbol API. In scala.
The docs mentions that loc and scale could be Symbols and have a shape (that's what I'm looking for). Although, when I try to use the symbol:

val mean:Symbol...
val std:Symbol...
Symbol.random_normal(i.output)(mean, std)()

MXNet complains that arguments are incorrect.

org.apache.mxnet.MXNetError: [23:24:15] src/core/symbolic.cc:290: Check failed: args.size() <= n_req (2 vs. 0) Incorrect number of arguments, requires 0, provided 2

Sounds like a bug to me. Or do I miss something ?

Environment

Full code

object NormalTest extends App {
  val mean = Symbol.Variable("mean")
  val std = Symbol.Variable("std")
  val s = Symbol.random_normal("gaussian")(mean, std)()

  val exec = s.bind(Context.defaultCtx, Map(
    "mean" -> NDArray.array(Array(0f, 10f), Shape(2)),
    "std" -> NDArray.array(Array(1f, 2f), Shape(2))
  ))

  exec.forward()
  println(s"Result= ${exec.outputs.head.toArray.mkString(",")} ")
}
@lanking520
Copy link
Member

Hi @mdespriee
Nice try with Scala package! In your case, you can implement in the following ways:

Symbol.random_normal(i.output)()(Map("loc" -> mean, ...))

Just put all of your args in the map. You can always check Python API example.

BTW, in the future, you can call Symbol.api.random_normal. This is a brand new api set introduced in v1.3 (not coming yet..) here is an example:

  /**
  * <pre>
  * Draw random samples from a normal (Gaussian) distribution.
  * 
  * .. note:: The existing alias ``normal`` is deprecated.
  * 
  * Samples are distributed according to a normal distribution parametrized by *loc* (mean) and *scale* (standard deviation).
  * 
  * Example::
  * 
  *    normal(loc=0, scale=1, shape=(2,2)) = [[ 1.89171135, -1.16881478],
  *                                           [-1.23474145,  1.55807114]]
  * 
  * 
  * Defined in src/operator/random/sample_op.cc:L85
  * </pre>
  * @param loc		Mean of the distribution.
  * @param scale		Standard deviation of the distribution.
  * @param shape		Shape of the output.
  * @param ctx		Context of output, in format [cpu|gpu|cpu_pinned](n). Only used for imperative calls.
  * @param dtype		DType of the output in case this can't be inferred. Defaults to float32 if not defined (dtype=None).
  * @return org.apache.mxnet.Symbol
  */
@Experimental
def random_normal (loc : Option[org.apache.mxnet.Base.MXFloat] = None, scale : Option[org.apache.mxnet.Base.MXFloat] = None, shape : Option[org.apache.mxnet.Shape] = None, ctx : Option[String] = None, dtype : Option[String] = None, name : String = null, attr : Map[String, String] = null) : org.apache.mxnet.Symbol

@mxnet-label-bot can you label this as [scala] ?

@mdespriee
Copy link
Contributor Author

Actually, I tried that as well, and can't make it work either.

object NormalTest extends App {
  val mean = Symbol.Variable("mean")
  val std = Symbol.Variable("std")
  val s = Symbol.random_normal("gaussian")()(Map("loc"->mean, "scale"->std, "shape" -> (2,2)))

  val exec = s.bind(Context.defaultCtx, Map(
    "mean" -> NDArray.array(Array(0f, 10f), Shape(2)),
    "std" -> NDArray.array(Array(1f, 2f), Shape(2))
  ))

  exec.forward()
  println(s"Result= ${exec.outputs.head.toArray.mkString(",")} ")
}
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f0ec1a742eb, pid=4360, tid=0x00007f0efab2f700
#
# JRE version: OpenJDK Runtime Environment (8.0_151-b12) (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
# Java VM: OpenJDK 64-Bit Server VM (25.151-b12 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [mxnet-scala+0x6a32eb]  mxnet::NDArray::NDArray(mxnet::NDArray const&)+0xb

and:

Stack: [0x00007f0efaa2f000,0x00007f0efab30000],  sp=0x00007f0efab2d160,  free space=1016k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [mxnet-scala+0x6a32eb]  mxnet::NDArray::NDArray(mxnet::NDArray const&)+0xb

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  org.apache.mxnet.LibInfo.mxExecutorBindEX(JIII[Ljava/lang/String;[I[II[J[J[I[JJLorg/apache/mxnet/Base$RefLong;)I+0
j  org.apache.mxnet.Symbol.bindHelper(Lorg/apache/mxnet/Context;Lscala/collection/Seq;Lscala/collection/Iterable;Lscala/collection/Iterable;Lscala/collection/Iterable;Lscala/collection/Iterable;Lscala/collection/immutable/Map;Lorg/apache/mxnet/Executor;)Lorg/apache/mxnet/Executor;+767
j  org.apache.mxnet.Symbol.bind(Lorg/apache/mxnet/Context;Lscala/collection/immutable/Map;)Lorg/apache/mxnet/Executor;+38
j  NormalTest$.delayedEndpoint$NormalTest$1()V+365
j  NormalTest$delayedInit$body.apply()Ljava/lang/Object;+4
j  scala.Function0$class.apply$mcV$sp(Lscala/Function0;)V+1
j  scala.runtime.AbstractFunction0.apply$mcV$sp()V+1
j  scala.App$$anonfun$main$1.apply(Lscala/Function0;)V+1
j  scala.App$$anonfun$main$1.apply(Ljava/lang/Object;)Ljava/lang/Object;+5
J 13378 C2 scala.collection.immutable.List.foreach(Lscala/Function1;)V (32 bytes) @ 0x00007f0f442aef24 [0x00007f0f442aee80+0xa4]
J 15261 C1 scala.collection.generic.TraversableForwarder$class.foreach(Lscala/collection/generic/TraversableForwarder;Lscala/Function1;)V (13 bytes) @ 0x00007f0f45cb6bfc [0x00007f0f45cb6b80+0x7c]
j  scala.App$class.main(Lscala/App;[Ljava/lang/String;)V+21
j  NormalTest$.main([Ljava/lang/String;)V+2

@lanking520
Copy link
Member

@mdespriee looks like you are using it in a wrong way, are you trying to use it in Symbolic way?
Here is what I have tried using NDArray

val nd = NDArray.random_normal(Map("loc" -> 0, "scale" -> 1, "shape" -> Shape(2, 2)))()
nd.toArray.foreach(println)

Output is:

1.1630787
0.4838046
0.29956347
0.15302546

@mdespriee
Copy link
Contributor Author

@lanking520 Oh wait, I think I understand.
Actually, I'm interested in this: https://mxnet.incubator.apache.org/api/python/symbol/symbol.html#mxnet.symbol.random.normal
Which can, if understand well the piece of doc about the shape, be fed with non scalar Symbols.

To put it differently, I don't have a single value for loc and scale, I have a full batch of them, and I want to draw random numbers for each pair. In a Symbolic computation.

As I'm looking for this in Scala, I've probably mixed the differents APIs. it's a bit of a mess.
So I guess this particular API is not (yet?) ported to scala ?

@vdantu
Copy link
Contributor

vdantu commented Aug 14, 2018

@mxnet-label-bot [Scala, Question]

@lanking520
Copy link
Member

lanking520 commented Aug 14, 2018

@lanking520
Copy link
Member

Actually if you are interested, you can be the contributor to get it out for Scala package. It needs some wrappers similar to python side and expose the following apis:

_random_exponential
_random_gamma
_random_generalized_negative_binomial
_random_negative_binomial
_random_normal
_random_poisson
_random_uniform

These can be code-generated from Macro side. Please let me know if you are interested

@mdespriee
Copy link
Contributor Author

@lanking520 yes, I could be interested. I'll have a look after my vacations, though ;-)

@mdespriee
Copy link
Contributor Author

mdespriee commented Sep 5, 2018

@lanking520 So, I just discovered Symbol.sample_normal which is exactly what it is about !
http:https://mxnet.incubator.apache.org/versions/1.2.1/api/python/symbol/symbol.html#mxnet.symbol.sample_normal. And which exists in Scala API.
So the Random module is useless, isn't it ?

@lanking520
Copy link
Member

Link this Issue to the PR: #12489

@zachgk
Copy link
Contributor

zachgk commented Jan 12, 2019

@lanking520 @mdespriee I think this can be closed because #13039 was merged

@lanking520
Copy link
Member

Close it for now, if you are still facing this problem. Please raise a PR (😄) or reopen this issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants