2

I tried to create some very basic mxnet code that should only initialize a variable and output the same.

The issue is, I can't get the initialization done.

I pass the initializer as parameter to Variable as indicated in the mxnet docs

I already tried to use different initializers like Xavier, One, Uniform but all results are the same [0,0,0,0] output.

import mxnet as mx
cst = mx.init.Constant(value=2)
a = mx.sym.Variable('A', init=cst)
executor = a.simple_bind(ctx=mx.cpu(), A=(1,4))
executor.forward()

Output:

 [[ 0.  0.  0.  0.]]
 <NDArray 1x4 @cpu(0)>]

However I would expect the output to be [2, 2, 2, 2]

Any idea about what's going on here is most welcome.

rudidu
  • 21
  • 2

1 Answers1

1

You are using the lowest level of the MXNet API, the Symbolic API. You are confusing the initializer, usually used for initializing parameters in the Module API, and the input of your computational graph. If you don't define them in the .forward() function the variables of your graph will be initialized to 0.

import mxnet as mx

a = mx.sym.Variable('A')
executor = a.simple_bind(ctx=mx.cpu(), A=(1,4))
executor.forward(A=np.ones((1,4))*2)
[
 [[2. 2. 2. 2.]]
 <NDArray 1x4 @cpu(0)>]

If you want to use the symbolic API and make use of parameter initializer etc, you can use the Module API. However I would strongly recommending the imperative MXNet Gluon API. You can refer to the list of MXNet tutorials for more information: http://mxnet.incubator.apache.org/api/python/docs/tutorials/

Emil
  • 629
  • 2
  • 7
  • 24
Thomas
  • 676
  • 3
  • 18