One Hot Encoding using Tensorflow
In this post, we will be seeing how to initialize a vector in TensorFlow with all zeros or ones. The function you will be calling is
tf.ones(). To initialize with zeros you could use
tf.zeros() instead. These functions take in a shape and return an array full of zeros and ones accordingly.
Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the Machine Learning Foundation Course at a student-friendly price and become industry ready.
[[1. 1. 1.] [1. 1. 1.]]
Using One Hot Encoding:
Many times in deep learning and general vector computations you will have a y vector with numbers ranging from 0 to C-1 and you want to do the following conversion. If C is for example 5, then you might have the following y vector which you will need to convert as follows:
This can be done as follows:
Parameters passed to the function:
indices: A Tensor of indices.
depth: A scalar defining the depth of the one hot dimension.
on_value: A scalar defining the value to fill in output when indices[j] = i. (default : 1)
off_value: A scalar defining the value to fill in output when indices[j] != i. (default : 0)
axis: The axis to fill (default : -1, a new inner-most axis).
dtype: The data type of the output tensor.
name: A name for the operation (optional).
[[0.0, 1.0, 0.0, 0.0, 0.0 ] [0.0, 0.0, 0.0, 0.0, 1.0] [0.0, 0.0, 1.0, 0.0, 0.0] [1.0, 0.0, 0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 1.0, 0.0]]
Feel free to change values and see the result.
[[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]], [[0.0, 1.0, 0.0], [0.0, 0.0, 0.0]]]