Tensors are like multi-dimensional arrays. Doing math with tensors helps computers learn patterns and solve problems.
0
0
Tensor math operations in TensorFlow
Introduction
When you want to add or multiply data in machine learning models.
When you need to calculate predictions from input data.
When you want to combine or transform data in neural networks.
When you want to compute loss or accuracy during training.
When you want to apply mathematical functions like square root or exponent.
Syntax
TensorFlow
import tensorflow as tf # Basic math operations result = tf.add(tensor1, tensor2) # addition result = tf.subtract(tensor1, tensor2) # subtraction result = tf.multiply(tensor1, tensor2) # element-wise multiplication result = tf.divide(tensor1, tensor2) # element-wise division # Other operations result = tf.matmul(tensor1, tensor2) # matrix multiplication result = tf.sqrt(tensor) # square root result = tf.exp(tensor) # exponentiation
Use tf.add, tf.subtract, tf.multiply, and tf.divide for element-wise math.
Use tf.matmul for matrix multiplication, which is different from element-wise multiplication.
Examples
Adds two 1D tensors element-wise: [1+4, 2+5, 3+6] = [5, 7, 9]
TensorFlow
a = tf.constant([1, 2, 3]) b = tf.constant([4, 5, 6]) sum_ab = tf.add(a, b)
Performs matrix multiplication of two 2x2 tensors.
TensorFlow
a = tf.constant([[1, 2], [3, 4]]) b = tf.constant([[5, 6], [7, 8]]) product = tf.matmul(a, b)
Computes square root of each element: [3.0, 4.0, 5.0]
TensorFlow
a = tf.constant([9.0, 16.0, 25.0]) sqrt_a = tf.sqrt(a)
Sample Model
This program shows basic tensor math: addition, element-wise multiplication, matrix multiplication, and square root.
TensorFlow
import tensorflow as tf # Define two tensors x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) y = tf.constant([[5, 6], [7, 8]], dtype=tf.float32) # Element-wise addition add_result = tf.add(x, y) # Element-wise multiplication mul_result = tf.multiply(x, y) # Matrix multiplication matmul_result = tf.matmul(x, y) # Square root of elements in x sqrt_x = tf.sqrt(x) # Print results print("Addition:\n", add_result.numpy()) print("Element-wise Multiplication:\n", mul_result.numpy()) print("Matrix Multiplication:\n", matmul_result.numpy()) print("Square Root of x:\n", sqrt_x.numpy())
OutputSuccess
Important Notes
Tensors must have compatible shapes for operations like addition or multiplication.
Matrix multiplication requires the inner dimensions to match (e.g., (2,3) x (3,4)).
Use .numpy() to convert tensors to arrays for easy printing.
Summary
Tensors are multi-dimensional arrays used in machine learning.
Tensor math operations include element-wise and matrix multiplication.
TensorFlow provides simple functions like tf.add and tf.matmul to do these operations.