Deep Learning
Neural Networks
Build and train neural networks with TensorFlow/Keras for complex pattern recognition.
Neural Networks
Inspired by the human brain, neural networks are layers of connected nodes (neurons) that learn to recognize patterns.
Architecture
- Input layer: Receives raw data
- Hidden layers: Extract increasingly abstract features
- Output layer: Produces predictions
Key Concepts
- Activation functions: ReLU, Sigmoid, Softmax
- Loss functions: MSE (regression), Cross-entropy (classification)
- Optimizer: Adam, SGD — updates weights to minimize loss
- Epochs: Number of times the model sees all training data
- Batch size: How many samples to process before updating weights
Overfitting Prevention
- Dropout: Randomly deactivate neurons during training
- Regularization: L1/L2 penalty on large weights
- Early stopping: Stop training when validation loss stops improving
- Data augmentation: Create variations of training data
Example
python
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Load MNIST dataset (handwritten digits)
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess
X_train = X_train / 255.0 # normalize to [0, 1]
X_test = X_test / 255.0
# Build model
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # 784 inputs
keras.layers.Dense(256, activation='relu'),
keras.layers.Dropout(0.3), # prevent overfitting
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.3),
keras.layers.Dense(10, activation='softmax') # 10 classes
])
model.summary() # print model architecture
# Compile
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Train
history = model.fit(
X_train, y_train,
epochs=10,
batch_size=128,
validation_split=0.1,
callbacks=[
keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)
]
)
# Evaluate
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print(f"Test accuracy: {test_accuracy:.4f}")
# Predict
predictions = model.predict(X_test[:5])
predicted_classes = np.argmax(predictions, axis=1)
print("Predictions:", predicted_classes)
print("Actual:", y_test[:5])Try it yourself — PYTHON