simple math functions on NDArray
Hi mxnet, I've recently looked into this cool project. I've also used minerva a little bit. I realize that there is no implementation of simple functions on NDArray such as log, exp, sqrt, sqr accessible from mx.sym. Only + - * / are there. Having those functions will help having some self-defined loss functions to be used. Is there particular reason why those are missing ? Or if I want to implement those, where I should start ?
Thanks !
It is fairly easy to add these. The first wave of effort was mainly aim to get things done and add things as we need them. Here are the few steps you can do to add these.
- As symbolic function: add them as new operator http://mxnet.readthedocs.org/en/latest/developer-guide/operator.html
- For the cases you discussed, they are actually special case of Activation function template here https://github.com/dmlc/mxnet/blob/master/src/operator/activation-inl.h
- The only thing you need to do is define ForwardOp and BackwardOp here, in this file https://github.com/dmlc/mxnet/blob/master/src/operator/mshadow_op.h
To expose these as NDArray functions, checkout https://github.com/dmlc/mxnet/blob/master/src/ndarray/ndarray_function.h#L50 square for example, three files are invovled.
- ndarray_function.h to declare the operator
- ndarray_function-inl.h to declare the unary operator https://github.com/dmlc/mxnet/blob/master/src/ndarray/ndarray_function-inl.h#L200
- https://github.com/dmlc/mxnet/blob/master/src/ndarray/ndarray.cc#L652 to register the final one.
We could love your contribution and feedback on this. Things are not the very easiest, but most customization do not involve cuda, but simply write a few lines of expressions and expose them. Hopefully we can improve them over time
hi tq,
thanks !. I assume that if I add it the automatic differentiation works just fine right ?
If you add it as symbolic operator, then auto diff will work out
I am making a PR to simplify it, now you only need to add one line to expose mshadow functions to ndarray
https://github.com/dmlc/mxnet/pull/350/files#diff-b200ea3951c4a013796f0e16694abe82R38
Not sure what you mean. Just start to dive into some code today... Now I am able to add exp, log to Activation as you mentioned, but not the ndarray.
This now is simpified, c.f. how sqrt and square are implemented. https://github.com/dmlc/mxnet/blob/master/src/ndarray/unary_function-inl.h#L74
You can add others similarly. You are welcome to open a PR on this
Close this, thanks to contribution from @Kublai-Jing