Tensor Operations in Python
Tensor operations are fundamental in the field of machine learning and data science. In Python, these operations are typically handled using libraries such as NumPy and TensorFlow. This page provides an overview of tensor operations in Python.
Basic Operations
- Addition: Combine tensors element-wise.
import numpy as np tensor1 = np.array([1, 2, 3]) tensor2 = np.array([4, 5, 6]) result = np.add(tensor1, tensor2) print(result)
- Subtraction: Subtract tensors element-wise.
result = np.subtract(tensor1, tensor2) print(result)
- Multiplication: Multiply tensors element-wise.
result = np.multiply(tensor1, tensor2) print(result)
- Division: Divide tensors element-wise.
result = np.divide(tensor1, tensor2) print(result)
Advanced Operations
- Matrix Multiplication: Multiply two matrices.
matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) result = np.dot(matrix1, matrix2) print(result)
- Transpose: Transpose a matrix.
result = np.transpose(matrix1) print(result)
Learning More
For more information on tensor operations in Python, you can visit our NumPy documentation.
TensorFlow Logo