Skip to content Skip to sidebar Skip to footer

TensorFlow - Dense Vector To One-hot

Suppose I have the following tensor: T = [[0.1, 0.3, 0.7], [0.2, 0.5, 0.3], [0.1, 0.1, 0.8]] I want to transform this into a one-hot tensor, such that the indexes with t

Solution 1:

I don't know if there's a way to do this in one step, but there's a one_hot function in tensorflow:

import tensorflow as tf
T = tf.constant([[0.1, 0.3, 0.7], [0.2, 0.5, 0.3], [0.1, 0.1, 0.8]])
T_onehot = tf.one_hot(tf.argmax(T, 1), T.shape[1])
tf.InteractiveSession()
print(T_onehot.eval())

# [[ 0.  0.  1.]
#  [ 0.  1.  0.]
#  [ 0.  0.  1.]]

Post a Comment for "TensorFlow - Dense Vector To One-hot"