Closed eric-haibin-lin closed 4 years ago
Any use-case you have come across (specific to MXNet)?
cumsum
is needed when you have an array of lengths with which to slice another concatenated array, and you want to calculate the start and end index to use. For example, suppose you have three arrays of different lengths, and you concatenated them like:
[1, 2, 3], [4, 5], [6, 7, 8, 9] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
Their corresponding lengths are [3, 2, 4]
.
The cumsum
would be [3, 5, 9]
, with which you can slice [0, 3)
for first array, [3, 5)
for second array, and [5, 9)
for third array.
For example the validation metric for object detection: https://github.com/dmlc/gluon-cv/blob/master/gluoncv/utils/metrics/voc_detection.py#L189-L211 Now it uses numpy and is extremely slow. You can get an order of magnitude speedup if the ops are available in mxnet
I'd really like to have this function for computing returns of rewards in the context of reinforcement learning.
Yeah, if ndarray supports cumsum operation, some custom metrics(i.e. mAP) will benefit from that.
I find a elegant method to implement cumsum. Hope this will be useful.
step = lambda data, state : (data + state[0], state[0] + data)
data = mx.nd.ones((10, 10))
state = mx.nd.zeros(10)
outs, state = mx.nd.contrib.foreach(step, data, state)
@MyYaYa Cool, is that also differentiable?
@tsuberim yeah, as I know, the symbol api in MXNet support auto-differentiation, so you can try the mx.sym.contrib.foreach(), maybe that will work for you. here is the link
Thank you @MyYaYa for the solution! I've wrapped it up into a generic consum
function here while we wait for official support in future versions.
Cumsum is important because we need it to calculate Lovasz loss
I find a elegant method to implement cumsum. Hope this will be useful.
step = lambda data, state : (data + state[0], state[0] + data) data = mx.nd.ones((10, 10)) state = mx.nd.zeros(10) outs, state = mx.nd.contrib.foreach(step, data, state)
Now, I find this solution is so slow, especially when the size of array is big. So this solution has become a main cost of time in my program. Is there any efficient solution of 'cumsum' using mxnet's ndarray?
I think a better solution is to write a c++ version of this op.
It would be great in order to more efficiently implement Earth Mover's Loss
It would be great to compute the CDF from the histogram.
Taken note of all the above comments. All these use cases make this feature request very relevant. Tagging myself here as I start working on this.
Added in mxnet.np
module now, closing.
MXNet equivalence of numpy.cumsum https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.cumsum.html?highlight=cumsum#numpy.cumsum