Machine Learning Fundamentals
Machine Learning Course - Lesson 7: Advanced Topics in Machine Learning
Lesson Overview
In this lesson, we will explore advanced topics in machine learning, focusing on deep learning and neural networks. We will cover the fundamentals of neural networks, deep learning architectures, and techniques for handling large-scale datasets. This lesson will include practical code examples using popular deep learning libraries.
1. Neural Networks
1.1 Introduction to Neural Networks
Neural networks are computational models inspired by the human brain, consisting of interconnected nodes (neurons) organized in layers. Each neuron processes input, applies a weight, adds a bias, and uses an activation function to produce an output.
- Input Layer: Receives the input features.
- Hidden Layers: Perform computations and transformations on the input data.
- Output Layer: Produces the final prediction.
Example Code (Python) - Basic Neural Network with Keras:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
# Load and prepare dataset
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Define the model
model = Sequential([
Dense(10, activation='relu', input_shape=(X_train.shape[1],)),
Dense(10, activation='relu'),
Dense(3, activation='softmax') # Output layer with softmax for classification
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=30, batch_size=10, validation_split=0.2)
# Make predictions
y_pred = model.predict(X_test)
y_pred_classes = y_pred.argmax(axis=1)
# Evaluate the model
print("Accuracy:", accuracy_score(y_test, y_pred_classes))
2. Deep Learning Architectures
2.1 Convolutional Neural Networks (CNNs)
CNNs are designed for processing structured grid data, such as images. They use convolutional layers to automatically and adaptively learn spatial hierarchies of features.
Example Code (Python) - Basic CNN with Keras:
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 prepare dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# Define the model
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(64, kernel_size=(3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=5, batch_size=64, validation_split=0.1)
# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
2.2 Recurrent Neural Networks (RNNs)
RNNs are designed for sequential data, such as time series or natural language. They have loops allowing information to persist.
Example Code (Python) - Basic RNN with Keras:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
# Prepare dummy sequential data
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y = np.array([1, 0, 1])
X = pad_sequences(X, padding='post', maxlen=5)
# Define the model
model = Sequential([
SimpleRNN(50, activation='relu', input_shape=(X.shape[1], 1)),
Dense(1, activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X, y, epochs=10, batch_size=1)
# Make predictions
y_pred = model.predict(X)
print("Predictions:", y_pred)
3. Techniques for Handling Large-Scale Datasets
3.1 Data Augmentation
Data augmentation involves creating new training samples by applying transformations such as rotations, flips, and shifts. This helps improve the robustness of models, especially for image data.
Example Code (Python) - Data Augmentation with Keras:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define data augmentation parameters
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
# Sample image (dummy data)
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(1, 28, 28, 1) # Dummy image data
# Generate augmented images
for batch in datagen.flow(x, batch_size=1):
plt.figure(figsize=(2, 2))
plt.imshow(batch[0].reshape(28, 28), cmap='gray')
plt.axis('off')
plt.show()
break
3.2 Distributed Training
Distributed training involves splitting the training process across multiple machines or devices to handle large-scale datasets more efficiently.
Example Code (Python) - Distributed Training with TensorFlow:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Define a distributed strategy
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
# Define the model
model = Sequential([
Dense(64, activation='relu', input_shape=(784,)),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Dummy data
X = np.random.rand(1000, 784)
y = np.random.randint(10, size=(1000,))
# Train the model
model.fit(X, y, epochs=5, batch_size=32)
4. Summary
In this lesson, we explored advanced topics in machine learning, including:
- Neural Networks: Basic neural networks, their architecture, and simple examples using Keras.
- Deep Learning Architectures: CNNs for image data and RNNs for sequential data, with practical code examples.
- Techniques for Handling Large-Scale Datasets: Data augmentation and distributed training.
In the next lesson, we will cover practical applications of machine learning, including deploying models in production, creating APIs for machine learning models, and using cloud services.