2

What is the CNTK way to convert a vector that contains labels as indices (just a regular vector, not a sparse representation) to a one hot-representation? Here is an example for 5 classes:

Input

[2, 0, 1, 1]

Desired output:

[[0,0,1,0,0],
[1,0,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0]]

Is there a way without going through Python/numpy?

pkranen
  • 36
  • 3
  • What do you by "what is the CNTK way..."? Do you mean: Which internal CNTK function does the conversion, because you don't want to do Python? – Anton Schwaighofer Mar 20 '17 at 09:23
  • 1
    Possible duplicate of [What is the CNTK way to convert a vector that contains labels as indices to a one hot-representation?](http://stackoverflow.com/questions/42896773/what-is-the-cntk-way-to-convert-a-vector-that-contains-labels-as-indices-to-a-on) – Anton Schwaighofer Mar 20 '17 at 09:32

1 Answers1

1

Here's how to do with one_hot (assuming you have a batch of 4 labels):

>>> x0 = np.array([2, 0, 1, 1]).reshape(4,1)
>>> x = C.input_variable(1)
>>> y = C.one_hot(x, 5, sparse_output=False)
>>> y(x0)
array([[[ 0.,  0.,  1.,  0.,  0.]],

       [[ 1.,  0.,  0.,  0.,  0.]],

       [[ 0.,  1.,  0.,  0.,  0.]],

       [[ 0.,  1.,  0.,  0.,  0.]]], dtype=float32)