TensorFlow – How to create one hot tensor
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
One hot tensor is a Tensor in which all the values at indices where i =j and i!=j is same.
- one_hot: This method accepts a Tensor of indices, a scalar defining depth of the one hot dimension and returns a one hot Tensor with default on value 1 and off value 0. These on and off values can be modified.
Indices: tf.Tensor([1 2 3], shape=(3, ), dtype=int32) Res: tf.Tensor( [[0. 1. 0.] [0. 0. 1.] [0. 0. 0.]], shape=(3, 3), dtype=float32)
Example 2: This example explicitly defines the on and off values for the one hot tensor.
Indices: tf.Tensor([1 2 3], shape=(3, ), dtype=int32) Res: tf.Tensor( [[-1 3 -1] [-1 -1 3] [-1 -1 -1]], shape=(3, 3), dtype=int32)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course