Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Data Types | Tensors
Introduction to TensorFlow
course content

Course Content

Introduction to TensorFlow

Introduction to TensorFlow

1. Tensors
2. Basics of TensorFlow

book
Data Types

Data Types

Welcome back, explorers of the TensorFlow realm! Today's quest involves navigating the world of data types in TensorFlow. Just as numbers and text have various types in programming languages (like integers, floats, or strings), tensors have their own set of types.

Available Data Types in TensorFlow

TensorFlow supports a plethora of data types to cater to different kinds of data and operations. Some of the most commonly used types are:

  • tf.float16, tf.float32, tf.float64: These are floating-point numbers where the numbers after the dot matter. The number in their name (like 16 in tf.float16) tells you about the number of bits used. tf.float32 is commonly used because it offers a good balance between precision and computation speed;

  • tf.int8, tf.int16, tf.int32, tf.int64: These are integer types, numbers without a decimal point. They can be both positive and negative;

  • tf.uint8, tf.uint16, tf.uint32, tf.uint64: 'u' here stands for 'unsigned', meaning these integers are always non-negative;

  • tf.bool: Represents boolean values (True or False);

  • tf.string: For text data.

Note

For integer types, a larger number in the name only signifies a wider range of values it can store. But for floating-point types, a higher number in the name also indicates greater computational accuracy.

There are more data types available in TensorFlow, but for beginners, it's crucial to get familiarized with these primary types first. For a comprehensive list of data types supported by TensorFlow, consider checking out this specific page in the TensorFlow documentation.

Setting the Data Type when Creating a Tensor

When you're initializing a tensor, you can specify its type using the dtype argument:

123456789101112
import tensorflow as tf # Creating a tensor of type `float16` tensor_float = tf.constant([1.2, 2.3, 3.4], dtype=tf.float16) # Creating a tensor of type `int64` tensor_int = tf.constant([1, 2, 3], dtype=tf.int64) # Display tensors print(tensor_float) print('-' * 50) print(tensor_int)
copy

Note

Many tensor creation functions adopt this approach. It's applicable in methods such as tf.Variable(), tf.zeros(), tf.ones(), tf.random.normal(), tf.random.uniform(), and even in tf.convert_to_tensor().

Converting Between Data Types

But what if the function you're using doesn't allow for direct data type specification? Or perhaps you already possess a tensor of a certain type and need to change it. In these cases, you'll need to transform tensors from one data type to another. This is especially relevant as certain neural network operations or layers often demand inputs of a particular type, predominantly floating point numbers.

You can use tf.cast() to achieve this:

123456789101112
import tensorflow as tf # Create a tensor tensor_float = tf.constant([-1.2, 2.3, 3.8]) # Convert our `tensor_float` from `float32` to `int32` tensor_int_converted = tf.cast(tensor_float, dtype=tf.int32) # Display a tensor print(tensor_float) print('-' * 50) print(tensor_int_converted)
copy

Remember, while converting from a floating-point type to an integer type, TensorFlow will perform floor operation, essentially dropping the decimal part. So, 3.8 becomes 3, and -1.2 becomes -1.

Note

Be cautious while changing data types, especially when you move to a type with less precision. You might lose information in the process.

Task
test

Swipe to show code editor

Your task is to create a tensor of a specific data type and then convert it to another data type.

  1. Create a tensor named initial_tensor of shape (3, 3) with normally distributed values. Ensure that this tensor possesses a data type of 64-bit floating point values.
  2. Transform the initial_tensor to a tensor called converted_tensor with a data type of 16-bit floating point values.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
toggle bottom row

book
Data Types

Data Types

Welcome back, explorers of the TensorFlow realm! Today's quest involves navigating the world of data types in TensorFlow. Just as numbers and text have various types in programming languages (like integers, floats, or strings), tensors have their own set of types.

Available Data Types in TensorFlow

TensorFlow supports a plethora of data types to cater to different kinds of data and operations. Some of the most commonly used types are:

  • tf.float16, tf.float32, tf.float64: These are floating-point numbers where the numbers after the dot matter. The number in their name (like 16 in tf.float16) tells you about the number of bits used. tf.float32 is commonly used because it offers a good balance between precision and computation speed;

  • tf.int8, tf.int16, tf.int32, tf.int64: These are integer types, numbers without a decimal point. They can be both positive and negative;

  • tf.uint8, tf.uint16, tf.uint32, tf.uint64: 'u' here stands for 'unsigned', meaning these integers are always non-negative;

  • tf.bool: Represents boolean values (True or False);

  • tf.string: For text data.

Note

For integer types, a larger number in the name only signifies a wider range of values it can store. But for floating-point types, a higher number in the name also indicates greater computational accuracy.

There are more data types available in TensorFlow, but for beginners, it's crucial to get familiarized with these primary types first. For a comprehensive list of data types supported by TensorFlow, consider checking out this specific page in the TensorFlow documentation.

Setting the Data Type when Creating a Tensor

When you're initializing a tensor, you can specify its type using the dtype argument:

123456789101112
import tensorflow as tf # Creating a tensor of type `float16` tensor_float = tf.constant([1.2, 2.3, 3.4], dtype=tf.float16) # Creating a tensor of type `int64` tensor_int = tf.constant([1, 2, 3], dtype=tf.int64) # Display tensors print(tensor_float) print('-' * 50) print(tensor_int)
copy

Note

Many tensor creation functions adopt this approach. It's applicable in methods such as tf.Variable(), tf.zeros(), tf.ones(), tf.random.normal(), tf.random.uniform(), and even in tf.convert_to_tensor().

Converting Between Data Types

But what if the function you're using doesn't allow for direct data type specification? Or perhaps you already possess a tensor of a certain type and need to change it. In these cases, you'll need to transform tensors from one data type to another. This is especially relevant as certain neural network operations or layers often demand inputs of a particular type, predominantly floating point numbers.

You can use tf.cast() to achieve this:

123456789101112
import tensorflow as tf # Create a tensor tensor_float = tf.constant([-1.2, 2.3, 3.8]) # Convert our `tensor_float` from `float32` to `int32` tensor_int_converted = tf.cast(tensor_float, dtype=tf.int32) # Display a tensor print(tensor_float) print('-' * 50) print(tensor_int_converted)
copy

Remember, while converting from a floating-point type to an integer type, TensorFlow will perform floor operation, essentially dropping the decimal part. So, 3.8 becomes 3, and -1.2 becomes -1.

Note

Be cautious while changing data types, especially when you move to a type with less precision. You might lose information in the process.

Task
test

Swipe to show code editor

Your task is to create a tensor of a specific data type and then convert it to another data type.

  1. Create a tensor named initial_tensor of shape (3, 3) with normally distributed values. Ensure that this tensor possesses a data type of 64-bit floating point values.
  2. Transform the initial_tensor to a tensor called converted_tensor with a data type of 16-bit floating point values.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt