• March 20, 2025

Activation Function vs Cost Fucnction

Both activation functions and cost functions play crucial roles in neural networks, but they serve different purposes in training deep learning models.


1️⃣ Activation Function

🔹 Purpose:

  • Transforms neuron outputs to introduce non-linearity.
  • Helps the network learn complex patterns.
  • Applied at each neuron in the hidden and output layers.

🔹 Examples:

  • ReLU → Used in hidden layers.
  • Sigmoid → Used for binary classification.
  • Softmax → Used for multi-class classification.
  • Tanh → Used in hidden layers for normalization.

🔹 Mathematical Example:
Sigmoid Activation Function:f(x)=11+e−xf(x) = \frac{1}{1 + e^{-x}}f(x)=1+e−x1​

🔹 Example in PyTorch:

import torch.nn.functional as F
x = torch.tensor([-1.0, 0.0, 2.0])
sigmoid_output = F.sigmoid(x)
print(sigmoid_output) # tensor([0.2689, 0.5000, 0.8808])

2️⃣ Cost Function (Loss Function)

🔹 Purpose:

  • Measures the difference between the model’s predictions and actual values.
  • Guides the optimizer in adjusting the model’s weights.
  • Used after forward propagation to compute error.

🔹 Examples:

  • Mean Squared Error (MSE) → For regression.
  • Cross-Entropy Loss → For classification.
  • Hinge Loss → Used for SVM models.

🔹 Mathematical Example:
Cross-Entropy Loss (for classification):L=−∑ylog⁡(y^)L = – \sum y \log(\hat{y})L=−∑ylog(y^​)

Where y is the actual label and ŷ is the predicted probability.

🔹 Example in PyTorch:

import torch.nn as nn
pred = torch.tensor([0.2, 0.8]) # Model output (probabilities)
target = torch.tensor([0, 1]) # Actual labels (one-hot encoded)
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(pred.unsqueeze(0), target.unsqueeze(0))
print(loss)

🔑 Key Differences

FeatureActivation FunctionCost Function
PurposeConverts neuron output into meaningful valuesMeasures the error in predictions
Used inHidden & output layersAfter forward pass (during training)
AffectsNon-linearity & learning abilityModel optimization & weight updates
OutputTransformed neuron valuesA single scalar loss value
ExamplesReLU, Sigmoid, SoftmaxMSE, Cross-Entropy, Hinge Loss

🛠️ When to Use Each?

  • Use an activation function to ensure neurons can model complex relationships.
  • Use a cost function to evaluate how well the model is performing.

🚀 Final Thought

Activation functions shape neuron outputs.
Cost functions evaluate model accuracy and guide optimization.

Let me know if you need further clarification! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *