tensorflow 0.8 one hot encoding

103 views Asked by At

the data that i wanna encode looks as follows:

print (train['labels'])

[ 0 0 0 ..., 42 42 42]

there are 43 classes going from 0-42

Now i read that tensorflow in version 0.8 has a new feature for one hot encoding so i tried to use it as following:

trainhot=tf.one_hot(train['labels'], 43, on_value=1, off_value=0)

only problem is that i think the output is not what i need

print (trainhot[1])

Tensor("strided_slice:0", shape=(43,), dtype=int32)

Can someone nudge me in the right direction please :)

1

There are 1 answers

0
Yao Zhang On

The output is correct and expected. trainhot[1] is the label of the second (0-based index) training sample, which is of 1D shape (43,). You can play with the code below to better understand tf.one_hot:

  onehot = tf.one_hot([0, 0, 41, 42], 43, on_value=1, off_value=0)                  
  with tf.Session() as sess:                                                        
    onehot_v = sess.run(onehot)                                                  
  print("v: ", onehot_v)                                                            
  print("v shape: ", onehot_v.shape)                                                
  print("v[1] shape: ", onehot[1])

output:
v:  [[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
  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
  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 0 0
  0 0 0 0 0 1]]
v shape:  (4, 43)
v[1] shape:  Tensor("strided_slice:0", shape=(43,), dtype=int32)