Data Science Machine Learning Data Analysis
38.8K subscribers
3.67K photos
31 videos
39 files
1.27K links
ads: @HusseinSheikho

This channel is for Programmers, Coders, Software Engineers.

1- Data Science
2- Machine Learning
3- Data Visualization
4- Artificial Intelligence
5- Data Analysis
6- Statistics
7- Deep Learning
Download Telegram
Topic: CNN (Convolutional Neural Networks) – Part 4: Training, Loss Functions, and Evaluation Metrics

---

1. Preparing for Training

To train a CNN, we need:

Dataset – Typically image data with labels (e.g., MNIST, CIFAR-10).

Loss Function – Measures the difference between predicted and actual values.

Optimizer – Updates model weights based on gradients.

Evaluation Metrics – Accuracy, precision, recall, F1 score, etc.

---

2. Common Loss Functions for CNNs

CrossEntropyLoss – For multi-class classification (most common).

criterion = nn.CrossEntropyLoss()


BCELoss – For binary classification.

---

3. Optimizers

SGD (Stochastic Gradient Descent)
Adam – Adaptive learning rate; widely used for faster convergence.

optimizer = torch.optim.Adam(model.parameters(), lr=0.001)


---

4. Basic Training Loop in PyTorch

for epoch in range(num_epochs):
model.train()
running_loss = 0.0

for images, labels in train_loader:
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()

print(f"Epoch {epoch+1}, Loss: {running_loss:.4f}")


---

5. Evaluating the Model

correct = 0
total = 0
model.eval()

with torch.no_grad():
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

accuracy = 100 * correct / total
print(f"Test Accuracy: {accuracy:.2f}%")


---

6. Tips for Better CNN Training

• Normalize images.

• Shuffle training data for better generalization.

• Use validation sets to monitor overfitting.

• Save checkpoints (torch.save(model.state_dict())).

---

Summary

• CNN training involves feeding batches of images, computing loss, backpropagation, and updating weights.

• Evaluation metrics like accuracy help track progress.

• Loss functions and optimizers are critical for learning quality.

---

Exercise

• Train a CNN on CIFAR-10 for 10 epochs using CrossEntropyLoss and Adam, then print accuracy and plot loss over epochs.

---

#CNN #DeepLearning #Training #LossFunction #ModelEvaluation

https://t.iss.one/DataScienceM
7
#CNN #DeepLearning #Python #Tutorial

Lesson: Building a Convolutional Neural Network (CNN) for Image Classification

This lesson will guide you through building a CNN from scratch using TensorFlow and Keras to classify images from the CIFAR-10 dataset.

---

Part 1: Setup and Data Loading

First, we import the necessary libraries and load the CIFAR-10 dataset. This dataset contains 60,000 32x32 color images in 10 classes.

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np

# Load the CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()

# Check the shape of the data
print("Training data shape:", x_train.shape)
print("Test data shape:", x_test.shape)

#TensorFlow #Keras #DataLoading

---

Part 2: Data Exploration and Preprocessing

We need to prepare the data before feeding it to the network. This involves:
Normalization: Scaling pixel values from the 0-255 range to the 0-1 range.
One-Hot Encoding: Converting class vectors (integers) to a binary matrix.

Let's also visualize some images to understand our data.

# Define class names for CIFAR-10
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

# Visualize a few images
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i])
plt.xlabel(class_names[y_train[i][0]])
plt.show()

# Normalize pixel values to be between 0 and 1
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# One-hot encode the labels
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)

#DataPreprocessing #Normalization #Visualization

---

Part 3: Building the CNN Model

Now, we'll construct our CNN model. A common architecture consists of a stack of Conv2D and MaxPooling2D layers, followed by Dense layers for classification.

Conv2D: Extracts features (like edges, corners) from the input image.
MaxPooling2D: Reduces the spatial dimensions (downsampling), which helps in making the feature detection more robust.
Flatten: Converts the 2D feature maps into a 1D vector.
Dense: A standard fully-connected neural network layer.

model = models.Sequential()

# Convolutional Base
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.Conv2D(64, (3, 3), activation='relu'))

# Flatten and Dense Layers
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax')) # 10 output classes

# Print the model summary
model.summary()

#ModelBuilding #CNN #KerasLayers

---

Part 4: Compiling the Model

Before training, we need to configure the learning process. This is done via the compile() method, which requires:
Optimizer: An algorithm to update the model's weights (e.g., 'adam').
Loss Function: A function to measure how inaccurate the model is during training (e.g., 'categorical_crossentropy' for multi-class classification).
Metrics: Used to monitor the training and testing steps (e.g., 'accuracy').

model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

#ModelCompilation #Optimizer #LossFunction

---