Understanding Tensor Operations
In TensorFlow, a wide range of operations can be performed using its core data structure—the tensor. From basic arithmetic operations like addition, subtraction, and multiplication to more complex operations like matrix multiplication and dimension alterations, tensors are highly versatile.
In this lesson, we’ll explore how to perform fundamental tensor operations in TensorFlow.
1. Basic Operations (Addition, Subtraction, Multiplication, Division)
Basic arithmetic operations can be performed on tensors.
import tensorflow as tf # Create two 1D tensors tensor_a = tf.constant([1, 2, 3]) tensor_b = tf.constant([4, 5, 6]) # Perform basic operations add_result = tf.add(tensor_a, tensor_b) # Addition diff_result = tf.subtract(tensor_a, tensor_b) # Subtraction mul_result = tf.multiply(tensor_a, tensor_b) # Multiplication div_result = tf.divide(tensor_a, tensor_b) # Division print("Addition result:", add_result) # Addition result: tf.Tensor([5 7 9], shape=(3,), dtype=int32) print("Subtraction result:", diff_result) # Subtraction result: tf.Tensor([-3 -3 -3], shape=(3,), dtype=int32) print("Multiplication result:", mul_result) # Multiplication result: tf.Tensor([ 4 10 18], shape=(3,), dtype=int32) print("Division result:", div_result) # Division result: tf.Tensor([0.25 0.4 0.5], shape=(3,), dtype=float64)
2. Matrix Multiplication
In neural networks, operations between input data and weights are typically performed through matrix multiplication.
For example, in each layer of a neural network, output is generated by multiplying an input vector with a weight matrix followed by applying an activation function, which utilizes matrix multiplication.
tf.matmul() is used for matrix multiplication.
# Create a 2x3 matrix and a 3x2 matrix matrix_a = tf.constant([[1, 2, 3], [4, 5, 6]]) matrix_b = tf.constant([[7, 8], [9, 10], [11, 12]]) # Perform matrix multiplication matmul_result = tf.matmul(matrix_a, matrix_b) print("Matrix multiplication result:", matmul_result) # Matrix multiplication result: tf.Tensor( # [[ 58 64] # [139 154]], shape=(2, 2), dtype=int32)
3. Dimension Alterations (Reshape & Transpose)
In deep learning models, data often needs to be transformed into specific shapes.
You can alter the shape of a tensor using tf.reshape() and tf.transpose().
# Convert a 1D tensor into a 2x3 matrix reshaped_tensor = tf.reshape(tf.constant([1, 2, 3, 4, 5, 6]), (2, 3)) print("Reshaped tensor:", reshaped_tensor) # Reshaped tensor: tf.Tensor( # [[1 2 3] # [4 5 6]], shape=(2, 3), dtype=int32) # Transpose a matrix transposed_tensor = tf.transpose(reshaped_tensor) print("Transposed tensor:", transposed_tensor) # Transposed tensor: tf.Tensor( # [[1 4] # [2 5] # [3 6]], shape=(3, 2), dtype=int32)
In addition, you can perform various other tensor operations such as:
-
Aggregation Operations: Calculate sum, mean, maximum, minimum along specific dimensions
-
Broadcasting: Automatically expands a smaller tensor to a larger tensor for operations
-
tf.where(): Select values based on specified conditions
In the next lesson, we will test your understanding with a simple quiz based on what you have learned so far.
Lessons in this chapter · Machine Learning: Learning Patterns Hidden in Data
- 1. What’s All the Hype About? What Is Machine Learning?
- 2. What Are Neural Networks That Mimic the Human Brain?
- 3. How Perceptrons Work
- 4. Taking It Further: What Is Deep Learning?
- 5. Machine Learning vs. Deep Learning: Key Differences
- 6. Multiple Choice Quiz
- 7. What Does It Mean to Train AI?
- 8. The Result of AI Training: Files Made of Matrices
- 9. Supervised Learning: Learning with the Right Answers
- 10. Unsupervised Learning: Finding Patterns Without Answers
- 11. Reinforcement Learning: Learning Through Rewards
- 12. Fill-in-the-Blank Quiz
- 13. TensorFlow: A Library for Machine Learning and Deep Learning
- 14. Tensor: The Core Unit of TensorFlow
- 15. Tensor Dimensions
- 16. Multiple Choice Quiz
- 17. Understanding Tensor Operations
- 18. Top 5 Most Used TensorFlow APIs
- 19. Building a Simple Linear Regression Model with TensorFlow
- 20. Fill-in-the-Blank Quiz
- 21. Keras: A Simple and Intuitive Neural Network Library
- 22. Training and Evaluating Models with Keras
- 23. Multiple Choice Quiz
What is the function used in TensorFlow to perform matrix multiplication?
tf.add()
tf.multiply()
tf.matmul()
tf.subtract()
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help