Incubator-mxnet: one_hot usage does not match documentation.

Created on 16 Mar 2017  路  5Comments  路  Source: apache/incubator-mxnet

The new one_hot usage does not match the usage described by the documentation.

Documentation claims:

one_hot([1,0,2,0], 3) = [[ 0.  1.  0.]
                         [ 1.  0.  0.]
                         [ 0.  0.  1.]
                         [ 1.  0.  0.]]

Using the same code yields the following error:

mxnet.base.MXNetError: Invalid Parameter format for depth expect int but value='[1, 0, 2, 0]', in operator one_hot(name="", depth="[1, 0, 2, 0]", on_value="3")

Somehow indices is being passed to depth and depth to on_value. Trying to manually specify indices makes it claim it doesn't know that argument:

mx.nd.one_hot(indices=[1,0,2,0], depth=3)

yeilds error

mxnet.base.MXNetError: Cannot find argument 'indices', Possible Arguments:
----------------
depth : int, required
    The dimension size at dim = axis.
on_value : double, optional, default=1
    The value assigned to the locations represented by indices.
off_value : double, optional, default=0
    The value assigned to the locations not represented by indices.
dtype : {'float16', 'float32', 'float64', 'int32', 'uint8'},optional, default='float32'
    DType of the output
, in operator one_hot(name="", depth="3", indices="[1, 0, 2, 0]")

Making the indices argument an ndarray does not help.

Also, the old onehot_encode function has been deprecated making this somewhat problematic.

Most helpful comment

I get the same message when using mx.nd.one_hot(). Try casting your indices array to np.int32:

import mxnet as mx
import numpy as np

idxs = mx.nd.array([1,0,2,0])
mx.nd.one_hot(idxs.astype(np.int32), 3)

This seems to solve the problem for me, even though the error message doesn't say anything about types. Perhaps a cast is needed inside mx.nd.one_hot() in order to support the functionality from the docs? Or an assert indicies.dtype == np.int32 so the error is more clear.

Edit: I just found the same problem in mx.nd.batch_take(). Casting to np.int32 worked there as well.

All 5 comments

input needs to be an ndarray, not list

As I noted at the end, that still doesn't work, unfortunately. Though it is a slightly different error:

mx.nd.one_hot(mx.nd.array([1,0,2,0]), 3)

yields

mxnet.base.MXNetError: Type inconsistent, Provided=0, inferred type=4

I get the same message when using mx.nd.one_hot(). Try casting your indices array to np.int32:

import mxnet as mx
import numpy as np

idxs = mx.nd.array([1,0,2,0])
mx.nd.one_hot(idxs.astype(np.int32), 3)

This seems to solve the problem for me, even though the error message doesn't say anything about types. Perhaps a cast is needed inside mx.nd.one_hot() in order to support the functionality from the docs? Or an assert indicies.dtype == np.int32 so the error is more clear.

Edit: I just found the same problem in mx.nd.batch_take(). Casting to np.int32 worked there as well.

Ah yes, that will get it through on my end too. Alternatively, you can set the type in the constructor.

mx.nd.one_hot(mx.nd.array([1,0,2,0], dtype=np.int32), 3)

However, I will second then that the operator should do an astype for you if the input type is not int32.

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

Was this page helpful?
0 / 5 - 0 ratings