Tensor Data type in Tensorflow
Last Updated :
20 Feb, 2024
In the realm of data science and machine learning, understanding the tensor data type is fundamental, particularly when working with TensorFlow. Tensors are the core data structures used in TensorFlow to represent and manipulate data. This article explores the concept of tensors in the context of a data science project using TensorFlow.
What are tensors?
Tensors are multi-dimensional arrays that can hold data of different types. In TensorFlow, tensors are the primary building blocks for creating computational graphs and performing operations in machine learning models. Tensors can be of various ranks, including scalars, vectors, matrices, and higher-dimensional arrays.
Data Types in TensorFlow
The datatypes in Python are as follows:
Data type |
Python type |
Description
|
DT_FLOAT
|
tf.float32
|
32-bit floating point.
|
DT_DOUBLE
|
tf.float64 |
64-bit floating point.
|
DT_INT8
|
tf.int8
|
8-bit signed integer.
|
DT_INT16
|
tf.int16
|
16-bit signed integer.
|
DT_INT32
|
tf.int32
|
32-bit signed integer.
|
DT_INT64
|
tf.int64
|
64-bit signed integer.
|
DT_UINT8
|
tf.uint8
|
8-bit unsigned integer.
|
DT_STRING
|
tf.string
|
Variable length byte arrays. Each element of a tensor is a byte array.
|
DT_BOOL
|
tf.bool
|
Boolean.
|
DT_COMPLEX64
|
tf.complex64 |
Complex number made of two 32 bits floating points, real and imaginary parts. |
DT_COMPLEX128
|
tf.complex128
|
Complex number made up of two 64 bits .
|
Tensor Ranks:
Tensor rank refers to the number of dimensions in a tensor. Scalars are rank-0 tensors, vectors are rank-1 tensors, matrices are rank-2 tensors, and tensors with more than two dimensions are considered to have a rank greater than 2. Each dimension of a tensor corresponds to a mode of variability in the data it represents.
Tensor Shapes:
The shape of a tensor tells you the number of elements along each dimension. For example, a 2×3 matrix has 2 rows and 3 columns, so its shape is (2, 3). In TensorFlow or PyTorch, you can use the .shape
attribute to get the shape of a tensor.
Tensor Types:
tf.constant
: These are immutable tensors initialized once and cannot be changed.
tf.Variable
: These are mutable tensors that can be modified after creation.
tf.placeholder
: These were widely used in TensorFlow 1.x for feeding data into the computational graph, but in TensorFlow 2.x, eager execution is the default mode, and placeholders are no longer used. Instead, you can directly pass inputs as arguments to your TensorFlow functions and methods.
tf.Tensor
: This represents the result of operations in TensorFlow 2.x when using eager execution. It’s the result of computations, not a placeholder for inputs. Eager execution allows TensorFlow code to be more like regular Python code, making it easier to understand and debug.
Common data types and their operations
In TensorFlow, various data types are used to represent different kinds of data. Here are some common data types and their operations:
- Scalar: A scalar is a single numerical value. In TensorFlow, scalars are represented as tensors with a shape of
()
. Operations on scalars are typically arithmetic operations like addition, subtraction, multiplication, and division. For example, in TensorFlow, you can add two scalars using the tf.add
function.
- Vector: A vector is a one-dimensional array of values. In TensorFlow, vectors are represented as tensors with a shape like
(n,)
, where n
is the size of the vector. Operations on vectors can include element-wise operations (e.g., element-wise addition, multiplication) and linear algebra operations (e.g., dot product, cross product).
- Matrix: A matrix is a two-dimensional array of values. In TensorFlow, matrices are represented as tensors with a shape like
(m, n)
, where m
is the number of rows and n
is the number of columns. Operations on matrices include matrix multiplication, transpose, and determinant calculation.
- Tensor: A tensor is a generalization of scalars, vectors, and matrices to potentially higher dimensions. In TensorFlow, tensors can have any number of dimensions. Common operations on tensors include element-wise operations, reduction operations (e.g., sum, mean), and reshaping operations (e.g., flattening, reshaping to a different shape).
- Complex: TensorFlow also supports complex numbers, which are represented as tensors with a complex data type (
tf.complex64
or tf.complex128
). Operations on complex numbers include arithmetic operations (e.g., addition, multiplication) and complex-specific operations (e.g., complex conjugate).
Creating and Manipulating Tensors
Let’s create tensors and perform basic operations using TensorFlow.
Python3
import tensorflow as tf
scalar_tensor = tf.constant( 42 )
vector_tensor = tf.constant([ 1.0 , 2.0 , 3.0 ])
matrix_tensor = tf.constant([[ 1 , 2 ], [ 3 , 4 ]])
result_tensor = scalar_tensor * matrix_tensor
print ( "Scalar Tensor:" , scalar_tensor.numpy())
print ( "Vector Tensor:" , vector_tensor.numpy())
print ( "Matrix Tensor:\n" , matrix_tensor.numpy())
print ( "Result Tensor (Scalar * Matrix):\n" , result_tensor.numpy())
|
Output:
Scalar Tensor: 42
Vector Tensor: [1.0, 2.0, 3.0]
Matrix Tensor:
[[1, 2],
[3, 4]]
Result Tensor (Scalar * Matrix):
[[42, 84],
[126, 168]]
Changing Data Types
Let’s demonstrate changing the data type of a tensor.
Python3
original_tensor = tf.constant([ 1.5 , 2.5 , 3.5 ], dtype = tf.float32)
converted_tensor = tf.cast(original_tensor, dtype = tf.int32)
print ( "Original Tensor (Float):" , original_tensor.numpy())
print ( "Converted Tensor (Int):" , converted_tensor.numpy())
|
Output:
Original Tensor (Float): [1.5, 2.5, 3.5]
Converted Tensor (Int): [1, 2, 3]
Operations on Complex Numbers
We create two complex numbers, complex1
and complex2
, and perform arithmetic operations (addition and multiplication) as well as a complex-specific operation (conjugate). The result of each operation is also a complex number,
Python3
complex1 = tf. complex ( 1.0 , 2.0 )
complex2 = tf. complex ( 3.0 , 4.0 )
complex_sum = tf.add(complex1, complex2)
complex_product = tf.multiply(complex1, complex2)
complex_conj = tf.math.conj(complex1)
print ( "Complex sum:" , complex_sum)
print ( "Complex product:" , complex_product)
print ( "Complex conjugate:" , complex_conj)
|
Output:
Complex sum: tf.Tensor((4+6j), shape=(), dtype=complex64)
Complex product: tf.Tensor((-5+10j), shape=(), dtype=complex64)
Complex conjugate: tf.Tensor((1-2j), shape=(), dtype=complex64)
Conclusion:
In conclusion, tensors are fundamental in TensorFlow for data science and machine learning, serving as versatile multi-dimensional arrays. This article explored tensor concepts, including ranks and data types, and provided practical examples for creating, manipulating, and changing tensor types in TensorFlow. The outlined steps offer a concise guide for incorporating tensors into data science projects, underscoring their importance and adaptability within the machine learning landscape.
Share your thoughts in the comments
Please Login to comment...