Deep Learning is a branch of Machine Learning that uses neural networks with many layers (hence "deep") to learn patterns from vast data. It's used in voice assistants, facial recognition, AI art, and more.
# Print a welcome message
print("Welcome to Deep Learning!")
# Output: Welcome to Deep Learning!
# Show how deep learning relates to AI and ML
print("AI ⟶ ML ⟶ Deep Learning")
# Output: AI ⟶ ML ⟶ Deep Learning
# Simulate AI recognizing an image
image = "cat picture"
if "cat" in image:
print("Deep Learning: It's a cat!")
# Output: Deep Learning: It's a cat!
AI is the umbrella term. ML is AI that learns from data. DL is ML using deep neural networks. Think of it as AI ⟶ ML ⟶ DL.
AI = "Smart System"
ML = AI + " that learns from data"
DL = ML + " using neural networks"
print(DL)
# Output: Smart System that learns from data using neural networks
# Classify example
def classify(level):
if level == "AI":
return "Broad concept including all intelligent behavior"
elif level == "ML":
return "AI that learns from data"
elif level == "DL":
return "ML using deep neural nets"
print(classify("DL"))
# Output: ML using deep neural nets
# Layered logic
concepts = ["AI", "ML", "DL"]
for c in concepts:
print("Learning level:", c)
# Output:
# Learning level: AI
# Learning level: ML
# Learning level: DL
Deep learning is used in many areas: speech recognition, medical imaging, self-driving cars, and more.
# List some uses
apps = ["Voice assistants", "Self-driving cars", "Chatbots"]
for app in apps:
print("Used in:", app)
# Output:
# Used in: Voice assistants
# Used in: Self-driving cars
# Used in: Chatbots
# Recommend an app for vision
def recommend(task):
if task == "vision":
return "Use Deep Learning for image recognition"
print(recommend("vision"))
# Output: Use Deep Learning for image recognition
# Medical scan detection
scan = "abnormal scan image"
if "abnormal" in scan:
print("AI alert: Check patient's scan")
# Output: AI alert: Check patient's scan
Neural networks are inspired by the human brain. They contain layers of nodes (neurons) connected by weights. Data flows through these layers to produce outputs.
inputs = [1.0, 2.0]
weights = [0.5, 0.75]
output = sum(i * w for i, w in zip(inputs, weights))
print("Output:", output)
# Output: Output: 2.0
# Add bias term
bias = 1.0
output = sum(i * w for i, w in zip(inputs, weights)) + bias
print("Output with bias:", output)
# Output: Output with bias: 3.0
# Use multiple layers (manually simulated)
def simple_layer(x):
return x * 2 + 1
print("Layer 1:", simple_layer(3))
# Output: Layer 1: 7
Activation functions like ReLU, Sigmoid, and Tanh introduce non-linearity to help neural networks learn complex patterns.
def relu(x):
return max(0, x)
print("ReLU(-3):", relu(-3))
# Output: ReLU(-3): 0
import math
def sigmoid(x):
return 1 / (1 + math.exp(-x))
print("Sigmoid(0):", sigmoid(0))
# Output: Sigmoid(0): 0.5
def tanh(x):
return (math.exp(x) - math.exp(-x)) / (math.exp(x) + math.exp(-x))
print("Tanh(1):", tanh(1))
# Output: Tanh(1): 0.7615941559557649
TensorFlow is a key library for deep learning. Install it via pip and verify.
# Run in terminal
pip install tensorflow
import tensorflow as tf
print("TensorFlow version:", tf.__version__)
# Output: TensorFlow version: (e.g., 2.15.0)
# Verify GPU support
print("Num GPUs:", len(tf.config.list_physical_devices('GPU')))
# Output: Num GPUs: 0 or more
Build a model to learn y = 2x using Keras in TensorFlow.
model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([1, 2, 3, 4], dtype=float)
ys = np.array([2, 4, 6, 8], dtype=float)
model.fit(xs, ys, epochs=500)
print("Prediction for 10:", model.predict([10.0]))
# Output: Prediction for 10: [[approx. 20]]
Overfitting means the model memorized training data. Underfitting means it learned too little. Balanced models generalize well.
print("Underfitting: Poor performance on training and test")
# Output: Underfitting: Poor performance on training and test
print("Overfitting: Great training performance, poor test")
# Output: Overfitting: Great training performance, poor test
print("Good model: Balanced and generalizes well")
# Output: Good model: Balanced and generalizes well
Split data into training (to learn), validation (to tune), and test (to evaluate).
data = list(range(10))
train = data[:6]
val = data[6:8]
test = data[8:]
print("Train:", train)
# Output: Train: [0, 1, 2, 3, 4, 5]
print("Val:", val, ", Test:", test)
# Output: Val: [6, 7] , Test: [8, 9]
You've learned what deep learning is, how it's structured, built a model, and seen real-world uses. You're now ready for deeper AI adventures!
print(" You finished Chapter 1!")
# Output: You finished Chapter 1!
print("Up next: Computer Vision, NLP, and AI Projects!")
# Output: Up next: Computer Vision, NLP, and AI Projects!
print("Keep experimenting with TensorFlow!")
# Output: Keep experimenting with TensorFlow!
- In deep learning, data can be numbers, images, audio, or text.
- Usually, we work with structured data (tables), unstructured data (images/text), or semi-structured (JSON/XML).
- For beginners, it's crucial to understand formats like CSV, JPG/PNG, WAV/MP3, and plain text.
# Let's read a CSV file (common format for datasets)
import pandas as pd # Importing pandas for data handling
# Load data from a CSV file
data = pd.read_csv("sample.csv") # 'sample.csv' should be a real file in your project
# Show the first 5 rows of the dataset
print(data.head()) # Output: 5 rows with columns, like name, age, score
- Images are 2D (grayscale) or 3D (RGB) arrays of numbers.
- Each pixel has an intensity between 0 and 255.
- Python libraries like Matplotlib and PIL help us view and manipulate images.
# Load and show an image
from PIL import Image # PIL is a library to handle images
import matplotlib.pyplot as plt # Matplotlib for plotting
# Open image
img = Image.open("sample.jpg") # 'sample.jpg' should exist in the project
# Show image
plt.imshow(img) # Display the image
plt.axis('off') # Hide axis labels for clarity
plt.show() # Render the image
- Neural networks work better with data between 0 and 1 or -1 and 1.
- Normalization transforms values to this range, avoiding large numbers that slow learning.
# Normalize a simple list of pixel values
import numpy as np # Used for numerical operations
pixels = np.array([0, 128, 255]) # Original pixel values (0 = black, 255 = white)
normalized = pixels / 255 # Divide all values to scale them between 0 and 1
print("Normalized pixels:", normalized) # Output: [0. 0.50196078 1. ]
- Training data teaches the model.
- Validation data helps tune it during training.
- Test data checks how it performs on new unseen data.
# Example of splitting data manually
data = list(range(100)) # Imagine this is our dataset
train = data[:70] # First 70% for training
val = data[70:85] # Next 15% for validation
test = data[85:] # Final 15% for testing
print("Train:", train)
print("Validation:", val)
print("Test:", test)
- Shuffling makes sure the model doesn't memorize the order.
- Batching splits data into smaller chunks to train faster.
# Shuffle and batch data
from sklearn.utils import shuffle # Function to shuffle data
shuffled = shuffle(data) # Shuffle the full dataset
batch_size = 10
batches = [shuffled[i:i+batch_size] for i in range(0, len(shuffled), batch_size)] # Create batches
print("First batch:", batches[0]) # Output: 10 random items from data
- Labels are the correct answers we want the model to learn.
- Example: In a cat vs dog image, label = 0 for cat, 1 for dog.
# Example labeled data for images
images = ["cat.jpg", "dog.jpg", "cat2.jpg"] # Image names
labels = [0, 1, 0] # 0 = cat, 1 = dog
# Combine them into pairs
for img, label in zip(images, labels):
print("Image:", img, ", Label:", label)
- Convert text labels like "dog" and "cat" into numbers.
- Most models can’t work with text directly.
# Encode animals into numbers
from sklearn.preprocessing import LabelEncoder # Tool to encode strings
encoder = LabelEncoder()
animals = ["cat", "dog", "cat", "dog"]
encoded = encoder.fit_transform(animals) # Transform into 0s and 1s
print("Encoded:", encoded) # Output: [0 1 0 1]
- Small datasets? Use rotation, zoom, flip, etc. to create more samples.
- Helps model generalize and not memorize.
# Use Keras to augment image data
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Create an image generator with rotation and flipping
datagen = ImageDataGenerator(rotation_range=40, horizontal_flip=True)
print("Data augmentation setup done!") # Used in training pipeline
print("Chapter 2 complete! You now understand how data feeds the AI brain!")
print("Next up: Layers, neurons, and model creation!")
- A neural network is a set of layers that learn to recognize patterns.
- Each layer transforms the data slightly to better solve the problem.
- The building blocks are neurons, which process weighted inputs.
- We'll build a simple model using TensorFlow and Keras.
# Import necessary libraries
import tensorflow as tf # Deep learning framework
from tensorflow import keras # High-level API for building models
from tensorflow.keras import layers # Layers help construct neural networks
print("Libraries loaded!") # Confirm imports
- A Sequential model allows you to stack layers in order.
- It's perfect for beginner-level models where each layer flows into the next.
# Create a basic neural network model
model = keras.Sequential([ # Sequential model structure
layers.Dense(64, activation='relu'), # First layer with 64 units and ReLU activation
layers.Dense(1) # Output layer with 1 neuron (e.g., regression output)
])
print("Model created")
- A layer is a group of neurons performing computation.
- The activation function (e.g., ReLU) decides how the signal flows forward.
- ReLU turns all negatives to zero — helps networks learn faster.
# Adding a hidden layer with 128 neurons
layer = layers.Dense(128, activation='relu')
print("Layer setup:", layer) # Output: Description of the layer
- Without activations, neural networks behave like linear equations.
- Popular ones include ReLU, Sigmoid, and Softmax.
- Use ReLU for hidden layers, Sigmoid/Softmax for output depending on the task.
# ReLU for hidden layer, Sigmoid for binary output
model = keras.Sequential([
layers.Dense(64, activation='relu'),
layers.Dense(1, activation='sigmoid') # Good for binary classification
])
print("Activation functions applied")
- Compilation prepares the model for training.
- You define loss (how wrong the prediction is), optimizer (how to fix mistakes), and metrics (what to track).
# Compile the model with loss and optimizer
model.compile(
optimizer='adam', # Efficient optimizer
loss='binary_crossentropy', # Loss for binary classification
metrics=['accuracy'] # Track accuracy during training
)
print("Model compiled!")
- Training feeds the model data and allows it to learn by adjusting weights.
- You choose number of epochs (full passes over data) and batch size (chunk of data).
# Sample training data (X = features, y = labels)
import numpy as np
X = np.array([[0], [1], [2], [3], [4], [5]]) # Input data
y = np.array([0, 0, 1, 1, 1, 1]) # Labels (binary output)
model.fit(X, y, epochs=10, batch_size=2) # Train for 10 passes
- After training, you evaluate the model using unseen test data.
- Accuracy and loss give insight into performance.
# Evaluate on new data
X_test = np.array([[6], [7]])
y_test = np.array([1, 1])
loss, acc = model.evaluate(X_test, y_test)
print("Test loss:", loss)
print("Test accuracy:", acc)
- Once trained, the model can make predictions on any input.
- You get probabilities or class labels depending on your output layer.
# Predict a new value
prediction = model.predict(np.array([[8]])) # Predict for input 8
print("Prediction:", prediction) # Output: A probability between 0 and 1
- Save your model to reuse or deploy it.
- You can load it later and make predictions without training again.
# Save the model
model.save("my_model.h5") # Save as HDF5 format
# Load the model
loaded_model = keras.models.load_model("my_model.h5")
print("Model loaded successfully!")
print("Chapter 3 complete! You're now building neural networks!")
print("Next: We dive into image-based models with CNNs")
- Traditional neural networks treat each input value independently.
- For image data, pixels have spatial relationships — CNNs capture these patterns.
- CNNs are widely used in image recognition, face detection, and medical imaging.
- Think of CNNs as layers that act like filters, scanning the image for features like edges or shapes.
# Import TensorFlow and other required libraries
import tensorflow as tf # Core deep learning library
from tensorflow.keras import layers, models # Layers and models for CNNs
import numpy as np # For handling data arrays
print("Libraries loaded!") # Confirm import
- We'll use the CIFAR-10 dataset, which contains 60,000 images of 10 classes.
- The images are 32x32 color images (RGB).
- Data is split into training and test sets.
# Load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
# Normalize pixel values between 0 and 1 for faster training
x_train, x_test = x_train / 255.0, x_test / 255.0
print("Data loaded and normalized")
- We'll stack layers including Conv2D, MaxPooling2D, Flatten, and Dense.
- Conv2D detects patterns, MaxPooling reduces size, Flatten prepares for Dense layers.
# Build CNN model step by step
model = models.Sequential() # Initialize sequential model
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) # First conv layer
model.add(layers.MaxPooling2D((2, 2))) # First pooling layer
model.add(layers.Conv2D(64, (3, 3), activation='relu')) # Second conv layer
model.add(layers.MaxPooling2D((2, 2))) # Second pooling layer
model.add(layers.Flatten()) # Flatten to 1D
model.add(layers.Dense(64, activation='relu')) # Dense layer for learning
model.add(layers.Dense(10)) # Final layer for 10 classes
print("CNN model built")
- Before training, we compile the model with optimizer, loss function, and metrics.
- For multi-class classification, we use SparseCategoricalCrossentropy.
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
print("CNN compiled")
- Fit the model to the training data with 10 epochs.
- Each epoch trains on the entire dataset.
# Train the CNN model
model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))
print("CNN training complete")
- Use the test set to check how well the model performs on unseen data.
# Evaluate model
loss, accuracy = model.evaluate(x_test, y_test)
print("Test loss:", loss)
print("Test accuracy:", accuracy)
- Use the model to predict the class of a new image.
- Outputs are logits, so apply softmax for probabilities.
# Predict on test data
predictions = model.predict(x_test)
predicted_label = np.argmax(predictions[0]) # Choose label with highest probability
print("Predicted label for first test image:", predicted_label)
- Save your trained model to a file and reload it later for inference.
- Useful for deployment or sharing.
# Save model to file
model.save("cnn_image_classifier.h5")
# Load model from file
new_model = tf.keras.models.load_model("cnn_image_classifier.h5")
print("CNN model loaded")
- You can use CNNs to classify traffic signs for self-driving cars.
- Dataset: German Traffic Sign Recognition Benchmark (GTSRB).
- Steps: load images, preprocess, build CNN model, train, and evaluate.
- Output: Classify speed limits, stop signs, and more.
# This is a placeholder. Real-world datasets like GTSRB follow similar steps as CIFAR-10.
# You can replace the CIFAR-10 dataset with your own images and labels for traffic sign classification.
print("Chapter 4 complete! You're now using CNNs to classify images!")
print("Next: Making CNNs smarter and faster with new techniques")
- Dropout randomly deactivates neurons during training.
- This forces the network to learn more general features instead of memorizing.
- Prevents overfitting, which is when your model performs well on training data but poorly on test data.
# Modify CNN model to include Dropout
from tensorflow.keras import layers, models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5)) # Drop 50% of neurons
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
- Batch Normalization normalizes activations during training.
- It speeds up training and stabilizes the learning process.
- Helps avoid issues like vanishing/exploding gradients.
# Add BatchNormalization layers
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dense(10))
- EarlyStopping stops training when validation loss stops improving.
- This avoids overtraining and saves time.
- Useful for large datasets.
# Add EarlyStopping callback
from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=3)
model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test), callbacks=[early_stop])
- Plot training and validation accuracy/loss using matplotlib.
- Helps you understand if your model is underfitting or overfitting.
# Plot accuracy and loss
import matplotlib.pyplot as plt
history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10)
plt.plot(history.history['accuracy'], label='train accuracy')
plt.plot(history.history['val_accuracy'], label='val accuracy')
plt.legend()
plt.title("Training vs Validation Accuracy")
plt.show()
- ModelCheckpoint saves the model at intervals or when performance improves.
- Ensures you keep the best model version.
# Add ModelCheckpoint callback
from tensorflow.keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint("best_model.h5", save_best_only=True, monitor='val_loss')
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, callbacks=[checkpoint])
- Data augmentation creates modified copies of images (flip, rotate, etc).
- This increases dataset size and improves model robustness.
# Data augmentation using ImageDataGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rotation_range=20, horizontal_flip=True, width_shift_range=0.2, height_shift_range=0.2)
datagen.fit(x_train)
model.fit(datagen.flow(x_train, y_train, batch_size=64), validation_data=(x_test, y_test), epochs=10)
- Pretrained models like VGG or MobileNet are trained on massive datasets.
- You can fine-tune them for your specific task.
- Saves training time and improves accuracy.
# Load MobileNetV2 and fine-tune
base_model = tf.keras.applications.MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights='imagenet')
base_model.trainable = False # Freeze base model
model = tf.keras.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(10)
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=5)
- You can load real images from your disk and use the trained model to classify them.
- Useful for deploying models in real apps.
# Predict a new image
from tensorflow.keras.preprocessing import image
img = image.load_img("cat.jpg", target_size=(32, 32))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
pred = model.predict(img_array)
print("Predicted class:", np.argmax(pred))
- Collect 1000 dog and cat images from the internet.
- Split them into train and test folders.
- Use data augmentation + CNN + dropout + checkpoint + early stopping.
- Train, test, save, and deploy your model.
# You can use the same techniques shown to build this real project
print("Chapter 5 complete! You've mastered CNN improvements and real-world techniques!")
print("Next up: Dive into Recurrent Neural Networks and sequences!")
- Transfer learning uses a model trained on one task and applies it to a different but related task.
- This saves time, requires less data, and improves performance.
- Useful when we don’t have a large dataset.
# No code for this part – it's conceptual
- Keras provides many pretrained models like VGG, ResNet, and MobileNet.
- We can load one and use its weights to avoid training from scratch.
from tensorflow.keras.applications import MobileNetV2
model = MobileNetV2(weights='imagenet') # Load pretrained model with ImageNet weights
- We usually freeze the early layers to keep learned features.
- Only train the new layers we add for our specific task.
for layer in model.layers:
layer.trainable = False # Freeze all layers
- We add new layers on top of the base model to classify new categories.
- Usually includes pooling and Dense layers.
from tensorflow.keras import layers, models
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(96, 96, 3))
base_model.trainable = False # Freeze the base model
model = models.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation='relu'),
layers.Dense(2, activation='softmax')
])
- Compile the model with optimizer, loss function, and metrics.
- Choose suitable loss and metrics depending on the task.
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
- Train the new model on your dataset.
- Use callbacks like EarlyStopping and ModelCheckpoint for better control.
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
callbacks = [
EarlyStopping(patience=3, restore_best_weights=True),
ModelCheckpoint('best_model.h5', save_best_only=True)
]
model.fit(train_data, train_labels, epochs=10, validation_data=(val_data, val_labels), callbacks=callbacks)
- After training custom layers, we can unfreeze some base layers.
- This lets us adjust more of the model to our data.
# Unfreeze the top layers of the base model
base_model.trainable = True
for layer in base_model.layers[:-20]:
layer.trainable = False # Keep lower layers frozen
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels, epochs=5, validation_data=(val_data, val_labels), callbacks=callbacks)
- You can use other pretrained models like VGG16, ResNet50, InceptionV3.
- Each has different architecture and trade-offs.
from tensorflow.keras.applications import VGG16
vgg = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
- Download 500 flower images (e.g., rose, sunflower).
- Use a pretrained model like MobileNetV2.
- Freeze layers, add custom output, train and fine-tune.
# This uses the same pipeline shown above, applied to flower dataset
print("Chapter 6 complete! You now understand Transfer Learning!")
print("Next: Let's dive into RNNs for sequences and text data!")
- Sequence data includes text, time series, or audio where order matters.
- Examples: predicting next word, stock price over time, or music notes.
- Each data point in the sequence depends on the previous one.
- RNNs are designed to handle sequential data.
- They keep a 'memory' using loops that pass information from one step to the next.
- This helps predict outcomes based on the full sequence history.
# RNN is like a loop over time steps, sharing weights across steps
- Keras provides a SimpleRNN layer to build basic RNNs.
- You provide input shape (time steps, features).
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
model = Sequential()
model.add(SimpleRNN(10, input_shape=(5, 1))) # 5 time steps, 1 feature
model.add(Dense(1)) # Output a single value
model.compile(optimizer='adam', loss='mse')
- You must reshape data into (samples, time steps, features).
- This format helps RNN understand the sequence structure.
import numpy as np
X = np.array([[[1], [2], [3], [4], [5]],
[[2], [3], [4], [5], [6]]]) # shape (2, 5, 1)
y = np.array([6, 7])
- Feed the sequential data into the model.
- It learns to predict the next number in the sequence.
model.fit(X, y, epochs=300, verbose=0)
- After training, use the model to predict new sequences.
test_input = np.array([[[3], [4], [5], [6], [7]]])
print(model.predict(test_input)) # Predicts next number in the sequence
- You can stack multiple RNN layers for deeper learning.
- Use return_sequences=True to pass outputs to the next RNN layer.
model = Sequential()
model.add(SimpleRNN(10, return_sequences=True, input_shape=(5, 1)))
model.add(SimpleRNN(5))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
- Suppose we want to predict tomorrow’s temperature based on the last 5 days.
- We reshape data as (samples, 5, 1), train RNN, and make predictions.
temps = np.array([30, 31, 32, 33, 34, 35, 36, 37])
X = np.array([[[30], [31], [32], [33], [34]],
[[31], [32], [33], [34], [35]],
[[32], [33], [34], [35], [36]]])
y = np.array([35, 36, 37])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[33], [34], [35], [36], [37]]]))) # Predicts day 8
- RNNs struggle with long sequences due to vanishing gradients.
- They forget older data over long time steps.
- In next chapter, we’ll fix this with LSTM networks.
print("Chapter 7 complete! You now understand RNN basics!")
print("Next: Let’s dive into LSTM networks!")
Output: array([[6.01]], dtype=float32)
Output: array([[38.02]], dtype=float32)
- LSTM is a special type of RNN designed to learn long-term dependencies.
- It uses a cell state and three gates (forget, input, output) to control the flow of information.
- This design helps avoid the vanishing gradient problem and remember information over longer sequences.
- Keras provides an LSTM layer that can be added just like SimpleRNN.
- You define the number of units and input shape.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(50, input_shape=(5, 1))) # 5 time steps, 1 feature
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
- Format is same as RNN: (samples, time steps, features).
- Example with synthetic sequence data:
import numpy as np
X = np.array([[[10], [11], [12], [13], [14]],
[[20], [21], [22], [23], [24]]])
y = np.array([15, 25])
- We train the model using the fit method.
- The model learns to predict the next value in the sequence.
model.fit(X, y, epochs=300, verbose=0)
- After training, you can use new input to make predictions.
- The input must match the shape (1, 5, 1).
test_input = np.array([[[30], [31], [32], [33], [34]]])
prediction = model.predict(test_input)
print(prediction) # Predicts 35
- We can use multiple LSTM layers for better learning.
- First LSTM must return sequences to feed into the next LSTM.
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(5, 1)))
model.add(LSTM(25))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
- Use past 5 stock prices to predict the next price.
- Data must be normalized if values are large.
prices = np.array([100, 102, 104, 106, 108, 110])
X = np.array([[[100], [102], [104], [106], [108]]])
y = np.array([110])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[102], [104], [106], [108], [110]]]))) # Predict next
- You can save your trained model and load it later.
- Use Keras save() and load_model() functions.
from tensorflow.keras.models import load_model
model.save("lstm_model.h5")
loaded_model = load_model("lstm_model.h5")
print(loaded_model.predict(test_input))
pip install tensorflow numpy
lstm_example.py
)python lstm_example.py
array([[111.02]], dtype=float32)
1. Weather Temperature Prediction
X = np.array([[[15], [16], [18], [20], [22]]])
y = np.array([24])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[16], [18], [20], [22], [24]]])))
2. Website Traffic Forecasting
X = np.array([[[100], [120], [150], [130], [170]]])
y = np.array([200])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[120], [150], [130], [170], [200]]])))
3. Energy Consumption Monitoring
X = np.array([[[300], [310], [320], [330], [340]]])
y = np.array([350])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[310], [320], [330], [340], [350]]])))
4. Heart Rate Monitoring
X = np.array([[[70], [72], [74], [76], [78]]])
y = np.array([80])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[72], [74], [76], [78], [80]]])))
5. Air Quality Index Prediction
X = np.array([[[40], [42], [44], [46], [48]]])
y = np.array([50])
model.fit(X, y, epochs=300, verbose=0)
print(model.predict(np.array([[[42], [44], [46], [48], [50]]])))
pip install tensorflow numpy
lstm_examples.py
)python lstm_examples.py
array([[111.02]], dtype=float32)
This example shows how to use LSTM to predict monthly temperature based on previous data.
import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense data = np.array([[30], [32], [35], [37], [38], [40], [42], [43], [45], [46], [47], [48], [50], [52], [53], [55], [57], [60], [62], [63]]) X = np.array([data[i:i+5] for i in range(len(data)-5)]) y = np.array([data[i+5] for i in range(len(data)-5)]) X = X.reshape((X.shape[0], X.shape[1], 1)) model = Sequential() model.add(LSTM(50, input_shape=(5, 1))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.fit(X, y, epochs=300, verbose=0) test_input = np.array([[[58], [59], [60], [61], [62]]]) prediction = model.predict(test_input) print(prediction) # Predicts temperature for the next month
This example demonstrates how to predict house prices using LSTM based on features such as size, age, and number of rooms.
import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense data = np.array([[1000, 10, 3, 300000], [1200, 15, 4, 350000], [1500, 5, 4, 400000], [900, 20, 2, 250000], [1100, 8, 3, 320000]]) X = data[:, :-1] y = data[:, -1] X = X.reshape((X.shape[0], 1, X.shape[1])) model = Sequential() model.add(LSTM(50, input_shape=(1, 3))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.fit(X, y, epochs=300, verbose=0) test_input = np.array([[[1000, 10, 3]]]) prediction = model.predict(test_input) print(prediction) # Predict house price
This example shows how to predict the electricity usage for the next hour based on previous usage data.
import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense data = np.array([5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]) X = np.array([data[i:i+5] for i in range(len(data)-5)]) y = np.array([data[i+5] for i in range(len(data)-5)]) X = X.reshape((X.shape[0], X.shape[1], 1)) model = Sequential() model.add(LSTM(50, input_shape=(5, 1))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.fit(X, y, epochs=300, verbose=0) test_input = np.array([[[22], [23], [24], [25], [26]]]) prediction = model.predict(test_input) print(prediction) # Predict next hour's usage
This example demonstrates how to predict the traffic flow for the next minute based on past traffic data.
import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense data = np.array([20, 30, 35, 40, 45, 50, 55, 60, 65, 70]) X = np.array([data[i:i+5] for i in range(len(data)-5)]) y = np.array([data[i+5] for i in range(len(data)-5)]) X = X.reshape((X.shape[0], X.shape[1], 1)) model = Sequential() model.add(LSTM(50, input_shape=(5, 1))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.fit(X, y, epochs=300, verbose=0) test_input = np.array([[[60], [65], [70], [75], [80]]]) prediction = model.predict(test_input) print(prediction) # Predict next minute's traffic flow
This example shows how to use LSTM to predict the stock market price for the next day based on past stock prices.
import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense data = np.array([100, 102, 104, 106, 108, 110, 112, 114, 116, 118]) X = np.array([data[i:i+5] for i in range(len(data)-5)]) y = np.array([data[i+5] for i in range(len(data)-5)]) X = X.reshape((X.shape[0], X.shape[1], 1)) model = Sequential() model.add(LSTM(50, input_shape=(5, 1))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.fit(X, y, epochs=300, verbose=0) test_input = np.array([[[115], [116], [117], [118], [119]]]) prediction = model.predict(test_input) print(prediction) # Predict next day's stock price
- CNNs are designed for processing grid-like data, such as images.
- They use convolutional layers to detect features like edges, shapes, and textures.
- Pooling layers reduce spatial size and improve computational efficiency.
- Import necessary Keras modules and create a basic CNN for image classification.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1))) # 28x28 grayscale image
model.add(MaxPooling2D(pool_size=(2, 2))) # Reduce spatial size
model.add(Flatten()) # Flatten into 1D vector
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax')) # 10 output classes
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
- Use MNIST dataset from Keras, which includes handwritten digit images.
- Normalize images and reshape them properly for CNN input.
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1) / 255.0 # Normalize pixel values
x_test = x_test.reshape(-1, 28, 28, 1) / 255.0
- Fit the model using training data and validate on test set.
model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
- Check the final performance on test data.
test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test Accuracy:", test_acc)
- Make predictions and visualize results for the first few test images.
import matplotlib.pyplot as plt
predictions = model.predict(x_test)
for i in range(5):
plt.imshow(x_test[i].reshape(28, 28), cmap="gray")
plt.title(f"Predicted: {predictions[i].argmax()} | True: {y_test[i]}")
plt.show()
- Dropout helps prevent overfitting by randomly turning off neurons during training.
from tensorflow.keras.layers import Dropout
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
- Save trained CNN for future use and load it when needed.
model.save("cnn_model.h5")
from tensorflow.keras.models import load_model
loaded_model = load_model("cnn_model.h5")
pip install tensorflow matplotlib
cnn_example.py
python cnn_example.py
Test Accuracy: 0.9875
This example demonstrates how to use CNN to classify images into categories like 'cat' and 'dog'.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Define the CNN model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(train_images, train_labels, epochs=10, batch_size=32)
This example shows how to implement CNN for facial recognition tasks.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Define the CNN model
model = Sequential()
model.add(Conv2D(64, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(facial_images, labels, epochs=20, batch_size=32)
This example uses CNN to classify handwritten digits from the MNIST dataset.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
# Define the CNN model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=10, batch_size=64)
This example demonstrates using CNN for object detection on images.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Define the CNN model
model = Sequential()
model.add(Conv2D(64, (3, 3), activation='relu', input_shape=(128, 128, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(2, activation='softmax'))
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(object_images, object_labels, epochs=20, batch_size=32)
This example shows how to use CNN for segmenting images (e.g., identifying boundaries in an image).
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Define the CNN model
model = Sequential()
model.add(Conv2D(64, (3, 3), activation='relu', input_shape=(256, 256, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(segmentation_images, segmentation_labels, epochs=30, batch_size=32)
This example shows how to use CNN to colorize black and white images.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Define the CNN model
model = Sequential()
model.add(Conv2D(64, (3, 3), activation='relu', input_shape=(256, 256, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(3, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
# Train the model
model.fit(black_and_white_images, colored_images, epochs=20, batch_size=32)
This tutorial explains how to set up a Flask web application to run a Convolutional Neural Network (CNN) model and display the result in the browser.
First, install Flask using pip by running this command in your terminal or command prompt:
pip install flask
Create a Python file (e.g., app.py
) and write the following code:
from flask import Flask, render_template, jsonify import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense import numpy as np app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/run_cnn') def run_cnn(): model = Sequential() model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) data = np.random.random((1, 64, 64, 3)) # Fake image data with 64x64 RGB prediction = model.predict(data) return jsonify({"prediction": prediction.tolist()}) if __name__ == '__main__': app.run(debug=True)
Create an HTML file (e.g., templates/index.html
) with the following code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CNN Example</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 20px; } .container { max-width: 900px; margin: 0 auto; padding: 20px; } .card { background-color: #f4f4f4; margin-bottom: 20px; padding: 15px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); } .card-header { font-weight: bold; margin-bottom: 10px; } pre { background-color: #eee; padding: 15px; border-radius: 5px; overflow-x: auto; white-space: pre-wrap; } .output { margin-top: 20px; padding: 15px; background-color: #f9f9f9; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); } </style> </head> <body> <div class="container"> <h1>Real-World CNN Examples</h1> <!-- Example 1: CNN for Image Classification --> <div class="card"> <div class="card-header">1. CNN for Image Classification</div> <pre> import tensorflow as tf<br> from tensorflow.keras.models import Sequential<br> from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense<br> <br> model = Sequential()<br> model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))<br> model.add(MaxPooling2D(pool_size=(2, 2)))<br> model.add(Conv2D(64, (3, 3), activation='relu'))<br> model.add(MaxPooling2D(pool_size=(2, 2)))<br> model.add(Flatten())<br> model.add(Dense(128, activation='relu'))<br> model.add(Dense(1, activation='sigmoid'))<br> <br> model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])<br> </pre> </div> <!-- Button to run the CNN model --> <button onclick="runCNN()">Run CNN Example</button> <div class="output" id="output"></div> </div> <script> function runCNN() { fetch('/run_cnn') .then(response => response.json()) .then(data => { // Display the CNN model's prediction result document.getElementById("output").innerHTML = `CNN Prediction: ${data.prediction}`; }) .catch(error => { document.getElementById("output").innerHTML = "Error running CNN model"; }); } </script> </body> </html>
After you've set up the Python server and the HTML interface, you can run the Flask application:
python app.py
Once the server is running, you can visit http://127.0.0.1:5000/
in your web browser to see the CNN example in action. Click the "Run CNN Example" button to run the model and view its prediction.
- When you click the "Run CNN Example" button, the frontend (HTML) sends a request to the Flask backend.
- The Flask backend then runs the CNN model using a fake image (a random 64x64x3 tensor), and returns a prediction.
- The frontend receives the prediction and displays it in the "output" div.
This chapter focuses on advanced concepts in deep learning, exploring cutting-edge techniques and architectures used in modern AI systems. We will walk through the different approaches in depth, providing fully commented examples and real-world scenarios where these concepts apply.
In this subchapter, we introduce the core concepts of advanced deep learning, such as the importance of deep neural networks, attention mechanisms, and the challenges that come with training deep architectures.
# Example: Basic neural network creation in Keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Create a simple neural network
model = Sequential([
Dense(64, activation='relu', input_shape=(784,)),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print("Model created with basic layers")
CNNs are a type of deep learning architecture commonly used for image classification tasks. In this subchapter, we will explore how to implement a CNN using Keras.
# Example: Simple CNN for image classification
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Create a CNN model
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the CNN model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print("CNN model created for image classification")
RNNs are great for sequence data, such as time-series data or natural language processing tasks. This subchapter will cover RNN implementation and how it can be applied to various problems.
# Example: Simple RNN for sequence prediction
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
# Create an RNN model
model = Sequential([
SimpleRNN(50, input_shape=(10, 1)),
Dense(1)
])
# Compile the RNN model
model.compile(optimizer='adam', loss='mean_squared_error')
print("RNN model created for sequence prediction")
LSTMs are a special kind of RNN that can capture long-term dependencies and are particularly useful for tasks like language modeling and sequence generation.
# Example: LSTM for sequence data
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Create an LSTM model
model = Sequential([
LSTM(100, input_shape=(10, 1)),
Dense(1)
])
# Compile the LSTM model
model.compile(optimizer='adam', loss='mean_squared_error')
print("LSTM model created for sequence prediction")
GANs are used for generating realistic images and videos. They consist of two models: a generator and a discriminator, working together in a zero-sum game.
# Example: Simple GAN setup (Generator and Discriminator)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LeakyReLU
# Define Generator
generator = Sequential([
Dense(256, input_dim=100),
LeakyReLU(0.2),
Dense(784, activation='tanh')
])
# Define Discriminator
discriminator = Sequential([
Dense(512, input_dim=784),
LeakyReLU(0.2),
Dense(1, activation='sigmoid')
])
print("GAN architecture created")
Attention mechanisms are powerful tools for models that process sequential data. They allow models to focus on specific parts of the input sequence that are more relevant for a given output.
# Example: Attention mechanism in a sequence-to-sequence task
from tensorflow.keras.layers import Attention
# Define query, value, and key vectors for attention mechanism
query = Input(shape=(None, 128))
value = Input(shape=(None, 128))
key = Input(shape=(None, 128))
# Apply attention
attention_layer = Attention()([query, value, key])
print("Attention mechanism applied")
The Transformer model is the backbone of many state-of-the-art models for tasks like machine translation. In this section, we will implement a simplified transformer.
# Example: Simplified Transformer Model
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, MultiHeadAttention, LayerNormalization
# Input layer
inputs = Input(shape=(None, 128))
# Transformer layer
attention_output = MultiHeadAttention(num_heads=8, key_dim=128)(inputs, inputs)
output = LayerNormalization()(attention_output)
print("Transformer layer created")
Autoencoders are unsupervised models that learn to compress data and reconstruct it back to its original form. They have applications in anomaly detection, noise reduction, and image denoising.
# Example: Simple Autoencoder
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
# Encoder
input_layer = Input(shape=(784,))
encoded = Dense(128, activation='relu')(input_layer)
# Decoder
decoded = Dense(784, activation='sigmoid')(encoded)
# Autoencoder model
autoencoder = Model(input_layer, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
print("Autoencoder model created")
Reinforcement Learning is a type of machine learning where an agent learns how to behave in an environment to maximize a reward. We will explore the basic components and how to implement a simple RL agent.
# Example: Simple Q-learning agent for reinforcement learning
import numpy as np
# Q-table initialization
q_table = np.zeros([5, 5]) # 5x5 grid
# Define learning rate and discount factor
alpha = 0.1 # learning rate
gamma = 0.9 # discount factor
print("Q-learning agent initialized")
Transfer learning involves using a pre-trained model on one task and fine-tuning it for a different but related task. This technique can dramatically improve performance on tasks with limited data.
# Example: Transfer learning using pre-trained ResNet
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Flatten, Dense
# Load the pre-trained ResNet50 model without the top layer
base_model = ResNet50(weights='imagenet', include_top=False)
# Add custom layers on top
flatten = Flatten()(base_model.output)
output = Dense(10, activation='softmax')(flatten)
print("Transfer learning model with ResNet50 created")
In this chapter, we covered various advanced deep learning techniques, from CNNs and RNNs to Transformer architectures and reinforcement learning. These models have broad applications across many domains and provide the foundation for cutting-edge AI systems.
This chapter provides 10 real-world examples showcasing advanced deep learning concepts, from image classification and generative models to reinforcement learning and transfer learning. All examples are explained and fully commented with instructions on how to run them.
A real-world image classification model that uses CNNs for recognizing handwritten digits from the MNIST dataset.
# Import necessary libraries
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# Load and preprocess MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1)) / 255.0
test_images = test_images.reshape((10000, 28, 28, 1)) / 255.0
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# Build CNN model
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile and train the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
# Evaluate model on test data
test_loss, test_acc = model.evaluate(test_images, test_labels)
print("Test accuracy:", test_acc)
To run this code, ensure you have TensorFlow installed (`pip install tensorflow`) and run the script.
A real-world example using RNNs for predicting future values in a time series dataset.
# Import necessary libraries
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
import numpy as np
# Generate a synthetic time series dataset
data = np.sin(np.arange(1000))
X = np.array([data[i:i+10] for i in range(990)])
y = np.array([data[i+10] for i in range(990)])
# Reshape data for RNN input
X = X.reshape((X.shape[0], X.shape[1], 1))
# Build RNN model
model = Sequential([
SimpleRNN(50, input_shape=(10, 1)),
Dense(1)
])
# Compile and train the model
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X, y, epochs=10, batch_size=32)
# Predict future values
predictions = model.predict(X)
print("Predictions:", predictions[:5])
Run the script with TensorFlow installed to train the RNN and make predictions on the synthetic dataset.
A simple LSTM network for text generation based on a given corpus.
# Import necessary libraries
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
# Sample text data
text = "The quick brown fox jumps over the lazy dog." * 10
# Tokenize the text
tokenizer = Tokenizer(char_level=True)
tokenizer.fit_on_texts([text])
# Prepare sequences for training
sequences = tokenizer.texts_to_sequences([text])[0]
X, y = [], []
for i in range(len(sequences) - 10):
X.append(sequences[i:i+10])
y.append(sequences[i+10])
X = np.array(X)
y = np.array(y)
# Reshape X for LSTM input
X = X.reshape((X.shape[0], X.shape[1], 1))
# Build LSTM model
model = Sequential([
LSTM(50, input_shape=(10, 1)),
Dense(len(tokenizer.word_index)+1, activation='softmax')
])
# Compile and train the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
model.fit(X, y, epochs=10, batch_size=64)
# Generate text
predicted = model.predict(X[:1])
predicted_text = tokenizer.sequences_to_texts([predicted.argmax(axis=1)])
print("Predicted Text:", predicted_text)
Install TensorFlow and run the script to generate text based on the trained LSTM model.
A simple GAN model to generate new images similar to the MNIST dataset.
# Import necessary libraries
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LeakyReLU
from tensorflow.keras.optimizers import Adam
import numpy as np
# Build the generator
generator = Sequential([
Dense(256, input_dim=100),
LeakyReLU(0.2),
Dense(784, activation='tanh')
])
# Build the discriminator
discriminator = Sequential([
Dense(512, input_dim=784),
LeakyReLU(0.2),
Dense(1, activation='sigmoid')
])
# Compile the discriminator
discriminator.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
# Combine the models
discriminator.trainable = False
gan = Sequential([generator, discriminator])
gan.compile(optimizer=Adam(), loss='binary_crossentropy')
# Train the GAN
for epoch in range(1000):
noise = np.random.normal(0, 1, (128, 100))
fake_images = generator.predict(noise)
real_images = np.random.normal(0, 1, (128, 784)) # Fake data for demonstration
# Train the discriminator
discriminator.trainable = True
discriminator.train_on_batch(real_images, np.ones((128, 1)))
discriminator.train_on_batch(fake_images, np.zeros((128, 1)))
# Train the GAN
discriminator.trainable = False
gan.train_on_batch(noise, np.ones((128, 1)))
print("GAN model training complete")
Run the script to train the GAN and generate fake images similar to the MNIST dataset.
Using a pre-trained model like ResNet50 for transfer learning on a new dataset.
# Import necessary libraries
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Load pre-trained ResNet50 model
base_model = ResNet50(weights='imagenet', include_top=False)
# Add custom layers on top
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# Final model
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Prepare image data generator
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory('data/train', target_size=(224, 224), batch_size=32, class_mode='categorical')
# Train the model
model.fit(train_generator, epochs=10)
print("Transfer learning with ResNet50 complete")
Ensure that the `data/train` folder contains your image dataset and then run the script to fine-tune the model.
A basic Q-learning implementation to solve the FrozenLake environment in OpenAI's Gym.
# Import necessary libraries
import numpy as np
import gym
# Initialize the environment
env = gym.make('FrozenLake-v0')
# Initialize Q-table
q_table = np.zeros([env.observation_space.n, env.action_space.n])
# Hyperparameters
alpha = 0.1 # learning rate
gamma = 0.6 # discount factor
epsilon = 0.1 # exploration factor
# Training loop
for i in range(1000):
state = env.reset()[0]
done = False
while not done:
if np.random.uniform(0, 1) < epsilon:
action = env.action_space.sample() # explore
else:
action = np.argmax(q_table[state]) # exploit
next_state, reward, done, _, _ = env.step(action)
old_q_value = q_table[state, action]
next_max_q_value = np.max(q_table[next_state])
# Update Q-value
q_table[state, action] = old_q_value + alpha * (reward + gamma * next_max_q_value - old_q_value)
state = next_state
print("Training complete, Q-table updated.")
Install Gym (`pip install gym`) and run the script to train the Q-learning agent.
Detect objects in an image using YOLO and a pre-trained model.
# Import necessary libraries
import cv2
# Load YOLO model
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i-1] for i in net.getUnconnectedOutLayers()]
# Load input image
img = cv2.imread('image.jpg')
height, width, channels = img.shape
# Prepare the image for YOLO
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# Process output to detect objects
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
cv2.rectangle(img, (center_x - w // 2, center_y - h // 2), (center_x + w // 2, center_y + h // 2), (0, 255, 0), 2)
# Show result
cv2.imshow("Object Detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Ensure you have the YOLOv3 model files and OpenCV installed (`pip install opencv-python`), then run the script to detect objects in an image.
Use BERT for sentiment analysis on a text dataset.
# Import necessary libraries
from transformers import BertTokenizer, BertForSequenceClassification
from torch.utils.data import DataLoader, TensorDataset
import torch
# Load pre-trained BERT model and tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
# Sample text for analysis
texts = ["I love deep learning!", "I hate waiting for long periods."]
inputs = tokenizer(texts, return_tensors='pt', padding=True, truncation=True)
# Predict sentiment
outputs = model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=-1)
print("Predictions:", predictions)
Install Hugging Face's transformers library (`pip install transformers`) and run the script for sentiment analysis.
Apply neural style transfer to combine the style of one image with the content of another.
# Import necessary libraries
import tensorflow as tf
import matplotlib.pyplot as plt
# Load content and style images
content_image = tf.image.decode_image(tf.io.read_file("content.jpg"))
style_image = tf.image.decode_image(tf.io.read_file("style.jpg"))
# Preprocess images
content_image = tf.image.resize(content_image, (256, 256)) / 255.0
style_image = tf.image.resize(style_image, (256, 256)) / 255.0
# Apply neural style transfer (simplified version)
stylized_image = tf.keras.preprocessing.image.img_to_array(content_image)
plt.imshow(stylized_image)
plt.show()
Run this script with TensorFlow installed to apply neural style transfer to your images.
Use an autoencoder model for anomaly detection in a dataset.
# Import necessary libraries
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np
# Generate synthetic data
data = np.random.normal(0, 1, (1000, 20))
normal_data = data[:800]
anomalous_data = np.random.normal(5, 1, (200, 20))
data = np.concatenate([normal_data, anomalous_data])
# Build autoencoder model
autoencoder = Sequential([
Dense(32, activation='relu', input_shape=(20,)),
Dense(16, activation='relu'),
Dense(8, activation='relu'),
Dense(16, activation='relu'),
Dense(32, activation='relu'),
Dense(20, activation='sigmoid')
])
# Compile and train model
autoencoder.compile(optimizer='adam', loss='mse')
autoencoder.fit(data, data, epochs=50, batch_size=64)
# Detect anomalies
reconstruction = autoencoder.predict(data)
mse = np.mean(np.square(data - reconstruction), axis=1)
threshold = np.percentile(mse, 95)
anomalies = mse > threshold
print("Detected anomalies:", anomalies)
Run the script to detect anomalies in the synthetic dataset using the trained autoencoder model.
This chapter covers advanced deep learning techniques, including attention mechanisms, transformers, and generative models. Each topic is explained with an example code to help you understand the concepts better.
The attention mechanism is a technique used in neural networks to improve the performance of models, particularly in sequence-to-sequence tasks such as machine translation, image captioning, and speech recognition. It allows models to focus on relevant parts of the input sequence when making predictions.
# Example: Attention Mechanism in Sequence-to-Sequence Task
import tensorflow as tf
from tensorflow.keras.layers import Attention, Input, LSTM, Dense
from tensorflow.keras.models import Model
# Define inputs
encoder_input = Input(shape=(None, 128)) # Encoder input shape
decoder_input = Input(shape=(None, 128)) # Decoder input shape
# Define LSTM layers
encoder_lstm = LSTM(128, return_state=True)(encoder_input)
decoder_lstm = LSTM(128, return_state=True)(decoder_input)
# Attention mechanism
attention = Attention()([decoder_lstm[0], encoder_lstm[0]])
# Final output layer
output = Dense(1, activation='softmax')(attention)
# Define model
model = Model(inputs=[encoder_input, decoder_input], outputs=output)
# Compile model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model (example with dummy data)
# model.fit([train_encoder_input, train_decoder_input], train_labels, epochs=5)
This code demonstrates the attention mechanism in a sequence-to-sequence task. The model learns to focus on relevant parts of the input sequence when making predictions.
Transformers are a type of deep learning architecture that use attention mechanisms to process sequences. Unlike RNNs or LSTMs, transformers process sequences in parallel, making them more efficient and effective for tasks such as machine translation, text summarization, and language modeling.
# Example: Transformer Architecture for Sequence Processing
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, LayerNormalization, Dropout
from tensorflow.keras.models import Model
# Define input
input_seq = Input(shape=(None, 128)) # Input sequence shape
# Transformer block
attention_output = tf.keras.layers.MultiHeadAttention(num_heads=8, key_dim=128)(input_seq, input_seq)
attention_output = LayerNormalization()(attention_output)
attention_output = Dropout(0.1)(attention_output)
# Output layer
output = Dense(1, activation='softmax')(attention_output)
# Define model
model = Model(inputs=input_seq, outputs=output)
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model (example with dummy data)
# model.fit(train_input_data, train_labels, epochs=5)
This transformer model uses multi-head attention to process input sequences. Transformers have become the foundation of many natural language processing tasks.
GANs are a class of machine learning frameworks consisting of two models: a generator and a discriminator. The generator creates fake data, and the discriminator tries to distinguish between real and fake data. Both models are trained simultaneously, improving their performance over time.
# Example: Simple GAN for Image Generation
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LeakyReLU
from tensorflow.keras.optimizers import Adam
# Build the generator
generator = Sequential([
Dense(256, input_dim=100),
LeakyReLU(0.2),
Dense(784, activation='tanh')
])
# Build the discriminator
discriminator = Sequential([
Dense(512, input_dim=784),
LeakyReLU(0.2),
Dense(1, activation='sigmoid')
])
# Compile the discriminator
discriminator.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
# Build the GAN
discriminator.trainable = False
gan = Sequential([generator, discriminator])
gan.compile(optimizer=Adam(), loss='binary_crossentropy')
# Train the GAN
for epoch in range(1000):
noise = np.random.normal(0, 1, (128, 100))
fake_images = generator.predict(noise)
real_images = np.random.normal(0, 1, (128, 784)) # Fake data for demonstration
# Train the discriminator
discriminator.trainable = True
discriminator.train_on_batch(real_images, np.ones((128, 1)))
discriminator.train_on_batch(fake_images, np.zeros((128, 1)))
# Train the GAN
discriminator.trainable = False
gan.train_on_batch(noise, np.ones((128, 1)))
print("GAN model training complete")
This GAN model demonstrates the process of training a generator and a discriminator to create realistic images.
Reinforcement learning (RL) is an area of machine learning where an agent learns to make decisions by interacting with an environment. The agent receives feedback in the form of rewards or penalties based on its actions.
# Example: Simple Q-Learning Algorithm for Reinforcement Learning
import numpy as np
# Initialize Q-table
Q = np.zeros((5, 5)) # 5 states, 5 actions
# Define reward function
reward = np.array([[-1, -1, -1, 0, 1],
[-1, -1, -1, 0, 1],
[-1, -1, -1, 0, 1],
[-1, -1, -1, 0, 1],
[-1, -1, -1, -1, 0]])
# Q-learning parameters
gamma = 0.8 # Discount factor
alpha = 0.1 # Learning rate
epsilon = 0.2 # Exploration rate
# Training loop
for episode in range(1000):
state = 0 # Start at initial state
while state != 4:
if np.random.uniform(0, 1) < epsilon:
action = np.random.randint(0, 5) # Exploration
else:
action = np.argmax(Q[state]) # Exploitation
# Take action and get reward
next_state = action # Transition to next state based on action
reward_value = reward[state, action] # Get reward
# Update Q-table
Q[state, action] += alpha * (reward_value + gamma * np.max(Q[next_state]) - Q[state, action])
state = next_state # Move to next state
print("Q-table after training:", Q)
This Q-learning algorithm demonstrates the basic principles of reinforcement learning, where the agent learns optimal actions to maximize cumulative rewards.
Advanced optimizers such as Adam, RMSprop, and Nadam are used to improve training efficiency and model convergence. These optimizers adapt the learning rate based on gradient information, helping to reduce training time and improve model performance.
# Example: Optimizer Comparison
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Create a simple model
model = Sequential([
Dense(64, activation='relu', input_shape=(32,)),
Dense(1)
])
# Compile with different optimizers
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
# Train model (example with dummy data)
# model.fit(train_data, train_labels, epochs=5)
This code demonstrates the use of different optimizers to train a neural network. You can experiment with different optimizers like Adam or RMSprop to see which one works best for your task.
Chapter 11 concludes with a look at these advanced techniques in deep learning. Understanding these concepts and their practical applications will provide a solid foundation for working with state-of-the-art deep learning models.
This chapter covers advanced machine learning applications, from natural language processing to computer vision and recommendation systems. Each subchapter will walk you through a practical example, explaining every step and showing how to run the example.
Sentiment analysis is the process of determining the sentiment expressed in text, whether positive, negative, or neutral. This section shows how to build a simple sentiment analysis model using a pre-trained model like BERT (Bidirectional Encoder Representations from Transformers).
# Example: Sentiment Analysis using Huggingface Transformers
import torch
from transformers import pipeline
# Load pre-trained sentiment-analysis model
model = pipeline('sentiment-analysis')
# Example sentence
text = "I love learning machine learning!"
# Get prediction
result = model(text)
print(result)
To run this example:
pip install transformers
Convolutional Neural Networks (CNNs) are widely used for image classification tasks. In this section, we'll use CNNs to classify images from a sample dataset like CIFAR-10.
# Example: Image Classification with CNN
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.optimizers import Adam
# Load dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize pixel values to [0, 1]
x_train, x_test = x_train / 255.0, x_test / 255.0
# Build CNN model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
To run this example:
pip install tensorflow
Time series forecasting is widely used in industries like finance and retail. This section demonstrates how to use machine learning for predictive analytics, specifically forecasting stock prices with historical data.
# Example: Time Series Forecasting using ARIMA
import pandas as pd
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
# Generate some dummy time series data (e.g., stock prices)
data = pd.Series(np.random.randn(1000), index=pd.date_range('20200101', periods=1000))
# Fit an ARIMA model
model = ARIMA(data, order=(5, 1, 0)) # (p,d,q)
model_fit = model.fit()
# Make predictions
forecast = model_fit.forecast(steps=10)
print(forecast)
To run this example:
pip install pandas statsmodels
Recommender systems are used in applications like Netflix and Amazon to suggest products or content to users. In this section, we'll build a simple collaborative filtering model for movie recommendations.
# Example: Movie Recommender System
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
# Load movie ratings data (dummy example)
ratings = pd.DataFrame({
'user': [1, 2, 3, 4, 5],
'movie': [101, 102, 103, 104, 105],
'rating': [5, 4, 4, 2, 5]
})
# Create a pivot table for user-movie ratings
pivot_table = ratings.pivot(index='user', columns='movie', values='rating').fillna(0)
# Calculate similarity between users
user_similarity = cosine_similarity(pivot_table)
# Make recommendations (find most similar users)
similar_users = user_similarity[0] # Get similarities of user 1
recommended_movies = ratings['movie'][similar_users.argmax()] # Find the movie most similar to user 1
print(f"Recommended movie for user 1: {recommended_movies}")
To run this example:
pip install pandas scikit-learn
Object detection involves identifying and locating objects in images. This section demonstrates how to use a pre-trained YOLO (You Only Look Once) model for real-time object detection.
# Example: Object Detection using YOLO
import cv2
# Load YOLO model (pre-trained weights)
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i-1] for i in net.getUnconnectedOutLayers()]
# Load image
image = cv2.imread("image.jpg")
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
# Get predictions
outs = net.forward(output_layers)
# Process output and draw bounding boxes
# (bounding box processing code here...)
cv2.imshow("Detected Objects", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
To run this example:
pip install opencv-python
Building chatbots using machine learning is a common application in AI. This section demonstrates how to implement a simple chatbot using sequence-to-sequence models, commonly used in NLP tasks.
# Example: Simple Chatbot using Sequence-to-Sequence Model
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Build simple sequence-to-sequence model
model = Sequential([
LSTM(128, input_shape=(None, 1), return_sequences=True),
Dense(1, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Example input-output sequences
input_seq = [[1, 2, 3]] # Sequence of words
output_seq = [[3, 4, 5]] # Corresponding output
# Train the model (simple dummy data)
model.fit(input_seq, output_seq, epochs=10)
To run this example:
pip install tensorflow
In this example, we'll use a CNN model to recognize handwritten digits from the MNIST dataset.
# Example: Handwriting Recognition using CNN
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.optimizers import Adam
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Reshape data and normalize
x_train = x_train.reshape(-1, 28, 28, 1) / 255.0
x_test = x_test.reshape(-1, 28, 28, 1) / 255.0
# Build CNN model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
To run this example:
pip install tensorflow
In this example, we will apply neural style transfer, where we blend the style of one image with the content of another.
# Example: Style Transfer using Pre-trained CNN
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
# Load content and style images
content_image = image.load_img("content.jpg", target_size=(224, 224))
style_image = image.load_img("style.jpg", target_size=(224, 224))
# Preprocess images
content_array = np.expand_dims(image.img_to_array(content_image), axis=0)
style_array = np.expand_dims(image.img_to_array(style_image), axis=0)
# Preprocess images for model
content_array = tf.keras.applications.vgg19.preprocess_input(content_array)
style_array = tf.keras.applications.vgg19.preprocess_input(style_array)
# Load pre-trained VGG19 model
vgg_model = tf.keras.applications.VGG19(weights='imagenet', include_top=False)
# Get intermediate layers for content and style features
content_layer = vgg_model.get_layer('block5_conv2').output
style_layer = vgg_model.get_layer('block1_conv1').output
# Define custom model for feature extraction
model = tf.keras.Model(inputs=vgg_model.input, outputs=[content_layer, style_layer])
# Run the style transfer optimization (code for optimization and blending)
# Apply style transfer and show final result
# (Add style transfer specific optimization code here...)
To run this example:
pip install tensorflow
In this example, we'll use an RNN to generate text based on a given corpus. This is a simple text generation model using the LSTM (Long Short-Term Memory) architecture.
# Example: Text Generation using RNN (LSTM)
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Prepare sample text data
corpus = "The quick brown fox jumps over the lazy dog. The dog barks." # Example corpus
# Create a character-to-index mapping
chars = sorted(list(set(corpus)))
char_to_index = {ch: i for i, ch in enumerate(chars)}
index_to_char = {i: ch for i, ch in enumerate(chars)}
# Convert text to integers
input_text = [char_to_index[ch] for ch in corpus]
input_text = np.array(input_text)
# Reshape and create sequences for training
X = input_text[:-1] # Sequence inputs
y = input_text[1:] # Sequence outputs
X = np.reshape(X, (X.shape[0], 1, 1)) # Reshape for LSTM
y = tf.keras.utils.to_categorical(y, num_classes=len(chars))
# Build RNN model
model = Sequential([
LSTM(128, input_shape=(1, 1), activation='relu', return_sequences=True),
LSTM(128, activation='relu'),
Dense(len(chars), activation='softmax')
])
# Compile and train the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=100)
# Generate text
seed = "The quick" # Start the text generation
generated_text = seed
for i in range(100): # Generate 100 characters
input_seq = [char_to_index[ch] for ch in generated_text[-1]]
input_seq = np.reshape(input_seq, (1, 1, 1))
prediction = model.predict(input_seq)
next_char = index_to_char[np.argmax(prediction)]
generated_text += next_char
print(generated_text)
To run this example:
pip install tensorflow
In this example, we will use CNN to classify different art styles based on images.
# Example: Art Style Classification using CNN
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.optimizers import Adam
# Load and preprocess images (assuming images are labeled and stored in directories)
# Example code to load data from directories
# from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Build CNN model for image classification
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(64, activation='relu'),
Dense(5, activation='softmax') # Assuming 5 art styles
])
# Compile the model
model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model (use your own art style dataset)
# model.fit(train_data, epochs=10, validation_data=test_data)
# Predict the style of a new image
# result = model.predict(new_image)
To run this example:
pip install tensorflow
Chapter 12 concludes with various advanced machine learning applications. Each example demonstrates how to implement state-of-the-art models and deploy them in real-world scenarios.
An AI agent is a system that perceives its environment through sensors, acts upon that environment through actuators, and learns from its actions over time to make decisions. These agents are typically designed to solve problems, optimize tasks, and interact with users or other systems.
# Example: Simple AI Agent
import random
class SimpleAgent:
def __init__(self, environment):
self.environment = environment
def act(self):
return random.choice(self.environment) # Make a random choice from the environment
# Define environment (possible actions)
environment = ['move_left', 'move_right', 'move_up', 'move_down']
# Create and use the AI agent
agent = SimpleAgent(environment)
action = agent.act()
print("Agent decided to:", action)
To run this example:
simple_agent.py
).python simple_agent.py
.Decision-making is a critical aspect of AI agents. It involves selecting the best possible action based on available information. AI agents use algorithms like decision trees, value iteration, or Q-learning to make decisions.
# Example: Decision Tree for AI Agent Decision Making
class DecisionAgent:
def __init__(self):
self.state = "start"
def make_decision(self):
if self.state == "start":
return "move_forward"
elif self.state == "move_forward":
return "turn_left"
else:
return "stop"
# Create agent and make decisions
agent = DecisionAgent()
decision = agent.make_decision()
print("Agent decision:", decision)
To run this example:
decision_agent.py
).python decision_agent.py
.Reinforcement learning (RL) is a type of machine learning where an agent learns to make decisions by interacting with an environment and receiving rewards or penalties based on its actions.
# Example: Reinforcement Learning (Q-learning) Agent
import numpy as np
class RLAgent:
def __init__(self, actions):
self.actions = actions
self.q_table = np.zeros(len(actions)) # Initialize Q-table
self.learning_rate = 0.1
self.discount_factor = 0.9
def act(self):
return np.argmax(self.q_table) # Choose the action with the highest Q-value
def update_q_table(self, action, reward):
self.q_table[action] = self.q_table[action] + self.learning_rate * (reward + self.discount_factor * np.max(self.q_table) - self.q_table[action])
# Define actions and rewards
actions = [0, 1, 2] # Example actions
# Create RL agent
agent = RLAgent(actions)
# Simulate interaction with environment
for _ in range(10):
action = agent.act()
reward = random.choice([1, -1]) # Simulate a reward or penalty
agent.update_q_table(action, reward)
print("Action taken:", action, "Updated Q-table:", agent.q_table)
To run this example:
rl_agent.py
).python rl_agent.py
.A multi-agent system (MAS) consists of multiple AI agents interacting with each other. These systems are often used for complex tasks that require cooperation, coordination, or competition.
# Example: Simple Multi-Agent System
class MultiAgentSystem:
def __init__(self, num_agents):
self.agents = [SimpleAgent(['move_left', 'move_right', 'move_up', 'move_down']) for _ in range(num_agents)]
def execute_actions(self):
actions = [agent.act() for agent in self.agents]
return actions
# Create and execute actions of multiple agents
mas = MultiAgentSystem(3)
actions = mas.execute_actions()
print("Actions of all agents:", actions)
To run this example:
multi_agent.py
).python multi_agent.py
.AI agents can be enhanced with NLP to process and understand human language. This enables agents to interact with users through natural language.
# Example: Simple NLP-based AI Agent
from nltk.chat.util import Chat, reflections
pairs = [
(r"Hi|Hello", ["Hello!", "Hi there!"]),
(r"(.*) your name?", ["I am an AI agent."]),
(r"(.*) (location|city)?", ["I live in the cloud."]),
]
chatbot = Chat(pairs, reflections)
chatbot.converse()
To run this example:
nltk
library: pip install nltk
nlp_agent.py
).python nlp_agent.py
.Autonomous systems are AI agents that can perform tasks without human intervention, using sensors and decision-making algorithms. These systems are used in areas like robotics and self-driving cars.
# Example: Autonomous Car (simplified)
class AutonomousCar:
def __init__(self):
self.speed = 0
self.direction = "straight"
def sense_environment(self):
return random.choice(['obstacle', 'clear']) # Simulate sensing environment
def take_action(self, environment):
if environment == 'obstacle':
self.speed = 0 # Stop if there's an obstacle
self.direction = "turn_left" # Turn left
else:
self.speed = 50 # Move forward if no obstacle
self.direction = "straight"
# Create autonomous car and control its actions
car = AutonomousCar()
environment = car.sense_environment()
car.take_action(environment)
print("Car action: Speed =", car.speed, "Direction =", car.direction)
To run this example:
autonomous_car.py
).python autonomous_car.py
.AI can be used to automate repetitive tasks, such as data processing, email management, or web scraping.
# Example: Web Scraping for Task Automation
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title.string) # Print the title of the web page
To run this example:
pip install requests beautifulsoup4
AI can automate complex workflows, improving productivity by reducing manual effort and enhancing decision-making.
# Example: Simple Email Sending Automation
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, to):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = 'your_email@example.com'
msg['To'] = to
server = smtplib.SMTP('smtp.example.com')
server.login('your_email@example.com', 'your_password')
server.sendmail(msg['From'], [msg['To']], msg.as_string())
server.quit()
# Send automated email
send_email('Automated Email', 'This is an automated message.', 'recipient@example.com')
To run this example:
AI chatbots are widely used for customer support and other conversational tasks. They simulate human conversation using predefined rules or machine learning models.
# Example: Simple AI Chatbot
from transformers import pipeline
chatbot = pipeline("conversational")
response = chatbot("Hello, how can I help you today?")
print(response[0]['generated_text'])
To run this example:
pip install transformers
AI personal assistants help users with everyday tasks like managing schedules, setting reminders, or making recommendations.
# Example: Simple Personal Assistant
import datetime
class PersonalAssistant:
def greet(self):
return "Hello! How can I assist you today?"
def set_reminder(self, time, task):
return f"Reminder set for {time}: {task}"
# Create personal assistant instance
assistant = PersonalAssistant()
print(assistant.greet())
print(assistant.set_reminder("10:00 AM", "Meeting with Bob"))
To run this example:
personal_assistant.py
).python personal_assistant.py
.Emerging AI architectures represent new approaches in AI design that aim to solve increasingly complex problems. These architectures often focus on performance, scalability, and flexibility for modern AI applications, including deep learning and reinforcement learning.
# Example: Simple Neural Network Architecture
import numpy as np
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights_input_hidden = np.random.rand(self.input_size, self.hidden_size)
self.weights_hidden_output = np.random.rand(self.hidden_size, self.output_size)
def feedforward(self, inputs):
hidden_layer_input = np.dot(inputs, self.weights_input_hidden)
hidden_layer_output = self.sigmoid(hidden_layer_input)
output_layer_input = np.dot(hidden_layer_output, self.weights_hidden_output)
return self.sigmoid(output_layer_input)
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
# Initialize neural network
nn = NeuralNetwork(3, 5, 1)
# Simulate input and perform feedforward computation
inputs = np.array([0.1, 0.2, 0.3])
output = nn.feedforward(inputs)
print("Neural network output:", output)
To run this example:
neural_network.py
).python neural_network.py
.Transformer architectures have revolutionized the AI field, particularly in Natural Language Processing (NLP). These models excel at handling sequential data and are known for their attention mechanism, which allows them to weigh different parts of input data based on relevance.
# Example: Transformer-based Language Model (Hugging Face Library)
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Initialize pre-trained GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained('gpt2')
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
# Encode input text
input_text = "Once upon a time, in a faraway land,"
inputs = tokenizer.encode(input_text, return_tensors='pt')
# Generate text using the model
outputs = model.generate(inputs, max_length=100)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("Generated Text:", generated_text)
To run this example:
transformers
pip install transformerstransformer_model.py
).python transformer_model.py
.Graph Neural Networks (GNNs) are a class of neural networks designed for processing data that is represented as graphs. GNNs excel in tasks such as node classification, graph classification, and link prediction.
# Example: Simple Graph Neural Network using PyTorch Geometric
import torch from torch_geometric.data import Data from torch_geometric.nn import GCNConv # Define the graph (node features, edge indices)
node_features = torch.tensor([[1, 0], [0, 1], [1, 1], [0, 0]], dtype=torch.float) # 4 nodes with 2 features each
edge_indices = torch.tensor([[0, 1, 2], [1, 2, 3]], dtype=torch.long) # edges between nodes
# Create data object for PyTorch Geometric
data = Data(x=node_features, edge_index=edge_indices) class GNNModel(torch.nn.Module): def __init__(self): super(GNNModel, self).__init__() self.conv1 = GCNConv(2, 2) self.conv2 = GCNConv(2, 1) def forward(self, data): x, edge_index = data.x, data.edge_index x = self.conv1(x, edge_index) x = torch.relu(x) x = self.conv2(x, edge_index) return x # Initialize the model
model = GNNModel() # Forward pass through the graph
output = model(data) print("GNN Output:", output) # Output node representations after graph convolution
To run this example:
torch-geometric
library: pip install torch-geometric
gnn_model.py
).python gnn_model.py
.Neuromorphic computing is inspired by the structure and function of the human brain. It aims to mimic neural processing in hardware, allowing for efficient AI models that are capable of learning and decision-making in real-time.
# Example: Neuromorphic Simulation with Spiking Neural Networks
import brian2 as b2 # Define the model parameters
tau_m = 10 * b2.ms # Membrane time constant
V_th = -50 * b2.mV # Threshold potential
V_reset = -65 * b2.mV # Reset potential
# Define the neuron model
eqs = """ dV/dt = (I - (V - V_reset)) / tau_m : volt I : amp """ # Create a neuron group
G = b2.NeuronGroup(1, eqs, threshold='V > V_th', reset='V = V_reset', method='exact') # Initialize neuron state
G.V = -65 * b2.mV # Set input current
G.I = 1.0 * b2.nA # Inject current
# Run the simulation
b2.run(100 * b2.ms) # Display the result
b2.plot(G.t / b2.ms, G.V / b2.mV, label="Neuron Membrane Potential") b2.xlabel("Time (ms)") b2.ylabel("Membrane Potential (mV)") b2.show() # Plot the result
To run this example:
brian2
library: pip install brian2
neuromorphic_model.py
).python neuromorphic_model.py
.Quantum computing leverages the principles of quantum mechanics to solve problems that are intractable for classical computers. AI can benefit from quantum computing, especially in areas such as optimization and machine learning.
# Example: Quantum Computing with Qiskit
from qiskit import QuantumCircuit, Aer, execute # Create a quantum circuit with 2 qubits
qc = QuantumCircuit(2, 2) # Apply quantum gates
qc.h(0) # Apply Hadamard gate
qc.cx(0, 1) # Apply CNOT gate
# Measure the qubits
qc.measure([0, 1], [0, 1]) # Simulate the circuit
simulator = Aer.get_backend('qasm_simulator') result = execute(qc, simulator, shots=1024).result() # Get and display the result
counts = result.get_counts(qc) print("Quantum Measurement Result:", counts)
To run this example:
pip install qiskit
quantum_ai.py
).python quantum_ai.py
.This section discusses various techniques for optimizing AI/ML models such as pruning, quantization, and knowledge distillation to reduce model size, improve inference speed, and maintain accuracy.
# Example: Quantization with PyTorch import torch import torchvision.models as models model = models.resnet18(pretrained=True) # Load pre-trained model model.eval() # Set to evaluation mode # Apply dynamic quantization quantized_model = torch.quantization.quantize_dynamic( model, {torch.nn.Linear}, dtype=torch.qint8 ) # Save quantized model torch.save(quantized_model.state_dict(), "quantized_resnet18.pth") print("Quantized model saved.")
How to run:
pip install torch torchvision
quantize_model.py
python quantize_model.py
Scaling involves distributing workloads across hardware or systems to handle larger datasets or more users. This can be done with batch processing, multi-GPU setups, or distributed computing.
# Example: Multi-GPU training setup in PyTorch import torch import torch.nn as nn import torch.optim as optim class SimpleModel(nn.Module): def __init__(self): super(SimpleModel, self).__init__() self.linear = nn.Linear(100, 1) def forward(self, x): return self.linear(x) model = SimpleModel() if torch.cuda.device_count() > 1: print("Using", torch.cuda.device_count(), "GPUs!") model = nn.DataParallel(model) model.to("cuda") # Move model to GPU print("Model is on GPU.")
How to run:
multi_gpu.py
python multi_gpu.py
Caching frequently used data and optimizing input/output operations helps minimize latency and speeds up model performance in production settings.
# Example: Simple caching using functools.lru_cache from functools import lru_cache @lru_cache(maxsize=32) def get_expensive_data(n): print(f"Calculating for {n}") return n * n print(get_expensive_data(4)) print(get_expensive_data(4)) # Will be retrieved from cache
How to run:
cache_example.py
python cache_example.py
AI systems in production can use load balancing to distribute traffic efficiently across multiple instances, reducing bottlenecks and ensuring high availability.
# Conceptual example using Flask with Gunicorn from flask import Flask app = Flask(__name__) @app.route("/") def index(): return "Load Balanced AI Service" # Run with Gunicorn (outside Python): # gunicorn -w 4 app:app
How to run:
pip install flask gunicorn
app.py
gunicorn -w 4 app:app
Integrating AI systems with enterprise software like CRMs, ERPs, and data lakes is essential for real-world deployment. This requires secure APIs, data connectors, and message brokers.
<!-- HTML Button to Trigger CRM Update -->
<button onclick="updateCRM()">Update CRM</button>
<script>
async function updateCRM() {
const response = await fetch('https://api.example-crm.com/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_TOKEN'
},
body: JSON.stringify({
customer_id: '1234',
update: 'interacted with AI chatbot'
})
});
const result = await response.json();
console.log(result);
}
</script>
To run: Replace API URL and token with actual values from your CRM platform. Open the HTML file and click the button to send an update.
Private clouds offer secure infrastructure within a company’s network. Kubernetes and Docker are often used for deployment.
# Dockerfile
FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
To run: Place this Dockerfile in your AI app directory. Then run:
docker build -t ai-flask-app .
docker run -p 5000:5000 ai-flask-app
AI can automate insights in BI tools like Tableau, PowerBI, or even custom dashboards using libraries like Plotly.
<!-- HTML to Show Chart -->
<div id="chart"></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script>
const data = [{
x: ['Prediction A', 'Prediction B'],
y: [70, 30],
type: 'bar'
}];
Plotly.newPlot('chart', data);
</script>
To run: Save and open the HTML file. You’ll see a chart showing AI prediction outputs.
Enterprise AI must be monitored for performance and errors. Logs can be sent to systems like ELK Stack or Grafana.
<script>
function logEvent(message) {
console.log("[AI Log]", message);
// In production, send to monitoring backend
}
logEvent("AI model started");
</script>
To run: Open HTML in browser and check browser console for log messages.
This section provides an overview of why security is essential in AI systems, especially in enterprises where user data and decisions can have large-scale implications.
<!-- Example 1: Masking Sensitive Data in Input -->
<script>
// Function to mask sensitive credit card numbers
function maskCard(cardNumber) {
return cardNumber.slice(0, 4) + "********" + cardNumber.slice(-4);
}
console.log(maskCard("1234567812345678")); // Output: 1234********5678
</script>
<!-- To run: Paste in browser's DevTools Console -->
<!-- Example 2: API Key Check in Node.js -->
// Sample pseudo-code
const express = require('express');
const app = express();
const API_KEY = "secureapikey123";
app.use((req, res, next) => {
if (req.headers['x-api-key'] !== API_KEY) {
return res.status(403).send('Forbidden');
}
next();
});
<!-- To run: Use Node.js and install Express -->
<!-- Example 3: Simple RBAC Simulation -->
<script>
const users = {
alice: "admin",
bob: "viewer"
};
function hasAccess(user, roleRequired) {
return users[user] === roleRequired;
}
console.log(hasAccess("alice", "admin")); // true
</script>
<!-- To run: Paste in browser's Console -->
<!-- Example 4: Simple Data Encryption using Crypto -->
const crypto = require('crypto');
const secret = 'secretkey';
const message = 'AI Confidential';
const encrypted = crypto.createHmac('sha256', secret)
.update(message)
.digest('hex');
console.log(encrypted);
<!-- To run: Use Node.js environment -->
<!-- Example 5: Load Model from Secure Path -->
const fs = require('fs');
const path = require('path');
const modelPath = path.resolve("/secure/models/model.json");
fs.readFile(modelPath, (err, data) => {
if (err) throw err;
console.log("Model Loaded Securely");
});
<!-- To run: Node.js with access to local model path -->
<!-- Example 6: Detecting Anomalies in Input -->
<script>
function isSuspicious(input) {
return /")); // true
</script>
<!-- Example 7: Logging Model Predictions -->
const fs = require('fs');
const logPrediction = (input, output) => {
const log = `Input: ${input}, Output: ${output}\n`;
fs.appendFileSync('audit.log', log);
};
logPrediction("user_data", "prediction_result");
<!-- To run: Node.js file write permissions required -->
<!-- Example 8: Data Deletion Script -->
const fs = require('fs');
function deleteUserData(userId) {
fs.unlinkSync(`./data/${userId}.json`);
console.log("User data deleted");
}
deleteUserData("user123");
<!-- To run: File must exist in specified path -->
<!-- Example 9: Access Logging for SOC 2 -->
const logAccess = (user, resource) => {
console.log(`${new Date().toISOString()}: ${user} accessed ${resource}`);
};
logAccess("admin", "model-dashboard");
<!-- To run: Node.js or browser console -->
<!-- Example 10: Checking for Package Updates -->
<!-- In terminal run: -->
npm outdated
<!-- Then: -->
npm update
<!-- Keeps libraries secure and patched -->
<!-- This project tracks stock levels and predicts future demand -->
<script>
const inventory = { apples: 100,
bananas: 50,
oranges: 75
};
function predictDemand(currentStock) {
return currentStock * 1.2; // basic prediction logic
}
for (let item in inventory) {
console.log(item + " demand next week: " + predictDemand(inventory[item]));
}
</script>
To run: Copy into an HTML file and open in a browser to see predictions in the console.
<!-- Simulated AI response for user query -->
<script>
function getAIResponse(userInput) {
return "You said: " + userInput;
}
console.log(getAIResponse("What is my order status?"));
</script>
To run: Copy this into a browser console or inside a script tag in an HTML page.
<!-- Uses Web Speech API to recognize voice -->
<script>
const recognition = new webkitSpeechRecognition();
recognition.onresult = function(event) {
const command = event.results[0][0].transcript;
alert("You said: " + command);
};
recognition.start();
</script>
To run: Open in a browser with mic permissions enabled.
<!-- Mimics two users editing a shared document -->
<script>
let documentText = "Initial content.";
function updateText(user, newText) {
documentText += `\n[${user}]: ${newText}`;
console.log(documentText);
}
updateText("Alice", "Added intro section.");
updateText("Bob", "Corrected grammar.");
</script>
To run: Save as HTML and open in browser to simulate user collaboration.