mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

simple math functions on NDArray

Open Kublai-Jing opened this issue 11 years ago • 9 comments

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 !

Kublai-Jing avatar Oct 21 '15 15:10 Kublai-Jing

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

tqchen avatar Oct 21 '15 15:10 tqchen

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.

tqchen avatar Oct 21 '15 15:10 tqchen

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

tqchen avatar Oct 21 '15 15:10 tqchen

hi tq,

thanks !. I assume that if I add it the automatic differentiation works just fine right ?

Kublai-Jing avatar Oct 21 '15 16:10 Kublai-Jing

If you add it as symbolic operator, then auto diff will work out

tqchen avatar Oct 21 '15 16:10 tqchen

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

tqchen avatar Oct 21 '15 19:10 tqchen

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.

Kublai-Jing avatar Oct 21 '15 19:10 Kublai-Jing

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

tqchen avatar Oct 22 '15 06:10 tqchen

Close this, thanks to contribution from @Kublai-Jing

tqchen avatar Nov 01 '15 01:11 tqchen