Core Concepts & Tensors
TensorFlow: Core Concepts & Tensors TensorFlow is Google's open-source machine learning framework. TF 2.x uses eager execution by default (compute immediately) …
TensorFlow: Core Concepts & Tensors
TensorFlow is Google's open-source machine learning framework. TF 2.x uses eager execution by default (compute immediately) and Keras as its primary high-level API. Used for neural networks, computer vision, NLP, and time-series.
TensorFlow vs PyTorch
TensorFlow: better for production deployment (TFLite, TF Serving, TF.js), stronger mobile support, TensorBoard built-in
PyTorch: more pythonic, preferred in research, easier debugging, dynamic computation graph
Both have converged significantly — Keras 3 supports both backends
JAX: growing alternative (Google), functional style, excellent for custom gradients
Tensors
import tensorflow as tf
import numpy as np
# Create tensors
scalar = tf.constant(3.14) # rank-0 tensor
vector = tf.constant([1.0, 2.0, 3.0]) # rank-1, shape (3,)
matrix = tf.constant([[1, 2], [3, 4]]) # rank-2, shape (2, 2)
tensor3d = tf.zeros([batch, height, width]) # shape (b, h, w)
# Tensor properties
matrix.shape # TensorShape([2, 2])
matrix.dtype # tf.int32
matrix.numpy() # convert to numpy array
# Operations (eager by default in TF2)
a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
tf.add(a, b) # element-wise addition
tf.matmul(a, b) # matrix multiplication
tf.reduce_sum(a) # sum all elements
tf.reduce_mean(a, axis=0) # mean along axis
tf.transpose(a)
tf.reshape(a, [4]) # flatten to [4]
# Variables (trainable parameters)
w = tf.Variable([[0.1, 0.2], [0.3, 0.4]], trainable=True)
w.assign(new_value)
w.assign_add(delta)Automatic Differentiation
# GradientTape — compute gradients
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x ** 2 + 2 * x + 1 # y = x² + 2x + 1
dy_dx = tape.gradient(y, x) # dy/dx = 2x + 2 = 8.0
# Multiple variables
w = tf.Variable(2.0)
b = tf.Variable(1.0)
with tf.GradientTape() as tape:
y_pred = w * x + b
loss = (y_pred - 5.0) ** 2
grads = tape.gradient(loss, [w, b]) # [dL/dw, dL/db]Data Pipelines with tf.data
import tensorflow as tf
# From numpy/lists
dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
# Efficient pipeline
dataset = (
dataset
.shuffle(buffer_size=1000, seed=42)
.batch(32)
.prefetch(tf.data.AUTOTUNE) # overlap preprocessing with training
)
# From files
image_dataset = tf.keras.utils.image_dataset_from_directory(
'data/images/',
image_size=(224, 224),
batch_size=32,
validation_split=0.2,
subset='training',
seed=42,
)
# Map preprocessing
def preprocess(image, label):
image = tf.cast(image, tf.float32) / 255.0
return image, label
dataset = dataset.map(preprocess, num_parallel_calls=tf.data.AUTOTUNE)