TensorFlow Basics: Tensors, Operations & GPU Acceleration

Master TensorFlow basics! Learn about tensors, data structures, manipulation, and GPU acceleration for deep learning and AI development.

2. TensorFlow - Basics

This section provides a foundational understanding of TensorFlow, focusing on its core data structures and fundamental operations.

Key Concepts

  • Tensor Data Structure: TensorFlow's primary data structure is the tensor. Tensors are multi-dimensional arrays, analogous to NumPy arrays, but with the added capability of being processed on GPUs for accelerated computation.
  • Tensor Handling and Manipulations: We will explore how to create, manipulate, and perform operations on tensors. This includes understanding tensor shapes, data types, and common mathematical operations.
  • Various Dimensions of TensorFlow: TensorFlow supports tensors of various dimensions, from scalar (0-dimensional) to higher-dimensional tensors. We'll cover how to work with these different dimensionalities.

Tensor Data Structure

A tensor in TensorFlow is a fundamental unit of data. It is essentially a multi-dimensional array.

  • Rank (or Number of Dimensions): The number of dimensions a tensor has.
  • Shape: The size of the tensor along each dimension.
  • Data Type (dtype): The type of data stored in the tensor (e.g., float32, int64).

Creating Tensors

You can create tensors in various ways:

  • From NumPy Arrays:

    import tensorflow as tf
    import numpy as np
    
    numpy_array = np.array([[1, 2], [3, 4]])
    tensor_from_numpy = tf.constant(numpy_array)
    print(tensor_from_numpy)
  • Using tf.constant: Create a tensor with fixed values.

    scalar_tensor = tf.constant(5)
    vector_tensor = tf.constant([1.0, 2.0, 3.0])
    matrix_tensor = tf.constant([[1, 2], [3, 4]])
  • Using tf.Variable: Create a tensor whose values can be changed during computation (useful for model parameters).

    variable_tensor = tf.Variable([1.0, 2.0, 3.0])
  • Zeros and Ones Tensors:

    zeros_tensor = tf.zeros(shape=(2, 3), dtype=tf.float32)
    ones_tensor = tf.ones(shape=[4, 2], dtype=tf.int32)
  • Random Tensors:

    random_tensor = tf.random.normal(shape=(2, 2), mean=0.0, stddev=1.0)

Tensor Handling and Manipulations

Once tensors are created, you can perform numerous operations on them.

Basic Operations

TensorFlow supports element-wise operations, similar to NumPy.

  • Addition:

    tensor_a = tf.constant([[1, 2], [3, 4]])
    tensor_b = tf.constant([[5, 6], [7, 8]])
    sum_tensor = tensor_a + tensor_b
    # or tf.add(tensor_a, tensor_b)
    print(sum_tensor)
  • Subtraction:

    difference_tensor = tensor_a - tensor_b
    # or tf.subtract(tensor_a, tensor_b)
    print(difference_tensor)
  • Multiplication (Element-wise):

    element_wise_product = tensor_a * tensor_b
    # or tf.multiply(tensor_a, tensor_b)
    print(element_wise_product)
  • Matrix Multiplication:

    matrix_product = tf.matmul(tensor_a, tensor_b)
    print(matrix_product)

Tensor Attributes

Accessing attributes of a tensor:

  • Shape:

    print(tensor_a.shape) # Output: (2, 2)
  • Data Type:

    print(tensor_a.dtype) # Output: <dtype: 'int32'>

Reshaping Tensors

You can change the shape of a tensor without changing its data.

reshaped_tensor = tf.reshape(tensor_a, shape=(4,)) # Reshape to a 1D tensor
print(reshaped_tensor)

Slicing and Indexing

Accessing specific elements or sub-sections of a tensor.

# Accessing an element
element = tensor_a[0, 1] # Gets the element at row 0, column 1 (value is 2)
print(element)

# Slicing a tensor
row_slice = tensor_a[0, :] # Gets the first row
print(row_slice)

column_slice = tensor_a[:, 1] # Gets the second column
print(column_slice)

Various Dimensions of TensorFlow

Tensors can have any number of dimensions (rank).

  • Rank 0 (Scalar): A single number.

    scalar = tf.constant(10)
    print(scalar.shape) # Output: ()
  • Rank 1 (Vector): A sequence of numbers.

    vector = tf.constant([1, 2, 3, 4])
    print(vector.shape) # Output: (4,)
  • Rank 2 (Matrix): A table of numbers (rows and columns).

    matrix = tf.constant([[1, 2], [3, 4], [5, 6]])
    print(matrix.shape) # Output: (3, 2)
  • Rank 3 and higher: Tensors with three or more dimensions are common in deep learning, especially for data like images (height, width, channels) or sequences.

    tensor_3d = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
    print(tensor_3d.shape) # Output: (2, 2, 2)