Deep Learning • CNN • RNN • LSTM • Optimizers • Pipelines • Deployment • Ethics
1. Deep Learning
Deep Learning is a field of Machine Learning that uses neural networks with many layers. These networks can learn very complex patterns from large amounts of data.
Deep learning powers many modern technologies:
- Voice assistants
- Medical imaging
- Self-driving cars
- Face recognition
- ChatGPT-like systems
Deep learning works using:
- An input layer
- Several hidden layers
- An output layer
- A loss function
- An optimizer
- Many training cycles (epochs)
Python Example – Simple Deep Learning Model
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation="relu"),
tf.keras.layers.Dense(16, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
print(model.summary())2. CNN – Convolutional Neural Networks (Computer Vision)
CNNs are neural networks designed for image processing.
A CNN has three main types of layers:
- Convolution Layer – detects edges, corners, shapes
- Pooling Layer – reduces image size
- Fully Connected Layer – makes the final decision
CNNs are widely used in:
- Face recognition
- Security cameras
- Traffic sign detection
- Medical X-ray analysis
- Object detection models (YOLO, SSD, etc.)
Python Example – Simple CNN (MNIST Digits)
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation="relu", input_shape=(28,28,1)),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
print(model.summary())3. RNN + LSTM (Natural Language Processing)
RNNs (Recurrent Neural Networks) are used for sequence-based data, such as:
- Sentences
- Time-series values
- Music
- Sensor readings
LSTM (Long Short-Term Memory) is an improved RNN that remembers information long-term and prevents forgetting.
Used in:
- Text generation
- Translation
- Speech recognition
- Chatbots
- Stock prediction
Python Example – LSTM for Text Sentiment
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Embedding(5000, 32),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
print(model.summary())4. Optimizers
Optimizers control how a neural network learns by adjusting weights after each training step.
Most common optimizers:
- SGD (Stochastic Gradient Descent) – slow but reliable
- Adam – fast and popular
- RMSProp – good for unstable data
Adam is used most often in industry projects.
Python Example – Using Adam Optimizer
model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"])5. Hyperparameter Tuning
Hyperparameters are training settings that you choose manually:
- Learning rate
- Batch size
- Number of layers
- Number of neurons
- Activation functions
- Dropout rates
Good tuning makes models faster, more accurate, and more stable.
Common methods:
- Grid search
- Random search
- Bayesian optimization
- Keras tuner
Python Example – Adjusting Hyperparameters
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(32, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0005)
model.compile(optimizer=optimizer,
loss="binary_crossentropy",
metrics=["accuracy"])6. Building Full ML Pipelines
A pipeline organizes ML steps in one clean workflow:
- Load data
- Preprocess data
- Scale features
- Train model
- Evaluate model
- Save/load model
Pipelines reduce mistakes and make ML production-ready.
Python Example – sklearn Pipeline
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipeline = Pipeline([
("scale", StandardScaler()),
("model", LogisticRegression())
])
pipeline.fit(X_train, y_train)
print(pipeline.score(X_test, y_test))7. Model Deployment Basics (Flask)
Deployment means making your model accessible to users, apps, or websites. With Flask, you can turn a trained model into an API.
Steps:
- Train model
- Save model
- Create Flask server
- Make prediction endpoint
- Send data from frontend/app
Python Example – Flask Deployment
from flask import Flask, request, jsonify
import joblib
app = Flask(__name__)
model = joblib.load("model.pkl")
@app.route("/predict", methods=["POST"])
def predict():
value = request.json["value"]
prediction = model.predict([[value]])
return jsonify({"prediction": int(prediction[0])})
app.run()8. AI Ethics & Safe Usage
Ethical AI ensures that systems are:
- Fair
- Safe
- Reliable
- Transparent
- Respectful of privacy
AI should not cause harm, show bias, or make dangerous decisions.
Principles of ethical AI:
- Avoid bias in datasets
- Protect user data
- Be transparent about AI use
- Evaluate risks and misuse
- Ensure human supervision
- Prevent harmful behavior