pythonmxnet

How to convert mxnet.ndarray.ndarray.NDArray to a scalar?


I got a weird object that shows like this...

type(l.sum())
l.sum()

This is what it outputs:

<class 'mxnet.ndarray.ndarray.NDArray'>
[83.32651]
<NDArray 1 @cpu(0)>

I want to convert it to a scalar but no matter how I try it stays as array.

l.sum()[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]

Still outputs:

[83.32651]
<NDArray 1 @cpu(0)>

Solution

  • For some unknown reasons probably the misbehavior is caused by sum().

    Converting to numpy before the operation has solved the problem:

    l.asnumpy().sum()
    

    This returns a scalar as expected.