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

Data TypesData 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:

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:

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.

Tarefa

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.

Tudo estava claro?

Seção 1. Capítulo 7
toggle bottom row
course content

Conteúdo do Curso

Introduction to TensorFlow

Data TypesData 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:

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:

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.

Tarefa

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.

Tudo estava claro?

Seção 1. Capítulo 7
toggle bottom row
some-alt