Зміст курсу
Introduction to TensorFlow
Introduction to TensorFlow
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 (like16
intf.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
orFalse
); -
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:
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)
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 intf.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:
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)
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.
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.
- 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. - Transform the
initial_tensor
to a tensor calledconverted_tensor
with a data type of 16-bit floating point values.
Дякуємо за ваш відгук!
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 (like16
intf.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
orFalse
); -
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:
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)
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 intf.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:
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)
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.
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.
- 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. - Transform the
initial_tensor
to a tensor calledconverted_tensor
with a data type of 16-bit floating point values.
Дякуємо за ваш відгук!