Machine Learning with TensorFlow
Build, train, and deploy neural networks using TensorFlow and the Keras API.
AI/MLAdvanced25 min read
AI Summary
Quick context for Machine Learning with TensorFlow
Machine Learning with TensorFlow is a advanced guide in AI/ML. Build, train, and deploy neural networks using TensorFlow and the Keras API.. Key topics: machine-learning, neural-networks, python, tensorflow.
## What Is Machine Learning?
Machine learning is a subset of artificial intelligence where systems learn patterns from data to make predictions or decisions without being explicitly programmed. TensorFlow, developed by Google, is one of the most widely used frameworks for building and deploying ML models at scale.
This guide covers the core concepts, from tensor operations to training production-ready neural networks.
## Setting Up Your Environment
```bash
# Create a virtual environment
python -m venv tf-env
source tf-env/bin/activate
# Install TensorFlow (includes Keras)
pip install tensorflow numpy matplotlib
# Verify GPU availability
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
```
Use **Visual Studio Code** with the Python extension for an efficient development experience. Track experiments and collaborate via **GitHub**.
## Tensors: The Fundamental Data Structure
Tensors are multi-dimensional arrays, similar to NumPy arrays but with GPU acceleration and automatic differentiation.
```python
import tensorflow as tf
# Scalar (0-D tensor)
scalar = tf.constant(42)
print(scalar.shape) # ()
# Vector (1-D tensor)
vector = tf.constant([1.0, 2.0, 3.0])
print(vector.shape) # (3,)
# Matrix (2-D tensor)
matrix = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
print(matrix.shape) # (2, 2)
# Operations are vectorized
result = matrix * 2 + 1
print(result)
# [[3. 5.]
# [7. 9.]]
```
## Building a Neural Network
The Keras Sequential API is the simplest way to stack layers into a model.
```python
import tensorflow as tf
from tensorflow import keras
# Build a model for classifying handwritten digits (MNIST)
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # 784 pixels
keras.layers.Dense(128, activation="relu"), # Hidden layer
keras.layers.Dropout(0.2), # Regularization
keras.layers.Dense(10, activation="softmax") # Output: 10 classes
])
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
model.summary()
```
## Training the Model
```python
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Normalize pixel values to [0, 1]
x_train = x_train / 255.0
x_test = x_test / 255.0
# Train with validation split
history = model.fit(
x_train, y_train,
epochs=10,
batch_size=32,
validati
callbacks=[
keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True),
keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=2)
]
)
# Evaluate on test set
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")
```
## Visualizing Training
```python
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
# Accuracy
ax1.plot(history.history["accuracy"], label="Train")
ax1.plot(history.history["val_accuracy"], label="Validation")
ax1.set_title("Accuracy")
ax1.set_xlabel("Epoch")
ax1.legend()
# Loss
ax2.plot(history.history["loss"], label="Train")
ax2.plot(history.history["val_loss"], label="Validation")
ax2.set_title("Loss")
ax2.set_xlabel("Epoch")
ax2.legend()
plt.tight_layout()
plt.savefig("training_history.png")
plt.show()
```
## Convolutional Neural Networks (CNNs)
CNNs are the backbone of image recognition tasks. They learn spatial hierarchies of features through convolutional layers.
```python
model_cnn = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), activation="relu"),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), activation="relu"),
keras.layers.Flatten(),
keras.layers.Dense(64, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation="softmax")
])
model_cnn.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
# Reshape data for CNN (add channel dimension)
x_train_cnn = x_train.reshape(-1, 28, 28, 1)
x_test_cnn = x_test.reshape(-1, 28, 28, 1)
model_cnn.fit(x_train_cnn, y_train, epochs=10, validati
```
## Transfer Learning
Leverage pre-trained models to solve new problems with limited data.
```python
# Load a pre-trained MobileNetV2 without the classification head
base_model = keras.applications.MobileNetV2(
input_shape=(224, 224, 3),
include_top=False,
weights="imagenet"
)
# Freeze the base model
base_model.trainable = False
# Build a custom classifier on top
model_transfer = keras.Sequential([
base_model,
keras.layers.GlobalAveragePooling2D(),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dropout(0.3),
keras.layers.Dense(5, activation="softmax") # 5 custom classes
])
model_transfer.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.001),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
model_transfer.summary()
```
## Saving and Loading Models
```python
# Save the entire model
model.save("mnist_model.keras")
# Load it later
loaded_model = keras.models.load_model("mnist_model.keras")
# Save just the weights
model.save_weights("model_weights.weights.h5")
# Load weights into a matching architecture
model.load_weights("model_weights.weights.h5")
```
## Making Predictions
```python
import numpy as np
# Predict on a single image
sample = x_test[0].reshape(1, 28, 28)
prediction = model.predict(sample)
predicted_class = np.argmax(prediction)
c
print(f"Predicted class: {predicted_class}, Confidence: {confidence:.2%}")
# Batch prediction
predicti
predicted_classes = np.argmax(predictions, axis=1)
```
## Hyperparameter Tuning
Finding the right configuration is often the difference between a mediocre and excellent model.
```python
import keras_tuner as kt
def build_model(hp):
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28, 28)))
# Tune the number of units in the first Dense layer
hp_units = hp.Int("units", min_value=32, max_value=256, step=32)
model.add(keras.layers.Dense(units=hp_units, activation="relu"))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(10, activation="softmax"))
# Tune the learning rate
hp_learning_rate = hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
return model
tuner = kt.Hyperband(
build_model,
objective="val_accuracy",
max_epochs=20,
directory="tuner_results"
)
tuner.search(x_train, y_train, epochs=10, validati
best_model = tuner.get_best_models(num_models=1)[0]
```
## Deploying Your Model
Once trained, deploy your model to production:
- **TensorFlow Serving**: High-performance model serving via REST or gRPC.
- **TensorFlow Lite**: Run models on mobile and edge devices.
- **TensorFlow.js**: Run models in the browser.
- **Docker + FastAPI**: Wrap your model in a microservice.
```python
# Convert to TensorFlow Lite for mobile deployment
c
tflite_model = converter.convert()
with open("model.tflite", "wb") as f:
f.write(tflite_model)
```
## Best Practices
- Start with simple models and increase complexity only when needed.
- Always split data into training, validation, and test sets.
- Use data augmentation to improve generalization with small datasets.
- Monitor for overfitting: if validation loss increases while training loss decreases, add regularization.
- Version your datasets and models alongside your code on **GitHub**.
## Next Steps
Explore **TensorFlow Extended (TFX)** for production ML pipelines, **TensorFlow Hub** for pre-trained models, and **KerasCV** and **KerasNLP** for domain-specific applications. For distributed training across multiple GPUs or machines, investigate `tf.distribute.Strategy`.
Continue with related content
Suggested next reads and tools from the knowledge graph.