One Hot Encoding using Tensorflow
Last Updated :
02 Sep, 2020
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.
Code:
python3
import tensorflow as tf
ones_matrix = tf.ones([2, 3])
sess = tf.Session()
ones = sess.run(ones_matrix)
sess.close()
print(ones)
Output:
[[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:
One Hot Encoding Example
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).
Code:
python3
indices = [1, 4, 2, 0, 3]
C = tf.constant(5, name = "C")
one_hot_matrix = tf.one_hot(
indices, C, on_value = 1.0, off_value = 0.0, axis =-1)
sess = tf.Session()
one_hot = sess.run(one_hot_matrix)
sess.close()
# output is of dimension 5 x 5
print(one_hot)
Output:
[[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.
Code:
python3
indices = [[0, 2], [1, -1]]
C = tf.constant(5, name = "C")
one_hot_matrix = tf.one_hot(
indices, C, on_value = 1.0, off_value = 0.0, axis =-1)
sess = tf.Session()
one_hot = sess.run(one_hot_matrix)
sess.close()
# output is of dimension 2 x 2 x 3
print(one_hot)
Output :
[[[1.0, 0.0, 0.0],
[0.0, 0.0, 1.0]],
[[0.0, 1.0, 0.0],
[0.0, 0.0, 0.0]]]
Similar Reads
Using the SavedModel format in Tensorflow TensorFlow is a popular deep-learning framework that provides a variety of tools to help users build, train, and deploy machine-learning models. One of the most important aspects of deploying a machine learning model is saving and exporting it to a format that can be easily used by other programs an
4 min read
Tensor Indexing in Tensorflow In the realm of machine learning and deep learning, tensors are fundamental data structures used to represent numerical data with multiple dimensions. TensorFlow, a powerful numerical computation library, equips you with an intuitive and versatile set of operations for manipulating and accessing dat
10 min read
ML | AutoEncoder with TensorFlow Autoencoders are neural networks used for unsupervised learning tasks like dimensionality reduction, anomaly detection and feature extraction. They consist of two key parts: Encoder that compresses data into a compact form.Decoder that reconstructs the original data from this compressed representati
4 min read
String tensors in Tensorflow TensorFlow is a comprehensive open-source library for data science, it offers various data types for handling complex operations. The tf.string data type is used to represent string values. Unlike numeric data types that have a fixed size, strings are variable-length and can contain sequences of cha
5 min read
Introduction to Tensor with Tensorflow Tensor is a multi-dimensional array used to store data in machine learning and deep learning frameworks, such as TensorFlow. Tensors are the fundamental data structure in TensorFlow, and they represent the flow of data through a computation graph. Tensors generalize scalars, vectors, and matrices to
5 min read