Introduction to TensorFlow and Keras
Build, train, and evaluate neural networks with TensorFlow 2.x and the Keras high-level API.
What Is TensorFlow?
TensorFlow is Google’s open-source ML framework, used across research and production at scale. TF 2.x runs eagerly by default (like PyTorch), making it Pythonic and easy to debug while retaining access to the full production ecosystem: TFX for pipelines, TF Serving for deployment, TFLite for mobile, and TPU support for massive scale.
Installation
pip install tensorflow
# or for GPU support
pip install tensorflow[and-cuda]
Your First Keras Model
import tensorflow as tf
import numpy as np
print(f"TensorFlow version: {tf.__version__}")
# Load MNIST — 60,000 handwritten digit images
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
# Preprocess
X_train = X_train.astype("float32") / 255.0 # normalize to [0, 1]
X_test = X_test.astype("float32") / 255.0
X_train = X_train.reshape(-1, 784) # flatten 28x28 to 784
X_test = X_test.reshape(-1, 784)
print(f"Training data: {X_train.shape}") # (60000, 784)
print(f"Test data: {X_test.shape}") # (10000, 784)
# Build model with Sequential API
model = tf.keras.Sequential([
tf.keras.layers.Dense(256, activation="relu", input_shape=(784,)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation="softmax"), # 10 digit classes
], name="mnist-classifier")
model.summary()
# Compile — specify optimizer, loss, and metrics
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
loss="sparse_categorical_crossentropy", # y is integer labels (not one-hot)
metrics=["accuracy"],
)
# Train
history = model.fit(
X_train, y_train,
epochs=10,
batch_size=256,
validation_split=0.1, # use 10% of training data for validation
callbacks=[
tf.keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True),
tf.keras.callbacks.ReduceLROnPlateau(patience=2, factor=0.5),
],
verbose=1,
)
# Evaluate
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=0)
print(f"\nTest accuracy: {test_acc:.4f}")
Functional API — For Complex Architectures
import tensorflow as tf
# Multi-input model: text + metadata → prediction
# Input 1: text features (e.g., TF-IDF embeddings)
text_input = tf.keras.Input(shape=(1000,), name="text_features")
x1 = tf.keras.layers.Dense(128, activation="relu")(text_input)
x1 = tf.keras.layers.Dropout(0.3)(x1)
# Input 2: numerical metadata
meta_input = tf.keras.Input(shape=(10,), name="metadata")
x2 = tf.keras.layers.Dense(32, activation="relu")(meta_input)
# Merge both branches
merged = tf.keras.layers.Concatenate()([x1, x2])
merged = tf.keras.layers.Dense(64, activation="relu")(merged)
# Output
output = tf.keras.layers.Dense(1, activation="sigmoid", name="prediction")(merged)
# Build model — defines the full computation graph
model = tf.keras.Model(
inputs=[text_input, meta_input],
outputs=output,
name="multi-input-model"
)
model.summary()
# Training with multiple inputs
import numpy as np
rng = np.random.default_rng(42)
n = 1000
X_text = rng.standard_normal((n, 1000)).astype("float32")
X_meta = rng.standard_normal((n, 10)).astype("float32")
y = rng.integers(0, 2, n).astype("float32")
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(
{"text_features": X_text, "metadata": X_meta},
y,
epochs=5,
batch_size=32,
validation_split=0.2,
)
Custom Training Loop
import tensorflow as tf
import numpy as np
# When you need more control than model.fit() provides
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = (X_train.reshape(-1, 784).astype("float32")) / 255.0
X_test = (X_test.reshape(-1, 784).astype("float32")) / 255.0
# Convert to tf.data.Dataset for efficient batching
BATCH_SIZE = 256
train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_ds = train_ds.shuffle(10000).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
model = tf.keras.Sequential([
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(10),
])
optimizer = tf.keras.optimizers.Adam(1e-3)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
train_acc = tf.keras.metrics.SparseCategoricalAccuracy()
@tf.function # compile to a graph for faster execution
def train_step(x_batch, y_batch):
with tf.GradientTape() as tape:
logits = model(x_batch, training=True)
loss = loss_fn(y_batch, logits)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_acc.update_state(y_batch, logits)
return loss
for epoch in range(5):
train_acc.reset_state()
total_loss = 0.0
batches = 0
for x_batch, y_batch in train_ds:
loss = train_step(x_batch, y_batch)
total_loss += loss.numpy()
batches += 1
avg_loss = total_loss / batches
print(f"Epoch {epoch+1}: loss={avg_loss:.4f} acc={train_acc.result():.4f}")
Saving and Loading Models
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation="relu", input_shape=(20,)),
tf.keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy")
# Save in SavedModel format — recommended for production + TF Serving
model.save("my_model") # saves as a directory
# Load it back
loaded_model = tf.keras.models.load_model("my_model")
# Save only weights (smaller, requires model definition to load)
model.save_weights("my_weights.h5")
model.load_weights("my_weights.h5")
# Keras native format (.keras) — simple, recommended for Keras models
model.save("my_model.keras")
loaded = tf.keras.models.load_model("my_model.keras")
# Convert to TFLite for mobile deployment
converter = tf.lite.TFLiteConverter.from_saved_model("my_model")
tflite_model = converter.convert()
with open("model.tflite", "wb") as f:
f.write(tflite_model) Frequently Asked Questions
What is the difference between TensorFlow and Keras?
TensorFlow is the full framework — it includes computation graphs, distributed training, model serving (TF Serving), mobile deployment (TFLite), and more. Keras is the high-level API built into TensorFlow (tf.keras) that provides a simple interface for building and training models. Most TensorFlow code uses Keras for model definition.
When should I use the Sequential API vs the Functional API?
Use Sequential for simple linear stacks of layers (most feedforward networks). Use the Functional API when you need multiple inputs or outputs, shared layers, skip connections (like ResNet), or any non-linear topology. The Functional API covers everything Sequential does, plus more.