๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #352

PyTorch Activations

A practical reference for every activation function covered conceptually in the Activation Functions category, showing both the module and functional forms PyTorch provides.

Module vs Functional Form

import torch
import torch.nn as nn
import torch.nn.functional as F

x = torch.randn(5)

# Module form -- used as a LAYER inside nn.Sequential or __init__
relu_layer = nn.ReLU()
output1 = relu_layer(x)

# Functional form -- called DIRECTLY inside forward(), no separate layer object needed
output2 = F.relu(x)

print(torch.equal(output1, output2))   # True -- identical computation, different calling convention

The module form is preferred when the activation needs to be part of an nn.Sequential chain or tracked as a submodule; the functional form is common (and slightly more concise) when writing a custom forward() method directly.

Common Activations, Quick Reference

ActivationModuleFunctionalConcept Note
ReLUnn.ReLU()F.relu(x)ReLU
Sigmoidnn.Sigmoid()torch.sigmoid(x)Sigmoid Function
Tanhnn.Tanh()torch.tanh(x)Tanh Function
Softmaxnn.Softmax(dim=-1)F.softmax(x, dim=-1)Softmax Function
GELUnn.GELU()F.gelu(x)GELU
Leaky ReLUnn.LeakyReLU(0.01)F.leaky_relu(x, 0.01)Leaky ReLU

Common Mistakes

  • Forgetting to specify dim for Softmax/F.softmax โ€” softmax must normalize along a specific dimension (typically the class dimension); omitting or mis-specifying this produces silently incorrect probabilities.
  • Applying nn.Softmax before nn.CrossEntropyLoss, which already applies it internally โ€” this exact double-softmax mistake has been flagged repeatedly across this hub because it's genuinely one of the most common practical PyTorch bugs.

Interview Relevance

Q: "What's the practical difference between using nn.ReLU() and F.relu() in a model's forward pass?" They compute the exact same operation โ€” the difference is purely about code structure. nn.ReLU() creates a reusable layer object, typically assigned in __init__ and useful within nn.Sequential chains or when you need the activation tracked as a named submodule. F.relu() is a direct functional call, often used inline inside a custom forward() method without needing a separate layer instance.

Practice Question

Why must the dim argument for softmax be chosen carefully for a batch of shape (batch_size, num_classes)?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

PyTorch Activations โ€“ FAQs

Quick answers about learning PyTorch Activations in Deep Learning.

This free note from CodingNow 2.0 explains PyTorch Activations in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including PyTorch Activations, is 100% free with no signup required.
With focused practice, most students grasp PyTorch Activations in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now