Search code examples
tensorone-hot-encoding

tf.one_hot returning array filled with 0's


I am new to deep learning, I am trying to do one hot encoding of this tensor

E = tf.constant(np.random.randint(1,100,size = 10))
E

Output

<tf.Tensor: shape=(10,), dtype=int64, numpy=array([48, 85, 75, 25, 28, 49, 3, 51, 47, 96])>

After using tf.one_hot() the returned array is

tf.one_hot(E,depth=10)

Output

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>

In returned tensor most one hot encoded values are [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]

According to me in one hot encoding there should be unique vector for each value but here there is same vector for many values ?


Solution

  • If your random integers are in the range 1-100, you'll need to keep depth=100 in the one_hot function.

    Conversely, you can reduce the range of random integers to 1-10.

    The idea is that if your number ranges between 1-100, it needs as many bits to represent the one-hot encoding, which you haven't provided by restricting the depth to 10.

    import tensorflow as tf
    import numpy as np
    E = tf.constant(np.random.randint(1,10,size = 10))
    tf.one_hot(E,depth=10)
    

    Output:

    <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
    array([[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]], dtype=float32)>