TensorFlow Basics
Chandra Girish S
Tech Blogger | AI Evangelist & Thought Leader | Startup Mentor (Advisory) | EGMP IIM Bangalore | ISB CTO Program, Leadership with AI | DOEACC B-Level | TOGAF? Practitioner | SAFe? 6 Agilist | PRINCE2? Practitioner
What is TensorFlow?
TensorFlow is an open-source machine learning framework developed by Google Brain. It allows developers and researchers to build and deploy machine learning (ML) and deep learning models efficiently. TensorFlow offers a variety of tools for designing, training, and deploying models across multiple platforms (cloud, edge devices, web, and mobile).
Why is TensorFlow Used?
TensorFlow provides:
Where is TensorFlow Used in Deep Learning?
TensorFlow is heavily utilized in deep learning for creating and training various models. Some examples include:
What is a Tensor?
A tensor is a multi-dimensional array used to represent data in TensorFlow. It's the fundamental data structure of TensorFlow, similar to arrays or matrices in other programming languages, but with added capabilities for higher dimensions. Tensors can represent scalars, vectors, matrices, or n-dimensional arrays.
Examples of Tensors
Tensor Dimensions and Rank
Tensor Properties
What is a constant in tensforflow?
In TensorFlow, a constant is a tensor whose value is fixed and does not change during execution. It is created using the tf.constant() function and is useful when you need tensors with predefined values that won’t be modified throughout the computation
Key Characteristics of TensorFlow Constants:
Syntax: tf.constant(value, dtype=None, shape=None)
When to Use Constants?
Install TensorFlow
Make sure you have Python installed and then run the below code:
pip install tensorflow
TensorFlow Data Structure
0D Tensor (Scalar)
import tensorflow as tf
# 0D Tensor (Scalar)
scalar_tensor = tf.constant(42)
print("0D Tensor:", scalar_tensor)
print("Shape:", scalar_tensor.shape) # Output: ()
1D Tensor (Vector)
# 1D Tensor (Vector)
vector_tensor = tf.constant([1, 2, 3, 4, 5])
print("1D Tensor:", vector_tensor)
print("Shape:", vector_tensor.shape) # Output: (5,)
2D Tensor (Matrix)
# 2D Tensor (Matrix)
matrix_tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
print("2D Tensor:\n", matrix_tensor)
print("Shape:", matrix_tensor.shape) # Output: (2, 3)
3D Tensor (Cube or Volume)
# 3D Tensor (Cube or Volume)
cube_tensor = tf.constant([
[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]
])
print("3D Tensor:\n", cube_tensor)
print("Shape:", cube_tensor.shape) # Output: (2, 2, 3)
Explanation of Shapes:
Tensor with Different Data Types
Tensors can store data of different types like integers, floats, or strings.
# Tensor of floats
float_tensor = tf.constant([1.2, 3.4, 5.6], dtype=tf.float32)
# Tensor of strings
string_tensor = tf.constant(["hello", "tensorflow"])
print(float_tensor)
print(string_tensor)
Basic Tensor Operations
Addition:
import tensorflow as tf
A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
add_result = tf.add(A, B)
print("Addition:\n", add_result)
Subtraction:
sub_result = tf.subtract(A, B)
print("Subtraction:\n", sub_result)
Element-wise Multiplication of Tensors:
In element-wise multiplication, corresponding elements of two tensors are multiplied together.
import tensorflow as tf
# Define two tensors
tensor_a = tf.constant([[1, 2], [3, 4]])
tensor_b = tf.constant([[5, 6], [7, 8]])
# Element-wise multiplication
result = tf.multiply(tensor_a, tensor_b)
print("Element-wise Multiplication:\n", result)
Explanation:
Matrix Multiplication of Tensors (Dot Product)
In matrix multiplication, the dot product is calculated between corresponding rows and columns.
# Matrix multiplication using tf.matmul()
matrix_mult_result = tf.matmul(tensor_a, tensor_b)
print("Matrix Multiplication:\n", matrix_mult_result)
Explanation:
Subtraction of Tensors
In subtraction, corresponding elements are subtracted from each other.
领英推荐
# Subtract tensor_b from tensor_a
subtraction_result = tf.subtract(tensor_a, tensor_b)
print("Subtraction:\n", subtraction_result)
Explanation:
Tensor with Different Data Types
Tensors can store data of different types like integers, floats, or strings.
# Tensor of floats
float_tensor = tf.constant([1.2, 3.4, 5.6], dtype=tf.float32)
# Tensor of strings
string_tensor = tf.constant(["hello", "tensorflow"])
print(float_tensor)
print(string_tensor)
Shape, Rank, Axis, and Size of Tensor
tensor = tf.constant([[1, 2], [3, 4], [5, 6]])
print("Shape:", tensor.shape) # (3, 2)
print("Rank:", tf.rank(tensor)) # 2
print("Size:", tf.size(tensor)) # 6 elements
Tensor Indexing
tensor = tf.constant([[1, 2], [3, 4], [5, 6]])
print(tensor[0, 1]) # Access element in 1st row, 2nd column -> 2
Tensor Reshaping
tensor = tf.constant([1, 2, 3, 4, 5, 6])
reshaped = tf.reshape(tensor, (2, 3)) # 2x3 matrix
print(reshaped)
Tensor Transpose
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
transposed = tf.transpose(tensor)
print(transposed)
Tensor Broadcasting
tensor_a = tf.constant([[1], [2], [3]]) # Shape: (3, 1)
tensor_b = tf.constant([1, 2, 3]) # Shape: (3,)
result = tensor_a + tensor_b
print(result) # Broadcasts to (3,3) shape
Tensor Slicing
tensor = tf.constant([1, 2, 3, 4, 5])
sliced_tensor = tensor[1:4] # Elements from index 1 to 3
print(sliced_tensor) # [2, 3, 4]
Random Number Generation
random_tensor = tf.random.normal([2, 2], mean=0, stddev=1)
print(random_tensor)
Ragged Tensors
Ragged tensors have rows of different lengths.
ragged_tensor = tf.ragged.constant([[1, 2], [3, 4, 5]])
print(ragged_tensor)
Tensor Concatenation
a = tf.constant([[1, 2]])
b = tf.constant([[3, 4]])
concat_result = tf.concat([a, b], axis=0) # Concatenating along rows
print(concat_result)
Variables in TensorFlow
Variables are tensors whose values can be changed.
var = tf.Variable([1, 2, 3])
var.assign([4, 5, 6])
print(var)
Creating a Simple Linear Model
Let’s build a simple linear regression model to fit y = 2x + 1.
import tensorflow as tf
# Input data
X = tf.constant([[1.0], [2.0], [3.0], [4.0]], dtype=tf.float32)
Y = tf.constant([[3.0], [5.0], [7.0], [9.0]], dtype=tf.float32)
# Initialize weight and bias
W = tf.Variable([0.0], dtype=tf.float32)
b = tf.Variable([0.0], dtype=tf.float32)
# Define the linear model
def linear_model(X):
return W * X + b
# Loss function: Mean Squared Error
def loss(Y_pred, Y_true):
return tf.reduce_mean(tf.square(Y_pred - Y_true))
# Optimizer
optimizer = tf.optimizers.SGD(learning_rate=0.01)
# Training loop
for epoch in range(100):
with tf.GradientTape() as tape:
Y_pred = linear_model(X)
current_loss = loss(Y_pred, Y)
# Compute gradients and update weights
gradients = tape.gradient(current_loss, [W, b])
optimizer.apply_gradients(zip(gradients, [W, b]))
if epoch % 10 == 0:
print(f"Epoch {epoch}: Loss = {current_loss.numpy()}")
print(f"\nTrained Weight: {W.numpy()[0]}, Bias: {b.numpy()[0]}")
Code Explanation:
Input Data
Initialize Weight and Bias
Define the Linear Model
Define the Loss Function
Define the Optimizer
Training Loop
Final Weight and Bias
Output
Epoch 0: Loss = 55.0
Epoch 10: Loss = 1.0125
Epoch 20: Loss = 0.03746875
...
Epoch 90: Loss = 0.00032407963
Trained Weight: 2.0, Bias: 1.0
What Each Part of the Output Means
Epoch 0: Loss = 55.0
Initial Loss: This is the loss after the first iteration (epoch 0) before any significant updates to the parameters W (weight) and b (bias).
High Loss: A large initial loss (55.0) indicates that the model’s predictions (Y_pred) are very far from the actual values (Y) at the start, as the initial values for W and b were both set to 0.0.
Epoch 10: Loss = 1.0125
After 10 epochs, the loss has decreased to 1.0125. This shows that the model is improving by adjusting the parameters (W and b) in the right direction, bringing predictions closer to the actual values.
Gradient Descent is Working: The optimizer (SGD) is successfully minimizing the difference between the predicted and actual values.
Epoch 20: Loss = 0.03746875
As the training progresses, the loss further decreases. By epoch 20, the loss is very small (0.037), indicating that the predictions are becoming more accurate.
Epoch 90: Loss = 0.00032407963
At epoch 90, the loss is very close to 0, meaning the model's predictions are nearly perfect for the given input data. This suggests that the model has almost perfectly learned the relationship y=2x+1.
Explanation of Loss Values Across Epochs
The loss decreases over time because the optimizer (SGD) continuously adjusts the parameters W and b to minimize the difference between predicted and actual values. The key idea is to reduce the error between the predicted outputs (Y_pred) and the actual outputs (Y) with each epoch, resulting in a smaller loss value.
High Initial Loss (Epoch 0):
Since both W and b were initialized to 0.0, the initial predictions are all 0.0. This causes a large error between predicted and actual values, resulting in a high loss.
Loss Decreases Over Time:
As the model learns, the weight and bias are gradually updated, and predictions get closer to the actual outputs.
Near Zero Loss (Epoch 90):
By epoch 90, the loss becomes extremely small, meaning that the model has effectively learned the correct relationship between X and Y.
For more in-depth technical insights and articles, feel free to explore:
"Have you used TensorFlow? Share your experiences or ask questions in the comments!"
Cloud-Native (AWS, GCP & Azure) Software & AI Architect | Leading Machine Learning, Artificial Intelligence and MLOps Programs | Generative AI | Coding and Mentoring
1 个月Very informative