Common TensorFlow data types in Python
Python TensorFlow Basic: Exercise-13 with Solution
Write a Python code to list some common TensorFlow data types available.
Sample Solution:
Python Code:
import tensorflow as tf
# List of common TensorFlow data types
common_data_types = [
tf.bfloat16, # 16-bit bfloat (brain floating point).
tf.bool, # Boolean.
tf.complex128, # 128-bit complex.
tf.complex64, # 64-bit complex.
tf.double, # 64-bit (double precision) floating-point.
tf.float16, # 16-bit (half precision) floating-point.
tf.float32, # 32-bit (single precision) floating-point.
tf.float64, # 64-bit (double precision) floating-point.
tf.half, # 16-bit (half precision) floating-point.
tf.int8, # Signed 8-bit integer.
tf.int16, # Signed 16-bit integer.
tf.int32, # Signed 32-bit integer.
tf.int64, # Signed 64-bit integer.
tf.qint32, # Signed quantized 32-bit integer.
tf.qint8, # Signed quantized 8-bit integer.
tf.quint16, # Unsigned quantized 16-bit integer.
tf.quint8, # Unsigned quantized 8-bit integer.
tf.resource, # Handle to a mutable, dynamically allocated resource.
tf.uint8, # Unsigned 8-bit (byte) integer.
tf.uint16, # Unsigned 16-bit (word) integer.
tf.uint32, # Unsigned 32-bit (dword) integer.
tf.uint64, # Unsigned 64-bit (qword) integer.
tf.string, # Variable-length string, represented as byte array.
tf.variant # Data of arbitrary type (known at runtime).
]
# Print the data types and their names
for dtype in common_data_types:
print(f"{dtype}: {tf.as_dtype(dtype).name}")
Output:
<dtype: 'bfloat16'>: bfloat16 <dtype: 'bool'>: bool <dtype: 'complex128'>: complex128 <dtype: 'complex64'>: complex64 <dtype: 'float64'>: float64 <dtype: 'float16'>: float16 <dtype: 'float32'>: float32 <dtype: 'float64'>: float64 <dtype: 'float16'>: float16 <dtype: 'int8'>: int8 <dtype: 'int16'>: int16 <dtype: 'int32'>: int32 <dtype: 'int64'>: int64 <dtype: 'qint32'>: qint32 <dtype: 'qint8'>: qint8 <dtype: 'quint16'>: quint16 <dtype: 'quint8'>: quint8 <dtype: 'resource'>: resource <dtype: 'uint8'>: uint8 <dtype: 'uint16'>: uint16 <dtype: 'uint32'>: uint32 <dtype: 'uint64'>: uint64 <dtype: 'string'>: string <dtype: 'variant'>: variant
Explanation:
In the exercise above -
- Import TensorFlow as tf.
- Create a list named common_data_types containing various common TensorFlow data types, such as floating-point types (e.g., tf.float32), integer types (e.g., tf.int64), unsigned integer types (e.g., tf.uint8), boolean (tf.bool), and string (tf.string) data types.
- Use a for loop to iterate through the list and print each data type along with its name using tf.as_dtype(dtype).name.
Python Code Editor:
Previous: Updating TensorFlow variables in Python.
Next: Specified TensorFlow Data Type in Python.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics