How do we change the data type of a tensor?

We can change the data type of any tensor. It takes no trouble. TensorFlow gives us easy options to do that.

But why should we change the data type of a tensor?

Before we know how we can do this, isn’t it not perfect to know why we need to do this?

Therefore, let’s find that answer first.

While computing sometimes we want to use less precision.

What does that mean?

When we change a data type from int 32 to int 16, we call it less precision.

Because when we use int 16 in place of int 32, the change in data type results in consuming less memory space.

In short, a device with less computing capacity, we can think of as a mobile, takes more time to compute heavy data types.

Let’s import TensorFlow first and create two constants of different data types.

import tensorflow as tf


# Creating a new tensor with default datatype (float32)
A = tf.constant([1.75, 85.44])

# Creating a new tensor with default datatype (int32)
B = tf.constant([128, 777])
A, B

# output:
(<tf.Tensor: shape=(2,), dtype=float32, numpy=array([ 1.75, 85.44], dtype=float32)>,
 <tf.Tensor: shape=(2,), dtype=int32, numpy=array([128, 777], dtype=int32)>)

But when we reduce the bits, like changing from int 32 to int 16, it takes less space while computing.

Let’s see the code.

# Changing from float32 to float16 so that we can reduce precision
A = tf.cast(A, dtype=tf.float16)
A

# output:
<tf.Tensor: shape=(2,), dtype=float16, numpy=array([ 1.75, 85.44], dtype=float16)>

As we see the first tensor constant represents floating point 32, and the other represents int 32.

We can change the data type from floating point 32 to floating point 16.

As a result it reduces precision.

In addition, we can change the entire data type.

That means we can change the data type from int 32 to floating point 32.

# Change from int32 to float32
B = tf.cast(B, dtype=tf.float32)
B

# output: 
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([128., 777.], dtype=float32)>

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

Twitter

Comments

Leave a Reply